Create all Phases after a Budget creation
This commit is contained in:
@@ -18,6 +18,8 @@ class Budget < ActiveRecord::Base
|
||||
|
||||
before_validation :sanitize_descriptions
|
||||
|
||||
after_create :generate_phases
|
||||
|
||||
scope :drafting, -> { where(phase: "drafting") }
|
||||
scope :accepting, -> { where(phase: "accepting") }
|
||||
scope :reviewing, -> { where(phase: "reviewing") }
|
||||
@@ -156,5 +158,17 @@ class Budget < ActiveRecord::Base
|
||||
send("description_#{phase}=", sanitized)
|
||||
end
|
||||
end
|
||||
|
||||
def generate_phases
|
||||
Budget::Phase::PHASE_KINDS.each do |phase|
|
||||
Budget::Phase.create(
|
||||
budget: self,
|
||||
kind: phase,
|
||||
prev_phase: phases&.last,
|
||||
starts_at: phases&.last&.ends_at || Date.current,
|
||||
ends_at: (phases&.last&.ends_at || Date.current) + 1.month
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ require 'rails_helper'
|
||||
|
||||
describe Budget do
|
||||
|
||||
let(:budget) { create(:budget) }
|
||||
|
||||
it_behaves_like "sluggable"
|
||||
|
||||
describe "name" do
|
||||
@@ -15,20 +17,38 @@ describe Budget do
|
||||
end
|
||||
|
||||
describe "description" do
|
||||
it "changes depending on the phase" do
|
||||
budget = create(:budget)
|
||||
describe "Without Budget::Phase associated" do
|
||||
before do
|
||||
budget.phases.destroy_all
|
||||
end
|
||||
|
||||
Budget::Phase::PHASE_KINDS.each do |phase|
|
||||
budget.phase = phase
|
||||
expect(budget.description).to eq(budget.send("description_#{phase}"))
|
||||
expect(budget.description).to be_html_safe
|
||||
it "changes depending on the phase, falling back to budget description attributes" do
|
||||
Budget::Phase::PHASE_KINDS.each do |phase_kind|
|
||||
budget.phase = phase_kind
|
||||
expect(budget.description).to eq(budget.send("description_#{phase_kind}"))
|
||||
expect(budget.description).to be_html_safe
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "With associated Budget::Phases" do
|
||||
before do
|
||||
budget.phases.each do |phase|
|
||||
phase.description = phase.kind.humanize
|
||||
phase.save
|
||||
end
|
||||
end
|
||||
|
||||
it "changes depending on the phase" do
|
||||
Budget::Phase::PHASE_KINDS.each do |phase_kind|
|
||||
budget.phase = phase_kind
|
||||
expect(budget.description).to eq(phase_kind.humanize)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "phase" do
|
||||
let(:budget) { create(:budget) }
|
||||
|
||||
it "is validated" do
|
||||
Budget::Phase::PHASE_KINDS.each do |phase|
|
||||
budget.phase = phase
|
||||
@@ -99,7 +119,6 @@ describe Budget do
|
||||
end
|
||||
|
||||
describe "heading_price" do
|
||||
let(:budget) { create(:budget) }
|
||||
let(:group) { create(:budget_group, budget: budget) }
|
||||
|
||||
it "returns the heading price if the heading provided is part of the budget" do
|
||||
@@ -113,8 +132,6 @@ describe Budget do
|
||||
end
|
||||
|
||||
describe "investments_orders" do
|
||||
let(:budget) { create(:budget) }
|
||||
|
||||
it "is random when accepting and reviewing" do
|
||||
budget.phase = 'accepting'
|
||||
expect(budget.investments_orders).to eq(['random'])
|
||||
@@ -136,5 +153,40 @@ describe Budget do
|
||||
expect(budget.investments_orders).to eq(['random', 'confidence_score'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#generate_phases" do
|
||||
let(:drafting_phase) { budget.phases.drafting }
|
||||
let(:accepting_phase) { budget.phases.accepting }
|
||||
let(:reviewing_phase) { budget.phases.reviewing }
|
||||
let(:selecting_phase) { budget.phases.selecting }
|
||||
let(:valuating_phase) { budget.phases.valuating }
|
||||
let(:publishing_prices_phase) { budget.phases.publishing_prices }
|
||||
let(:balloting_phase) { budget.phases.balloting }
|
||||
let(:reviewing_ballots_phase) { budget.phases.reviewing_ballots }
|
||||
let(:finished_phase) { budget.phases.finished }
|
||||
|
||||
it "generates all phases linked in correct order" do
|
||||
expect(budget.phases.count).to eq(Budget::Phase::PHASE_KINDS.count)
|
||||
|
||||
expect(drafting_phase.next_phase).to eq(accepting_phase)
|
||||
expect(accepting_phase.next_phase).to eq(reviewing_phase)
|
||||
expect(reviewing_phase.next_phase).to eq(selecting_phase)
|
||||
expect(selecting_phase.next_phase).to eq(valuating_phase)
|
||||
expect(valuating_phase.next_phase).to eq(publishing_prices_phase)
|
||||
expect(publishing_prices_phase.next_phase).to eq(balloting_phase)
|
||||
expect(balloting_phase.next_phase).to eq(reviewing_ballots_phase)
|
||||
expect(reviewing_ballots_phase.next_phase).to eq(finished_phase)
|
||||
expect(finished_phase.next_phase).to eq(nil)
|
||||
|
||||
expect(drafting_phase.prev_phase).to eq(nil)
|
||||
expect(accepting_phase.prev_phase).to eq(drafting_phase)
|
||||
expect(reviewing_phase.prev_phase).to eq(accepting_phase)
|
||||
expect(selecting_phase.prev_phase).to eq(reviewing_phase)
|
||||
expect(valuating_phase.prev_phase).to eq(selecting_phase)
|
||||
expect(publishing_prices_phase.prev_phase).to eq(valuating_phase)
|
||||
expect(balloting_phase.prev_phase).to eq(publishing_prices_phase)
|
||||
expect(reviewing_ballots_phase.prev_phase).to eq(balloting_phase)
|
||||
expect(finished_phase.prev_phase).to eq(reviewing_ballots_phase)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user