-
Notifications
You must be signed in to change notification settings - Fork 3
Add Opening hours to database #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class OpeningHour < ApplicationRecord | ||
| DAYS = { | ||
| lundi: 0, | ||
| mardi: 1, | ||
| mercredi: 2, | ||
| jeudi: 3, | ||
| vendredi: 4, | ||
| samedi: 5, | ||
| dimanche: 6 | ||
| }.freeze | ||
|
|
||
| DEFAULT_SCHEDULE = { | ||
| "lundi" => "Fermé", | ||
| "mardi" => "14:00 - 22:00", | ||
| "mercredi" => "14:00 - 22:00", | ||
| "jeudi" => "14:00 - 22:00", | ||
| "vendredi" => "14:00 - 22:00", | ||
| "samedi" => "14:00 - 22:00", | ||
| "dimanche" => "14:00 - 22:00" | ||
| }.freeze | ||
|
|
||
| belongs_to :updated_by_user, class_name: "User", optional: true | ||
|
|
||
| enum :day, DAYS | ||
|
|
||
| validates :day, presence: true, uniqueness: true | ||
| validates :open_at, presence: true, unless: :closed? | ||
| validates :close_at, presence: true, unless: :closed? | ||
| validate :close_after_open, unless: :closed? | ||
|
|
||
| scope :ordered, -> { order(:day) } | ||
|
|
||
| def self.schedule_hash | ||
| schedule = DEFAULT_SCHEDULE.dup | ||
|
|
||
| ordered.each do |record| | ||
| schedule[record.day.to_s] = record.formatted_range | ||
| end | ||
|
|
||
| schedule | ||
| end | ||
|
|
||
| def self.latest_update_entry | ||
| ordered.where.not(updated_at: nil).order(updated_at: :desc, id: :desc).first | ||
| end | ||
|
|
||
| def self.replace_schedule!(schedule_hash:, updated_by_user:) | ||
| transaction do | ||
| DAYS.each_key do |day_name| | ||
| raw_value = schedule_hash.fetch(day_name.to_s) { schedule_hash.fetch(day_name.to_sym, "Fermé") } | ||
| record = find_or_initialize_by(day: day_name) | ||
| record.updated_by_user = updated_by_user | ||
|
|
||
| if raw_value.to_s.strip.casecmp("fermé").zero? | ||
| record.closed = true | ||
| record.open_at = nil | ||
| record.close_at = nil | ||
| else | ||
| open_at, close_at = parse_range!(raw_value) | ||
| record.closed = false | ||
| record.open_at = open_at | ||
| record.close_at = close_at | ||
| end | ||
|
|
||
| record.save! | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def self.parse_range!(value) | ||
| match = value.to_s.strip.match(/\A(\d{2}:\d{2}) - (\d{2}:\d{2})\z/) | ||
| raise ArgumentError, "invalid time range" if match.nil? | ||
|
|
||
| [ parse_time!(match[1]), parse_time!(match[2]) ] | ||
| end | ||
|
|
||
| def self.parse_time!(value) | ||
| Time.zone.parse(value) || raise(ArgumentError, "invalid time") | ||
| end | ||
|
|
||
| def formatted_range | ||
| return "Fermé" if closed? | ||
|
|
||
| "#{open_at.strftime("%H:%M")} - #{close_at.strftime("%H:%M")}" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def close_after_open | ||
| return if open_at.blank? || close_at.blank? | ||
| return if close_at > open_at | ||
|
|
||
| errors.add(:close_at, "doit être après l'heure d'ouverture") | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PersistOpeningHoursSchedule < ActiveRecord::Migration[8.1] | ||
| DAY_MAP = { | ||
| "lundi" => 0, | ||
| "mardi" => 1, | ||
| "mercredi" => 2, | ||
| "jeudi" => 3, | ||
| "vendredi" => 4, | ||
| "samedi" => 5, | ||
| "dimanche" => 6 | ||
| }.freeze | ||
|
|
||
| DEFAULT_SCHEDULE = { | ||
| "lundi" => "Fermé", | ||
| "mardi" => "14:00 - 22:00", | ||
| "mercredi" => "14:00 - 22:00", | ||
| "jeudi" => "14:00 - 22:00", | ||
| "vendredi" => "14:00 - 22:00", | ||
| "samedi" => "14:00 - 22:00", | ||
| "dimanche" => "14:00 - 22:00" | ||
| }.freeze | ||
|
|
||
| def up | ||
| add_column :opening_hours, :closed, :boolean, default: false, null: false | ||
| add_reference :opening_hours, :updated_by_user, foreign_key: { to_table: :users } | ||
| change_column_null :opening_hours, :open_at, true | ||
| change_column_null :opening_hours, :close_at, true | ||
|
|
||
| say_with_time "Deduplicating opening hours by day" do | ||
| OpeningHourRecord.order(:day, updated_at: :desc, id: :desc).group_by(&:day).each_value do |rows| | ||
| rows.drop(1).each(&:destroy!) | ||
| end | ||
| end | ||
|
|
||
| say_with_time "Ensuring one opening hour row per weekday" do | ||
| DEFAULT_SCHEDULE.each do |day_name, value| | ||
| day_value = DAY_MAP.fetch(day_name) | ||
| record = OpeningHourRecord.find_or_initialize_by(day: day_value) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this migration runs on a database that already has Useful? React with 👍 / 👎. |
||
|
|
||
| if value == "Fermé" | ||
| record.closed = true | ||
| record.open_at = nil | ||
| record.close_at = nil | ||
| else | ||
| open_at, close_at = value.split(" - ") | ||
| record.closed = false | ||
| record.open_at = open_at | ||
| record.close_at = close_at | ||
| end | ||
|
|
||
| record.save! | ||
| end | ||
| end | ||
|
|
||
| add_index :opening_hours, :day, unique: true | ||
| end | ||
|
|
||
| def down | ||
| remove_index :opening_hours, :day | ||
| change_column_null :opening_hours, :open_at, false | ||
| change_column_null :opening_hours, :close_at, false | ||
|
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Rolling back immediately after this migration will fail because Useful? React with 👍 / 👎. |
||
| remove_reference :opening_hours, :updated_by_user, foreign_key: { to_table: :users } | ||
| remove_column :opening_hours, :closed | ||
| end | ||
|
|
||
| class OpeningHourRecord < ApplicationRecord | ||
| self.table_name = "opening_hours" | ||
| end | ||
| end | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| FactoryBot.define do | ||
| factory :opening_hour do | ||
| day { :mardi } | ||
| open_at { "14:00" } | ||
| close_at { "22:00" } | ||
| closed { false } | ||
| association :updated_by_user, factory: :user | ||
|
|
||
| trait :closed do | ||
| closed { true } | ||
| open_at { nil } | ||
| close_at { nil } | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
OpeningHour.replace_schedule!raises anArgumentErrorhere, for example from a crafted time parameter likeopen_hour_lundi=100that produces an invalid range, this rescue path raises a newNoMethodErrorbecauseArgumentErrordoes not respond torecord. That turns the intended validation response into a 500 instead of rendering the edit form withe.message.Useful? React with 👍 / 👎.