Skip to content

Commit

Permalink
Merge pull request #7071 from fjordllc/bug/skip_regular_events_on_hol…
Browse files Browse the repository at this point in the history
…idays

定期イベントの次回開催日が祝日を考慮するように修正
  • Loading branch information
komagata authored Dec 14, 2023
2 parents 0670e4e + 1148b7e commit 5a71f97
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/decorators/regular_event_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def next_holding_date
if finished
'開催終了'
elsif holding_today?
'本日開催'
!hold_national_holiday && HolidayJp.holiday?(Time.zone.today) ? "次回の開催日は #{l next_event_date} です" : '本日開催'
else
"次回の開催日は #{l next_event_date} です"
end
Expand Down
28 changes: 20 additions & 8 deletions app/models/regular_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,31 @@ def next_event_date
end

def possible_next_event_date(first_day, repeat_rule)
if repeat_rule.frequency.zero?
next_specific_day_of_the_week(repeat_rule) if Time.zone.today.mon == first_day.mon
else
# 次の第n X曜日の日付を計算する
date = (repeat_rule.frequency - 1) * DAYS_OF_THE_WEEK_COUNT + repeat_rule.day_of_the_week - first_day.wday + 1
date += DAYS_OF_THE_WEEK_COUNT if repeat_rule.day_of_the_week < first_day.wday
Date.new(first_day.year, first_day.mon, date)
return next_specific_day_of_the_week(repeat_rule) if repeat_rule.frequency.zero?

possible_date = calculate_date_of_specific_nth_day_of_the_week(repeat_rule, first_day, DAYS_OF_THE_WEEK_COUNT)

return possible_date if hold_national_holiday

while possible_date.mon == first_day.mon && HolidayJp.holiday?(possible_date)
first_day = first_day.next_month
possible_date = calculate_date_of_specific_nth_day_of_the_week(repeat_rule, first_day, DAYS_OF_THE_WEEK_COUNT)
end
possible_date
end

def next_specific_day_of_the_week(repeat_rule)
day_of_the_week_symbol = DateAndTime::Calculations::DAYS_INTO_WEEK.key(repeat_rule.day_of_the_week)
0.days.ago.next_occurring(day_of_the_week_symbol).to_date
possible_date = 0.days.ago.next_occurring(day_of_the_week_symbol).to_date
possible_date = possible_date.next_occurring(day_of_the_week_symbol) while !hold_national_holiday && HolidayJp.holiday?(possible_date)
possible_date
end

def calculate_date_of_specific_nth_day_of_the_week(repeat_rule, first_day, days_of_the_week_count)
# 次の第n X曜日の日付を計算する
specific_date = (repeat_rule.frequency - 1) * days_of_the_week_count + repeat_rule.day_of_the_week - first_day.wday + 1
specific_date += days_of_the_week_count if repeat_rule.day_of_the_week < first_day.wday
Date.new(first_day.year, first_day.mon, specific_date)
end

def holding_tomorrow?
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/regular_event_repeat_rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ regular_event_repeat_rule34:
frequency: 0
day_of_the_week: 1
regular_event: regular_event32

regular_event_repeat_rule35:
frequency: 0
day_of_the_week: 3
regular_event: regular_event33

regular_event_repeat_rule36:
frequency: 1
day_of_the_week: 3
regular_event: regular_event34
25 changes: 25 additions & 0 deletions test/models/regular_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class RegularEventTest < ActiveSupport::TestCase
first_day = Time.zone.today
assert_equal Date.new(2022, 6, 5), regular_event.possible_next_event_date(first_day, regular_event_repeat_rule)
end

holiday_not_held_event = regular_events(:regular_event1)
repeat_rule = regular_event_repeat_rules(:regular_event_repeat_rule36) # 第1週水曜日
travel_to Time.zone.local(2020, 1, 1, 0, 0, 0) do
first_day = Time.zone.today
assert_equal Date.new(2020, 2, 5), holiday_not_held_event.possible_next_event_date(first_day, repeat_rule)
end
end

test '#next_specific_day_of_the_week' do
Expand All @@ -56,6 +63,24 @@ class RegularEventTest < ActiveSupport::TestCase
travel_to Time.zone.local(2022, 6, 1, 0, 0, 0) do
assert_equal Date.new(2022, 6, 5), regular_event.next_specific_day_of_the_week(regular_event_repeat_rule)
end

holiday_not_held_event = regular_events(:regular_event1)
repeat_rule = regular_event_repeat_rules(:regular_event_repeat_rule35) # 毎週水曜日
travel_to Time.zone.local(2020, 1, 1, 0, 0, 0) do
first_day = Time.zone.today
assert_equal Date.new(2020, 1, 8), holiday_not_held_event.possible_next_event_date(first_day, repeat_rule)
end
end

test '#calculate_date_of_specific_nth_day_of_the_week' do
regular_event = regular_events(:regular_event1)
repeat_rule = regular_event_repeat_rules(:regular_event_repeat_rule2)
days_of_the_week_count = 7

travel_to Time.zone.local(2020, 1, 1, 0, 0, 0) do
first_day = Time.zone.today
assert_equal Date.new(2020, 1, 6), regular_event.calculate_date_of_specific_nth_day_of_the_week(repeat_rule, first_day, days_of_the_week_count)
end
end

test '#holding_tomorrow?' do
Expand Down

0 comments on commit 5a71f97

Please sign in to comment.