diff options
| author | Vlatka Pavisic | 2017-01-02 11:52:46 +0100 |
|---|---|---|
| committer | Vlatka Pavisic | 2017-01-02 11:52:46 +0100 |
| commit | 7657a10c7c4b126cdaabb779657e9a5270fc729d (patch) | |
| tree | 18e7b56d837f0c70a47345b45e32d01b08dfdf33 | |
| parent | ad71715b42f5278fb46cedec874cb7e9a4fe3cf3 (diff) | |
| download | chouette-core-7657a10c7c4b126cdaabb779657e9a5270fc729d.tar.bz2 | |
Refs #2262 : Calendar#dates uniqueness
| -rw-r--r-- | app/models/calendar.rb | 6 | ||||
| -rw-r--r-- | app/views/calendars/_form.html.slim | 4 | ||||
| -rw-r--r-- | config/locales/calendars.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/calendars.fr.yml | 4 | ||||
| -rw-r--r-- | spec/models/calendar_spec.rb | 9 |
5 files changed, 18 insertions, 6 deletions
diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 40af2d0e3..fc339caad 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -3,7 +3,7 @@ class Calendar < ActiveRecord::Base validates_presence_of :name, :short_name, :organisation validates_uniqueness_of :short_name - validate :date_not_in_date_ranges + validate :date_not_in_date_ranges, :dates_uniqueness after_initialize :init_dates_and_date_ranges @@ -27,6 +27,10 @@ class Calendar < ActiveRecord::Base overlap end + def dates_uniqueness + errors.add(:dates, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')) if dates && dates.length > dates.uniq.length + end + class Period include ActiveAttr::Model diff --git a/app/views/calendars/_form.html.slim b/app/views/calendars/_form.html.slim index 2d36d7d9e..ea2e0b30b 100644 --- a/app/views/calendars/_form.html.slim +++ b/app/views/calendars/_form.html.slim @@ -9,8 +9,8 @@ / = render 'date_fields', f: date / .links / = link_to_add_association '+', f, :dates - = f.label :date_ranges - = f.simple_fields_for :periods do |period| + /= f.label :date_ranges + /= f.simple_fields_for :periods do |period| = render 'period_fields', f: period .links /= link_to_add_association '+', f, :periods diff --git a/config/locales/calendars.en.yml b/config/locales/calendars.en.yml index 87ffef141..24acb7680 100644 --- a/config/locales/calendars.en.yml +++ b/config/locales/calendars.en.yml @@ -24,6 +24,7 @@ en: attributes: dates: date_in_date_ranges: A date can not be in Dates and in Date ranges. + date_in_dates: A date can appear only once in the list of dates. index: title: Calendars all: All diff --git a/config/locales/calendars.fr.yml b/config/locales/calendars.fr.yml index 7a6fd541f..358ae0d46 100644 --- a/config/locales/calendars.fr.yml +++ b/config/locales/calendars.fr.yml @@ -47,6 +47,6 @@ fr: calendar: attributes: dates: - date_in_date_ranges: 'Une même date ne peut pas être à la fois dans Dates et dans Intervalles de dates.' - + date_in_date_ranges: Une même date ne peut pas être incluse à la fois dans la liste et dans les intervalles de dates. + date_in_dates: Une même date ne peut pas être incluse plusieurs fois dans la liste. diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 6cc256582..2557cdb93 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -17,6 +17,14 @@ RSpec.describe Calendar, :type => :model do }.to raise_error(ActiveRecord::RecordInvalid) expect(calendar.errors.messages[:dates]).to eq([I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_date_ranges')]) end + + it 'validates that there are no duplicates in dates' do + calendar = build(:calendar, dates: [Date.yesterday, Date.yesterday], date_ranges: [Date.today..Date.tomorrow]) + expect { + calendar.save! + }.to raise_error(ActiveRecord::RecordInvalid) + expect(calendar.errors.messages[:dates]).to eq([I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')]) + end end describe 'Period' do @@ -86,7 +94,6 @@ RSpec.describe Calendar, :type => :model do end end - end describe 'before_validation' do |
