Merge pull request #1126 from consul/ballot-model-specs

Ballot model specs
This commit is contained in:
Enrique García
2016-05-24 14:12:42 +02:00
6 changed files with 59 additions and 16 deletions

View File

@@ -2,9 +2,16 @@ 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
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

View File

@@ -3,7 +3,6 @@ class Budget
class Line < ActiveRecord::Base
belongs_to :ballot
belongs_to :investment
end
end
end

View File

@@ -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

View File

@@ -0,0 +1,5 @@
class DeletesHeadingIdFromBallot < ActiveRecord::Migration
def change
remove_column :budget_ballots, :heading_id
end
end

View File

@@ -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"

View File

@@ -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