Skip to content

Commit

Permalink
Adding per-user expenses coefficient and renaming food multiplier to …
Browse files Browse the repository at this point in the history
…food coefficient
  • Loading branch information
rmatei committed Jul 20, 2011
1 parent 1c9d13e commit 18f1ea9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Expand Up @@ -2,7 +2,7 @@ class UsersController < ApplicationController

active_scaffold :user do |config|
# Limited column display for usability
config.list.columns = [:profile_picture, :full_name, :venmo_username, :total_spent, :expenses, :payments, :tallied_consumptions, :monthly_rent, :food_multiplier]
config.list.columns = [:profile_picture, :full_name, :venmo_username, :total_spent, :expenses, :payments, :tallied_consumptions, :monthly_rent, :food_coefficient, :expenses_coefficient]

# If using full display, comment line above and uncomment lines below
# config.columns << :venmo_id
Expand All @@ -12,7 +12,7 @@ class UsersController < ApplicationController
config.create.columns.remove :expenses

# Don't allow any changes to DB, just reading
config.actions = [:list, :show, :search]
# config.actions = [:list, :show, :search]
end

end
14 changes: 11 additions & 3 deletions app/helpers/users_helper.rb
Expand Up @@ -12,9 +12,17 @@ def total_spent_column(record)
number_to_currency record.total_spent
end

def food_multiplier_column(record)
if record.food_multiplier
number_to_percentage record.food_multiplier*100, :precision => 0
def food_coefficient_column(record)
if record.food_coefficient
number_to_percentage record.food_coefficient*100, :precision => 0
else
nil
end
end

def expenses_coefficient_column(record)
if record.expenses_coefficient
number_to_percentage record.expenses_coefficient*100, :precision => 0
else
nil
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/billing_period.rb
Expand Up @@ -129,12 +129,12 @@ def split_general_expenses
# adds food expenses to each payment
# these are split proportionally to a food multiplier that ranges from 0 to 1
def split_food_expenses
total_food_multiplier = User.all.map(&:food_multiplier).inject{|sum,x| sum + x }
total_food_coefficient = User.all.map(&:food_coefficient).inject{|sum,x| sum + x }
payments.each do |payment| # for each user
details = []
total_for_person = 0
expenses.food.sort_by {|e| e.amount}.reverse.each do |expense| # for each expense in category
amount_for_item = expense.amount * payment.user.food_multiplier / total_food_multiplier
amount_for_item = expense.amount * payment.user.food_coefficient / total_food_coefficient
total_for_person += amount_for_item
details << {:amount => amount_for_item, :note => expense.note, :time => expense.created_at}
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20110309020248_create_users.rb
Expand Up @@ -9,7 +9,7 @@ def self.up
t.string :profile_picture
t.string :email
t.float :monthly_rent
t.float :food_multiplier
t.float :food_coefficient

t.timestamps
end
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20110720164056_add_expenses_coefficient_to_user.rb
@@ -0,0 +1,11 @@
class AddExpensesCoefficientToUser < ActiveRecord::Migration
def self.up
add_column :users, :expenses_coefficient, :float, :default => 1
rename_column :users, :food_multiplier, :food_coefficient
change_column_default :users, :food_coefficient, 1
end

def self.down
remove_column :users, :expenses_coefficient
end
end

0 comments on commit 18f1ea9

Please sign in to comment.