From e73baa2754fb02c807afa3457ce08b141d9029a6 Mon Sep 17 00:00:00 2001 From: hitman Date: Tue, 21 Mar 2017 16:46:09 +0530 Subject: [PATCH] add validation for name, used and quantity --- app/helpers/application_helper.rb | 1 + app/models/resource.rb | 8 +++++--- app/views/admin/resources/_form.html.haml | 2 +- spec/models/resource_spec.rb | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5bb129ae..0d281c18 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -578,6 +578,7 @@ module ApplicationHelper end def quantity_left_of(resource) + return '-/-' if resource.quantity.blank? "#{resource.quantity - resource.used}/#{resource.quantity}" end diff --git a/app/models/resource.rb b/app/models/resource.rb index c113522e..ce6e7b94 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -1,10 +1,12 @@ class Resource < ActiveRecord::Base belongs_to :conference - validate :used_less_than_quantity + validates :name, :used, :quantity, presence: true + validates :used, :quantity, numericality: { greater_than_or_equal_to: 0, only_integer: true } + validate :used_no_more_than_quantity private - def used_less_than_quantity - errors.add(:used, 'can not be higher than total quantity') unless used <= quantity + def used_no_more_than_quantity + errors.add(:used, 'cannot be higher than total quantity') if used.present? && quantity.present? && used > quantity end end diff --git a/app/views/admin/resources/_form.html.haml b/app/views/admin/resources/_form.html.haml index 818bfc27..ef999104 100644 --- a/app/views/admin/resources/_form.html.haml +++ b/app/views/admin/resources/_form.html.haml @@ -9,7 +9,7 @@ .col-md-8 = semantic_form_for(@resource, :url => (@resource.new_record? ? admin_conference_resources_path : admin_conference_resource_path(@conference.short_title, @resource))) do |f| = f.input :name - = f.input :description, input_html: { rows: 5, data: { provide: "markdown-editable" } } + = f.input :description, input_html: { rows: 5, data: { provide: 'markdown-editable' } } = f.input :used = f.input :quantity %p.text-right diff --git a/spec/models/resource_spec.rb b/spec/models/resource_spec.rb index 51406f62..03a6a502 100644 --- a/spec/models/resource_spec.rb +++ b/spec/models/resource_spec.rb @@ -4,6 +4,24 @@ describe Resource do let(:conference) { create(:conference) } let(:resource) { create :resource } + it { is_expected.to validate_presence_of(:name) } + + it { is_expected.to validate_presence_of(:used) } + + it { is_expected.to validate_presence_of(:quantity) } + + it { is_expected.to validate_numericality_of(:used) } + + it { is_expected.to validate_numericality_of(:quantity) } + + it { is_expected.not_to allow_value(-1).for(:used) } + + it { is_expected.to allow_value(0).for(:used) } + + it { is_expected.not_to allow_value(-1).for(:quantity) } + + it { is_expected.to allow_value(0).for(:quantity) } + it 'has a valid factory' do expect(build(:resource)).to be_valid end