From c48059594b478b16611ccf8c528e41d0c216fdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 23 May 2016 16:35:05 +0200 Subject: [PATCH 1/4] removes belongs_to :heading relation from ballot --- app/models/budget/ballot.rb | 1 - db/migrate/20160523143320_deletes_heading_id_from_ballot.rb | 5 +++++ db/schema.rb | 5 +---- 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20160523143320_deletes_heading_id_from_ballot.rb diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index b56a514b0..cf472d95c 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -2,7 +2,6 @@ class Budget class Ballot < ActiveRecord::Base belongs_to :user belongs_to :budget - belongs_to :heading has_many :lines, dependent: :destroy has_many :spending_proposals, through: :lines diff --git a/db/migrate/20160523143320_deletes_heading_id_from_ballot.rb b/db/migrate/20160523143320_deletes_heading_id_from_ballot.rb new file mode 100644 index 000000000..3a7d17e67 --- /dev/null +++ b/db/migrate/20160523143320_deletes_heading_id_from_ballot.rb @@ -0,0 +1,5 @@ +class DeletesHeadingIdFromBallot < ActiveRecord::Migration + def change + remove_column :budget_ballots, :heading_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ea5199e03..c6f6ffe67 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160520114820) do +ActiveRecord::Schema.define(version: 20160523143320) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -78,11 +78,8 @@ ActiveRecord::Schema.define(version: 20160520114820) do t.integer "budget_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "heading_id" end - add_index "budget_ballots", ["heading_id"], name: "index_budget_ballots_on_heading_id", using: :btree - create_table "budget_headings", force: :cascade do |t| t.integer "budget_id" t.integer "geozone_id" From 282553543f240cfc74acbf84abd43f93d3c491db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 23 May 2016 16:36:33 +0200 Subject: [PATCH 2/4] removes forum and delegated votes from investments --- app/models/budget/investment.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index e1e193a81..a736751ad 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -133,15 +133,7 @@ class Budget end def total_votes - cached_votes_up + physical_votes + delegated_votes - forum_votes - end - - def delegated_votes - count = 0 - representative_voters.each do |voter| - count += voter.forum.represented_users.select { |u| !u.voted_for?(self) }.count - end - return count + cached_votes_up + physical_votes end def code From 1cae255555f4b0c3696709ab937175a494afee35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 23 May 2016 16:37:01 +0200 Subject: [PATCH 3/4] changes sps to investments in ballot relation --- app/models/budget/ballot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index cf472d95c..4b683134b 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -4,6 +4,6 @@ class Budget belongs_to :budget has_many :lines, dependent: :destroy - has_many :spending_proposals, through: :lines + has_many :investments, through: :lines end end From 57642075a730738be3047703349ab6195462701f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 23 May 2016 16:37:29 +0200 Subject: [PATCH 4/4] adds ballot model spec --- app/models/budget/ballot.rb | 8 ++++++ app/models/budget/ballot/line.rb | 1 - spec/models/budget/ballot_spec.rb | 43 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 spec/models/budget/ballot_spec.rb diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index 4b683134b..6dbd67aed 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -5,5 +5,13 @@ class Budget has_many :lines, dependent: :destroy has_many :investments, through: :lines + + def total_amount_spent + investments.sum(:price).to_i + end + + def amount_spent(heading) + investments.by_heading(heading).sum(:price).to_i + end end end diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 01b320579..804ee3a2c 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -3,7 +3,6 @@ class Budget class Line < ActiveRecord::Base belongs_to :ballot belongs_to :investment - end end end diff --git a/spec/models/budget/ballot_spec.rb b/spec/models/budget/ballot_spec.rb new file mode 100644 index 000000000..df31c9eae --- /dev/null +++ b/spec/models/budget/ballot_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +describe Budget::Ballot do + + describe "#amount_spent" do + it "returns the total amount spent in investments" do + inv1 = create(:budget_investment, :feasible, price: 10000) + inv2 = create(:budget_investment, :feasible, price: 20000) + + ballot = create(:budget_ballot) + ballot.investments << inv1 + + expect(ballot.total_amount_spent).to eq 10000 + + ballot.investments << inv2 + + expect(ballot.total_amount_spent).to eq 30000 + end + end + + describe "#amount_spent by heading" do + it "returns the amount spent on all investments assigned to a specific heading" do + heading = create(:budget_heading) + inv1 = create(:budget_investment, :feasible, price: 10000, heading: heading) + inv2 = create(:budget_investment, :feasible, price: 20000, heading: create(:budget_heading)) + inv3 = create(:budget_investment, :feasible, price: 25000) + inv4 = create(:budget_investment, :feasible, price: 40000, heading: heading) + + ballot = create(:budget_ballot) + ballot.investments << inv1 + ballot.investments << inv2 + ballot.investments << inv3 + + expect(ballot.amount_spent(heading.id)).to eq 10000 + expect(ballot.amount_spent(nil)).to eq 25000 + + ballot.investments << inv4 + + expect(ballot.amount_spent(heading.id)).to eq 50000 + end + end + +end \ No newline at end of file