Refactor concept of current budget

When there was only one budget this implementation worked fine

Nowadays there can be multiple budgets, and therefore the definition of
the current_budget has changed. It is no longer a budget that has not
finished, but rather, the last budget created that is not in the
initial drafting phase.

Budgets in the drafting phase are not considered the current_budget,
but rather a budget that is still being prepared and that soon will
become the current_budget
This commit is contained in:
rgarcia
2018-01-15 18:37:03 +01:00
parent 77e10ac297
commit 9176de949a
2 changed files with 28 additions and 5 deletions

View File

@@ -31,7 +31,9 @@ class Budget < ActiveRecord::Base
scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") }
scope :finished, -> { where(phase: "finished") }
scope :current, -> { where.not(phase: "finished") }
def self.current
where.not(phase: "drafting").last
end
def description
send("description_#{phase}").try(:html_safe)
@@ -93,10 +95,6 @@ class Budget < ActiveRecord::Base
balloting_process? || finished?
end
def current?
!finished?
end
def heading_price(heading)
heading_ids.include?(heading.id) ? heading.price : -1
end

View File

@@ -98,6 +98,31 @@ describe Budget do
end
end
describe "#current" do
it "returns nil if there is only one budget and it is still in drafting phase" do
budget = create(:budget, phase: "drafting")
expect(Budget.current).to eq(nil)
end
it "returns the budget if there is only one and not in drafting phase" do
budget = create(:budget, phase: "accepting")
expect(Budget.current).to eq(budget)
end
it "returns the last budget created that is not in drafting phase" do
old_budget = create(:budget, phase: "finished", created_at: 2.years.ago)
previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago)
current_budget = create(:budget, phase: "accepting", created_at: 1.month.ago)
next_budget = create(:budget, phase: "drafting", created_at: 1.week.ago)
expect(Budget.current).to eq(current_budget)
end
end
describe "heading_price" do
let(:budget) { create(:budget) }
let(:group) { create(:budget_group, budget: budget) }