Skip to content

Commit

Permalink
Merge pull request #146 from cobudget/refactor/simplify-group-balance
Browse files Browse the repository at this point in the history
Move Group balance calculation to Group model
  • Loading branch information
data-doge authored Aug 12, 2016
2 parents af2a1e9 + 3d662bf commit 1e80cfb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
10 changes: 9 additions & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ def add_member(user)
memberships.create!(member: user, is_admin: false)
end

def total_allocations
allocations.sum(:amount)
end

def total_contributions
Contribution.where(bucket_id: buckets.map(&:id)).sum(:amount)
end

def balance
memberships.map { |m| m.balance }.reduce(:+) || 0
(total_allocations - total_contributions).floor || 0
end

def formatted_balance
Expand Down
57 changes: 47 additions & 10 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rails_helper'

RSpec.describe Group, :type => :model do
describe "#add_admin(user)" do
describe "#add_admin(user)" do
context "user already member of group" do
it "makes the user an admin of group" do
make_user_group_member
make_user_group_member
group.add_admin(user)
expect(group.memberships.find_by(member: user, is_admin: true)).to be_truthy
end
Expand All @@ -16,16 +16,53 @@
user = create(:user)
group.add_admin(user)
expect(group.memberships.find_by(member: user, is_admin: true)).to be_truthy
end
end
end
end
end

describe "#balance" do
context "if no allocations or contributions" do
it "returns 0" do
group = create(:group)
expect(group.balance).to eq(0)
describe "group balance calculations" do
subject(:group) { create(:group) }
let(:bucket) { create(:bucket, group: group) }

describe "#total_allocations" do
it "calculates all allocations" do
create(:allocation, group: group, amount: 100)
create(:allocation, group: group, amount: 100.5)
expect(subject.total_allocations).to eq 200.5
end
end

describe "#total_contributions" do
it "calculates all contributions" do
create(:contribution, bucket: bucket, amount: 100)
create(:contribution, bucket: bucket, amount: 100)
expect(subject.total_contributions).to eq 200
end
end

describe "#balance" do
context "if no allocations or contributions" do
it "returns 0" do
expect(subject.balance).to eq(0)
end
end

context "with allocations and contributions" do
it "calculates the group balance" do
create(:allocation, group: group, amount: 500)
create(:allocation, group: group, amount: 100)
create(:contribution, bucket: bucket, amount: 100)
create(:contribution, bucket: bucket, amount: 100)
# contribution factory creates an allocation equalling
# the contribution amount
expect(subject.reload.balance).to eq 600
end

it "rounds the balance down" do
create(:allocation, group: group, amount: 500.50)
expect(subject.reload.balance).to eq 500
end
end
end
end
end
end

0 comments on commit 1e80cfb

Please sign in to comment.