diff options
| -rw-r--r-- | app/models/clean_up.rb | 8 | ||||
| -rw-r--r-- | config/locales/clean_ups.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/clean_ups.fr.yml | 1 | ||||
| -rw-r--r-- | spec/models/clean_up_spec.rb | 12 | 
4 files changed, 22 insertions, 0 deletions
| diff --git a/app/models/clean_up.rb b/app/models/clean_up.rb index a51a98260..cdbf6c00a 100644 --- a/app/models/clean_up.rb +++ b/app/models/clean_up.rb @@ -7,9 +7,17 @@ class CleanUp < ActiveRecord::Base    enumerize :date_type, in: %i(between before after)    validates_presence_of :begin_date, message: :presence +  validates_presence_of :end_date, message: :presence, if: Proc.new {|cu| cu.date_type == 'between'}    validates_presence_of :date_type, message: :presence +  validate :end_date_must_be_greater_that_begin_date    after_commit :perform_cleanup, :on => :create +  def end_date_must_be_greater_that_begin_date +    if self.end_date && self.date_type == 'between' && self.begin_date >= self.end_date +      errors.add(:base, I18n.t('clean_ups.activerecord.errors.invalid_period')) +    end +  end +    def perform_cleanup      CleanUpWorker.perform_async(self.id)    end diff --git a/config/locales/clean_ups.en.yml b/config/locales/clean_ups.en.yml index 588eb55d5..a05750f6d 100644 --- a/config/locales/clean_ups.en.yml +++ b/config/locales/clean_ups.en.yml @@ -24,6 +24,7 @@ en:          end_date: "End date of clean up"    activerecord:      errors: +      invalid_period: "Invalid period : the end date must be strictly greater than the begin date"        models:          clean_up:            attributes: diff --git a/config/locales/clean_ups.fr.yml b/config/locales/clean_ups.fr.yml index 001c2b1cb..77e07591b 100644 --- a/config/locales/clean_ups.fr.yml +++ b/config/locales/clean_ups.fr.yml @@ -23,6 +23,7 @@ fr:          end_date: "Date de fin de la purge"    activerecord:      errors: +      invalid_period: "Période invalide : tLa date de fin doit être strictement supérieure à la date de début"        models:          clean_up:            attributes: diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index e03921582..2753c8718 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -6,6 +6,18 @@ RSpec.describe CleanUp, :type => :model do    it { should validate_presence_of(:begin_date).with_message(:presence) }    it { should belong_to(:referential) } +  context 'Clean Up With Date Type : Between' do +    subject(:cleaner) { create(:clean_up, date_type: :between) } +    it { should validate_presence_of(:end_date).with_message(:presence) } + +    it 'should have a end date strictly greater than the begin date' do +      expect(cleaner).to be_valid + +      cleaner.end_date = cleaner.begin_date +      expect(cleaner).not_to be_valid +    end +  end +    context '#exclude_dates_in_overlapping_period with :before date_type' do      let(:time_table) { create(:time_table) }      let(:period) { time_table.periods[0] } | 
