Add Budget::Phase model, spec and factory

Create a new Budget::Phase model that:
* Stablishes a relation with its budget
* Stablishes relation with two other Budget::Phases (previous and next)
* Validates basic dates range, kind and description rules.
* Adds scopes to get the ones enabled as well as each individual phase

Create a factory that generates a basic and valid Budget::Phase

Create a model spec that checks kind, date range and budget validations.
This commit is contained in:
Bertocq
2018-01-15 19:38:39 +01:00
parent 82d67258e8
commit 36e74d0ef2
5 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
class Budget
class Phase < ActiveRecord::Base
belongs_to :budget
belongs_to :next_phase, class_name: 'Budget::Phase', foreign_key: :next_phase_id
has_one :prev_phase, class_name: 'Budget::Phase', foreign_key: :next_phase_id
validates :budget, presence: true
validates :kind, presence: true, uniqueness: { scope: :budget }, inclusion: { in: Budget::PHASES }
validates :description, length: { maximum: Budget.description_max_length }
validate :dates_range_valid?
scope :enabled, -> { where(enabled: true) }
scope :drafting, -> { find_by_kind('drafting') }
scope :accepting, -> { find_by_kind('accepting')}
scope :reviewing, -> { find_by_kind('reviewing')}
scope :selecting, -> { find_by_kind('selecting')}
scope :valuating, -> { find_by_kind('valuating')}
scope :publishing_prices, -> { find_by_kind('publishing_prices')}
scope :balloting, -> { find_by_kind('balloting')}
scope :reviewing_ballots, -> { find_by_kind('reviewing_ballots')}
scope :finished, -> { find_by_kind('finished')}
def dates_range_valid?
if starts_at.present? && ends_at.present? && starts_at >= ends_at
errors.add(:starts_at, I18n.t('budgets.phases.errors.dates_range_invalid'))
end
end
end
end

View File

@@ -158,3 +158,6 @@ en:
accepted: "Accepted spending proposal: "
discarded: "Discarded spending proposal: "
incompatibles: Incompatibles
phases:
errors:
dates_range_invalid: "Start date can't be equal or later than End date"

View File

@@ -158,3 +158,6 @@ es:
accepted: 'Propuesta de inversión aceptada: '
discarded: 'Propuesta de inversión descartada: '
incompatibles: Incompatibles
phases:
errors:
dates_range_invalid: "La fecha de comienzo no puede ser igual o superior a la de finalización"

View File

@@ -341,7 +341,16 @@ FactoryBot.define do
feasibility "feasible"
valuation_finished true
end
end
factory :budget_phase, class: 'Budget::Phase' do
budget
kind :balloting
summary Faker::Lorem.sentence(3)
description Faker::Lorem.sentence(10)
starts_at Date.yesterday
ends_at Date.tomorrow
enabled true
end
factory :image do

View File

@@ -0,0 +1,44 @@
require 'rails_helper'
describe Budget::Phase do
let(:budget) { create(:budget) }
describe "validates" do
it "is not valid without a budget" do
expect(build(:budget_phase, budget: nil)).not_to be_valid
end
describe "kind validations" do
it "is not valid without a kind" do
expect(build(:budget_phase, kind: nil)).not_to be_valid
end
it "is not valid with a kind not in valid budget phases" do
expect(build(:budget_phase, kind: 'invalid_phase_kind')).not_to be_valid
end
it "is not valid with the same kind as another budget's phase" do
create(:budget_phase, budget: budget)
expect(build(:budget_phase, budget: budget)).not_to be_valid
end
end
describe "#dates_range_valid?" do
it "is valid when start & end dates are different & consecutive" do
expect(build(:budget_phase, starts_at: Date.today, ends_at: Date.tomorrow)).to be_valid
end
it "is not valid when dates are equal" do
expect(build(:budget_phase, starts_at: Date.today, ends_at: Date.today)).not_to be_valid
end
it "is not valid when start date is later than end date" do
expect(build(:budget_phase, starts_at: Date.tomorrow, ends_at: Date.today)).not_to be_valid
end
end
end
end