Skip to content
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

Utiliser 2 selects (heures et minutes) sur le formulaire de plage d'ouverture #4971

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ gem "rubyzip" # zip export files
# Recurring events in Ruby
gem "montrose"
# Supplies TimeOfDay and Shift class
gem "tod", "~> 2.2"
gem "tod"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

# A ruby implementation of the iCalendar specification (RFC-5545).
gem "icalendar", "~> 2.5"

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ GEM
thread_safe (0.3.6)
tilt (2.5.0)
timeout (0.4.1)
tod (2.2.0)
tod (3.1.2)
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
Expand Down Expand Up @@ -816,7 +816,7 @@ DEPENDENCIES
sprockets-rails
stackprof
strong_migrations
tod (~> 2.2)
tod
turbolinks (~> 5)
typhoeus
wannabe_bool
Expand Down
13 changes: 0 additions & 13 deletions app/helpers/plage_ouvertures_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
module PlageOuverturesHelper
# Generates ["00:00", "00:05", "00:10", ... "23:50", "23:55"]
EVERY_5_MINUTES_OF_THE_DAY = (0..23).flat_map do |h|
padded_h = format("%02i", h)
(0..55).step(5).map do |m|
padded_min = format("%02i", m)
"#{padded_h}:#{padded_min}"
end
end.freeze

def every_5_minutes_of_the_day
EVERY_5_MINUTES_OF_THE_DAY
end

Comment on lines -2 to -14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette logique est fournie par Rails à travers le paramètre minute_step ! 👯

def display_recurrence(plage_ouverture)
every_part = display_every(plage_ouverture)

Expand Down
12 changes: 6 additions & 6 deletions app/models/concerns/recurrence_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module RecurrenceConcern

included do
serialize :recurrence, coder: Montrose::Recurrence
serialize :start_time, coder: Tod::TimeOfDay
serialize :end_time, coder: Tod::TimeOfDay
attribute :start_time, :time_only # uses Tod::TimeOfDayType
attribute :end_time, :time_only # uses Tod::TimeOfDayType

before_save :clear_empty_recurrence, :set_recurrence_ends_at

Expand All @@ -22,8 +22,8 @@ module RecurrenceConcern
class_methods do
def serialize_for_active_job(record)
manually_serialized_attrs = {
start_time: Tod::TimeOfDay.dump(record.start_time),
end_time: Tod::TimeOfDay.dump(record.end_time),
start_time: record.start_time.to_s,
end_time: record.end_time.to_s,
Comment on lines -25 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette sérialisation manuelle a été ajoutée dans #4625, à contre cœur, lorsque nous avons constaté que les serializers custom ActiveJob ne fonctionnaient pas en prod (alors qu'ils fonctionnent en dev et test 😬 )

recurrence: Montrose::Recurrence.dump(record.recurrence),
}
record.attributes.merge(manually_serialized_attrs.stringify_keys)
Expand All @@ -32,8 +32,8 @@ def serialize_for_active_job(record)
def deserialize_for_active_job(hash)
hash = hash.symbolize_keys
manually_deserialized_attrs = {
start_time: Tod::TimeOfDay.load(hash[:start_time]),
end_time: Tod::TimeOfDay.load(hash[:end_time]),
start_time: Tod::TimeOfDay.parse(hash[:start_time]),
end_time: Tod::TimeOfDay.parse(hash[:end_time]),
recurrence: Montrose::Recurrence.load(hash[:recurrence]),
}
new(hash.merge(manually_deserialized_attrs))
Expand Down
4 changes: 2 additions & 2 deletions app/views/common/_recurrence.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
= date_input(f, :end_day)

.row
.col = f.input :start_time, as: :select, collection: every_5_minutes_of_the_day, selected: model.start_time&.strftime("%H:%M"), include_blank: false
.col = f.input :end_time, as: :select, collection: every_5_minutes_of_the_day, selected: model.end_time&.strftime("%H:%M"), include_blank: false
.col = f.input :start_time, as: :time, ignore_date: true, minute_step: 5
.col = f.input :end_time, as: :time, ignore_date: true, minute_step: 5

= f.input :recurrence, as: :hidden, input_html: { value: model.recurrence ? model.recurrence.as_json.to_json : "", "data-target": "recurrence.recurrenceComputed", class: "js-recurrence-computed" }

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/absences_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
end

describe "DELETE #destroy" do
let!(:absence) { create(:absence, agent_id: agent.id) }
let!(:absence) { create(:absence, :once_a_week, agent_id: agent.id) }

it "destroys the requested absence" do
expect do
Expand Down
18 changes: 18 additions & 0 deletions spec/features/agents/agent_can_crud_plage_ouvertures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,22 @@
expect(PlageOuverture.last.motifs).to contain_exactly(motif_1_service_avocat, motif_2_service_avocat)
end
end

describe "selecting a time range" do
it "works" do
visit new_admin_organisation_agent_plage_ouverture_path(organisation, agent)
check motif.name
select(lieu.full_name, from: "plage_ouverture_lieu_id")

# Set start time at 10:30
select "10", from: "plage_ouverture_start_time_4i"
select "30", from: "plage_ouverture_start_time_5i"
# Set start time at 13:45
select "13", from: "plage_ouverture_end_time_4i"
select "45", from: "plage_ouverture_end_time_5i"

expect { click_on "Créer la plage d'ouverture" }.to change(PlageOuverture, :count).by(1)
expect(PlageOuverture.last).to have_attributes(start_time: Tod::TimeOfDay.new(10, 30), end_time: Tod::TimeOfDay.new(13, 45))
end
end
end
10 changes: 0 additions & 10 deletions spec/helpers/plage_ouvertures_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
travel_to(now)
end

describe "#every_5_minutes_of_the_day" do
it "return 288 entries for all times of the day by 5 minutes increment" do
expect(every_5_minutes_of_the_day.count).to eq(288)
expect(every_5_minutes_of_the_day.first).to eq("00:00")
expect(every_5_minutes_of_the_day.last).to eq("23:55")
expect(every_5_minutes_of_the_day[12 * 18]).to eq("18:00")
expect(every_5_minutes_of_the_day[(12 * 18) + 5]).to eq("18:25")
end
end

describe "#display_recurrence" do
it "with a weekly recurrence" do
plage_ouverture = build(:plage_ouverture, recurrence: Montrose.every(:week))
Expand Down
Loading