Commit e9b19a7a773bbcd57b4ce2a6e2a0a5d8928ddfe5

Authored by Larissa Reis
1 parent c5fe417b

Validates period range

plugins/custom_forms/lib/custom_forms_plugin/form.rb
@@ -10,6 +10,7 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord @@ -10,6 +10,7 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord
10 10
11 validates_presence_of :profile, :name 11 validates_presence_of :profile, :name
12 validates_uniqueness_of :slug, :scope => :profile_id 12 validates_uniqueness_of :slug, :scope => :profile_id
  13 + validate :period_range, :if => Proc.new { |f| f.begining.present? && f.ending.present? }
13 validate :access_format 14 validate :access_format
14 15
15 before_validation do |form| 16 before_validation do |form|
@@ -75,4 +76,8 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord @@ -75,4 +76,8 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord
75 end 76 end
76 end 77 end
77 end 78 end
  79 +
  80 + def period_range
  81 + errors.add(:base, _('The time range selected is invalid.')) if ending < begining
  82 + end
78 end 83 end
plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
@@ -53,6 +53,20 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase @@ -53,6 +53,20 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase
53 assert !form.errors.invalid?(:slug) 53 assert !form.errors.invalid?(:slug)
54 end 54 end
55 55
  56 + should 'validate the difference between ending and beginning is positive' do
  57 + profile = fast_create(Profile)
  58 + form = CustomFormsPlugin::Form.new(:profile => profile, :name => 'Free Software')
  59 +
  60 + form.begining = Time.now
  61 + form.ending = Time.now + 1.day
  62 + assert form.valid?
  63 + assert !form.errors.invalid?(:base)
  64 +
  65 + form.ending = Time.now - 2.day
  66 + assert !form.valid?
  67 + assert form.errors.invalid?(:base)
  68 + end
  69 +
56 should 'define form expiration' do 70 should 'define form expiration' do
57 form = CustomFormsPlugin::Form.new 71 form = CustomFormsPlugin::Form.new
58 assert !form.expired? 72 assert !form.expired?