From 3c2ab9fb056720adf056abb824db154d3130996b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 27 Mar 2017 13:14:26 +0200 Subject: [PATCH] adds validation for price when investment is feasible --- app/models/budget/investment.rb | 5 +++++ spec/models/budget/investment_spec.rb | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 99e0eb25a..4bfd038c3 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -25,6 +25,7 @@ class Budget validates :description, presence: true validates :heading_id, presence: true validates_presence_of :unfeasibility_explanation, if: :unfeasibility_explanation_required? + validates_presence_of :price, if: :price_required? validates :title, length: { in: 4..Budget::Investment.title_max_length } validates :description, length: { maximum: Budget::Investment.description_max_length } @@ -136,6 +137,10 @@ class Budget unfeasible? && valuation_finished? end + def price_required? + feasible? && valuation_finished? + end + def unfeasible_email_pending? unfeasible_email_sent_at.blank? && unfeasible? && valuation_finished? end diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 3ab97c574..fa51f3cab 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -54,7 +54,7 @@ describe Budget::Investment do expect(investment.group_id).to eq group_2.id end - describe "#unfeasibility_explanation" do + describe "#unfeasibility_explanation blank" do it "should be valid if valuation not finished" do investment.unfeasibility_explanation = "" investment.valuation_finished = false @@ -76,6 +76,29 @@ describe Budget::Investment do end end + describe "#price blank" do + it "should be valid if valuation not finished" do + investment.price = "" + investment.valuation_finished = false + expect(investment).to be_valid + end + + it "should be valid if valuation finished and unfeasible" do + investment.price = "" + investment.unfeasibility_explanation = "reason" + investment.feasibility = "unfeasible" + investment.valuation_finished = true + expect(investment).to be_valid + end + + it "should not be valid if valuation finished and feasible" do + investment.price = "" + investment.feasibility = "feasible" + investment.valuation_finished = true + expect(investment).to_not be_valid + end + end + describe "#code" do let(:investment) { create(:budget_investment) }