From dd65f895978ae9db753d4a2e2a86c0715899352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Tue, 8 Mar 2016 16:24:34 +0100 Subject: [PATCH] adds validation for price fields --- .../spending_proposals_controller.rb | 19 +++++++++++++++++-- config/locales/en.yml | 1 + config/locales/es.yml | 1 + .../valuation/spending_proposals_spec.rb | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/controllers/valuation/spending_proposals_controller.rb b/app/controllers/valuation/spending_proposals_controller.rb index f5074ab1e..b0d83d96e 100644 --- a/app/controllers/valuation/spending_proposals_controller.rb +++ b/app/controllers/valuation/spending_proposals_controller.rb @@ -17,8 +17,11 @@ class Valuation::SpendingProposalsController < Valuation::BaseController end def valuate - @spending_proposal.update_attributes(valuation_params) - redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate') + if valid_price_params? && @spending_proposal.update(valuation_params) + redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate') + else + render action: :edit + end end private @@ -35,4 +38,16 @@ class Valuation::SpendingProposalsController < Valuation::BaseController raise ActionController::RoutingError.new('Not Found') unless current_user.administrator? || ValuationAssignment.exists?(spending_proposal_id: params[:id], valuator_id: current_user.valuator.id) end + def valid_price_params? + if /\D/.match params[:spending_proposal][:price] + @spending_proposal.errors.add(:price, I18n.t('spending_proposals.wrong_price_format')) + end + + if /\D/.match params[:spending_proposal][:price_first_year] + @spending_proposal.errors.add(:price_first_year, I18n.t('spending_proposals.wrong_price_format')) + end + + @spending_proposal.errors.empty? + end + end diff --git a/config/locales/en.yml b/config/locales/en.yml index e3f51219c..468889c28 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -423,6 +423,7 @@ en: recommendation_two: Any proposal or comment suggesting illegal action will be deleted. recommendations_title: How to create a spending proposal start_new: Create spending proposal + wrong_price_format: Only integer numbers stats: index: visits: Visits diff --git a/config/locales/es.yml b/config/locales/es.yml index 55177e15a..b9701cbf5 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -423,6 +423,7 @@ es: recommendation_two: Cualquier propuesta o comentario que implique acciones ilegales será eliminada. recommendations_title: Cómo crear una propuesta de gasto start_new: Crear una propuesta de gasto + wrong_price_format: Solo puede incluir caracteres numéricos stats: index: visits: Visitas diff --git a/spec/features/valuation/spending_proposals_spec.rb b/spec/features/valuation/spending_proposals_spec.rb index e5bde98d4..a5b761e65 100644 --- a/spec/features/valuation/spending_proposals_spec.rb +++ b/spec/features/valuation/spending_proposals_spec.rb @@ -284,6 +284,20 @@ feature 'Valuation spending proposals' do click_link @spending_proposal.title expect(page).to have_content('Valuation finished') end + + scenario 'Validates price formats' do + visit valuation_spending_proposals_path + within("#spending_proposal_#{@spending_proposal.id}") do + click_link "Edit" + end + + fill_in 'spending_proposal_price', with: '12345,98' + fill_in 'spending_proposal_price_first_year', with: '9876.6' + click_button 'Save changes' + + expect(page).to have_content('2 errors') + expect(page).to have_content('Only integer numbers', count: 2) + end end end