From 2a51355046b466197cef149c731ead42d98ecfa5 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 17:21:53 +0200 Subject: [PATCH] fix shamepie, break distribution --- app/controllers/concerns/statistics.rb | 15 ++++++++------- app/models/user.rb | 3 +++ app/views/pages/landing.html.haml | 2 +- config/application.rb | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index 3783d67..6d91c67 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -2,18 +2,19 @@ class Statistics < Rails::Application def shameful_users - User.where('balance > :amount', amount: config.shameful_balance) - .order(balance: :desc) + User.humans + .where('-balance > :amount', amount: config.shameful_balance) + .order(:balance) end def total_debt - User.where.not(id: User.zeus).where('balance > 0').sum(:balance) + -User.humans.where('balance < 0').sum(:balance) end def shamehash - none_shaming = shameful_users.sum(:balance) - shameful_users.inject({'None-shameful users' => none_shaming}) do |h, u| - h.merge({u.name => u.balance}) + none_shaming = total_debt + shameful_users.sum(:balance) + shameful_users.inject({'Reputable users' => none_shaming.to_f / total_debt}) do |h, u| + h.merge({u.name => - u.balance.to_f / total_debt}) end end @@ -24,7 +25,7 @@ class Statistics < Rails::Application end def amount_distribution - Transaction.group("round(amount / 1000, 2)").count.inject(Hash.new) do |hash, (group, count)| + Transaction.group("round(amount / 1000.0, 2)").count.inject(Hash.new) do |hash, (group, count)| hash.merge({10 * group.to_i => count}) end end diff --git a/app/models/user.rb b/app/models/user.rb index d490c29..d3c234b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,6 +23,8 @@ class User < ActiveRecord::Base validates :name, presence: true, uniqueness: true + scope :humans, -> { where.not(id: self.zeus) } + def transactions Transaction.where("creditor_id = ? OR debtor_id = ?", id, id) end @@ -42,4 +44,5 @@ class User < ActiveRecord::Base def self.zeus find_or_create_by name: 'Zeus' end + end diff --git a/app/views/pages/landing.html.haml b/app/views/pages/landing.html.haml index 765edb7..eef0fbc 100644 --- a/app/views/pages/landing.html.haml +++ b/app/views/pages/landing.html.haml @@ -20,7 +20,7 @@ %td.shameful-person= user.name // Won't divide by zero because there won't be any users with // a shameful debt if the total debt is zero. - %td.shame-percentage= "#{100 * user.balance / @statistics.total_debt}%" + %td.shame-percentage= "#{-100 * user.balance / @statistics.total_debt}%" .pure-u-1-2 %h3 Pie of Shame = pie_chart @statistics.shamehash diff --git a/config/application.rb b/config/application.rb index 5525edd..90e0743 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,6 +24,6 @@ module Tab config.active_record.raise_in_transactional_callbacks = true # Which is the lowest balance you should be ashamed of. - config.shameful_balance = 50 # In eurocents! + config.shameful_balance = 5000 # In eurocents! end end