Fix: add option to choose 24 hour working slot (#4018)

* Add option to choose 24 hour working slot

* fix spec

* add check to update open hour and close hour for open all day

* update 24 hour working slot change in widget side

* add validation to check open_all_day and closed_all_day true at the same time
This commit is contained in:
Aswin Dev P.S
2022-02-22 01:28:49 -08:00
committed by GitHub
parent 7ba24b90c4
commit e348db1e37
10 changed files with 151 additions and 19 deletions

View File

@@ -3,7 +3,7 @@
module OutOfOffisable
extend ActiveSupport::Concern
OFFISABLE_ATTRS = %w[day_of_week closed_all_day open_hour open_minutes close_hour close_minutes].freeze
OFFISABLE_ATTRS = %w[day_of_week closed_all_day open_hour open_minutes close_hour close_minutes open_all_day].freeze
included do
has_many :working_hours, dependent: :destroy_async
@@ -29,7 +29,8 @@ module OutOfOffisable
# "open_hour"=>9,
# "open_minutes"=>0,
# "close_hour"=>17,
# "close_minutes"=>0},...]
# "close_minutes"=>0,
# "open_all_day=>false" },...]
def update_working_hours(params)
ActiveRecord::Base.transaction do
params.each do |working_hour|
@@ -41,12 +42,12 @@ module OutOfOffisable
private
def create_default_working_hours
working_hours.create!(day_of_week: 0, closed_all_day: true)
working_hours.create!(day_of_week: 1, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
working_hours.create!(day_of_week: 2, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
working_hours.create!(day_of_week: 3, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
working_hours.create!(day_of_week: 4, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
working_hours.create!(day_of_week: 5, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
working_hours.create!(day_of_week: 6, closed_all_day: true)
working_hours.create!(day_of_week: 0, closed_all_day: true, open_all_day: false)
working_hours.create!(day_of_week: 1, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0, open_all_day: false)
working_hours.create!(day_of_week: 2, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0, open_all_day: false)
working_hours.create!(day_of_week: 3, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0, open_all_day: false)
working_hours.create!(day_of_week: 4, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0, open_all_day: false)
working_hours.create!(day_of_week: 5, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0, open_all_day: false)
working_hours.create!(day_of_week: 6, closed_all_day: true, open_all_day: false)
end
end

View File

@@ -7,6 +7,7 @@
# close_minutes :integer
# closed_all_day :boolean default(FALSE)
# day_of_week :integer not null
# open_all_day :boolean default(FALSE)
# open_hour :integer
# open_minutes :integer
# created_at :datetime not null
@@ -22,6 +23,7 @@
class WorkingHour < ApplicationRecord
belongs_to :inbox
before_validation :ensure_open_all_day_hours
before_save :assign_account
validates :open_hour, presence: true, unless: :closed_all_day?
@@ -35,6 +37,7 @@ class WorkingHour < ApplicationRecord
validates :close_minutes, inclusion: 0..59, unless: :closed_all_day?
validate :close_after_open, unless: :closed_all_day?
validate :open_all_day_and_closed_all_day
def self.today
find_by(day_of_week: Date.current.wday)
@@ -69,4 +72,19 @@ class WorkingHour < ApplicationRecord
errors.add(:close_hour, 'Closing time cannot be before opening time')
end
def ensure_open_all_day_hours
return unless open_all_day?
self.open_hour = 0
self.open_minutes = 0
self.close_hour = 23
self.close_minutes = 59
end
def open_all_day_and_closed_all_day
return unless open_all_day? && closed_all_day?
errors.add(:base, 'open_all_day and closed_all_day cannot be true at the same time')
end
end