diff --git a/app/models/calendar.rb b/app/models/calendar.rb index dca9048ec..c3ed16db0 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -8,11 +8,14 @@ class Calendar < ApplicationModel store :business_hours store :public_holidays - before_create :validate_public_holidays, :validate_hours, :fetch_ical - after_create :sync_default, :min_one_check - before_update :validate_public_holidays, :validate_hours, :fetch_ical - after_update :sync_default, :min_one_check - after_destroy :min_one_check + validate :validate_hours + + before_save :ensure_public_holidays_details, :fetch_ical + + after_destroy :min_one_check + after_save :min_one_check + + after_save :sync_default =begin @@ -391,8 +394,8 @@ returns true end - # validate format of public holidays - def validate_public_holidays + # ensure integrity of details of public holidays + def ensure_public_holidays_details # fillup feed info before = public_holidays_was @@ -414,7 +417,13 @@ returns # get business hours hours = business_hours_to_hash - raise Exceptions::UnprocessableEntity, 'No configured business hours found!' if hours.blank? + + if hours.blank? + errors.add :base, 'No configured business hours found!' + return + end + + #raise Exceptions::UnprocessableEntity, 'No configured business hours found!' if hours.blank? # validate if business hours are usable by execute a try calculation begin @@ -423,10 +432,8 @@ returns end Biz.time(10, :minutes).after(Time.zone.parse('Tue, 05 Feb 2019 21:40:28 UTC +00:00')) rescue => e - raise Exceptions::UnprocessableEntity, e.message + errors.add :base, e.message end - - true end end diff --git a/db/migrate/20210617051913_issue_3618_google_calendar_url_https.rb b/db/migrate/20210617051913_issue_3618_google_calendar_url_https.rb index 2d53227ed..b1e472a8a 100644 --- a/db/migrate/20210617051913_issue_3618_google_calendar_url_https.rb +++ b/db/migrate/20210617051913_issue_3618_google_calendar_url_https.rb @@ -7,8 +7,10 @@ class Issue3618GoogleCalendarUrlHttps < ActiveRecord::Migration[5.2] Calendar .where('ical_url LIKE ?', 'http://www.google.com/calendar/ical/%') .each do |calendar| - calendar.ical_url.sub!(%r{^http://}, 'https://') - calendar.save + new_url = calendar.ical_url.sub(%r{^http://}, 'https://') + # skipping validation allows to update old misconfigured calendar + # https://github.com/zammad/zammad/issues/3641 + calendar.update_attribute :ical_url, new_url # rubocop:disable Rails/SkipsModelValidations end end end diff --git a/spec/db/migrate/issue_3618_google_calendar_url_https_spec.rb b/spec/db/migrate/issue_3618_google_calendar_url_https_spec.rb index 05f526855..d73391c6b 100644 --- a/spec/db/migrate/issue_3618_google_calendar_url_https_spec.rb +++ b/spec/db/migrate/issue_3618_google_calendar_url_https_spec.rb @@ -12,4 +12,13 @@ RSpec.describe Issue3618GoogleCalendarUrlHttps, type: :db_migration, db_strategy .from(false) .to(true) end + + it 'other' do + calendar.update_attribute(:business_hours, nil) + + expect { migrate } + .to change { calendar.reload.ical_url.starts_with? 'https://' } + .from(false) + .to(true) + end end diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 418c6c95d..f2f66cab8 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -211,14 +211,14 @@ RSpec.describe Calendar, type: :model do timeframes: [['09:00', '00:00']] } }) - end.to raise_error(Exceptions::UnprocessableEntity, 'nonsensical hours provided') + end.to raise_error(ActiveRecord::RecordInvalid, %r{nonsensical hours provided}) end it 'fails for blank structure' do expect do create(:calendar, business_hours: {}) - end.to raise_error(Exceptions::UnprocessableEntity, 'No configured business hours found!') + end.to raise_error(ActiveRecord::RecordInvalid, %r{No configured business hours found!}) end end end