summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/concerns/rrule_humanizer.rb23
-rw-r--r--test/models/concerns/rrule_humanizer_test.rb8
2 files changed, 23 insertions, 8 deletions
diff --git a/app/models/concerns/rrule_humanizer.rb b/app/models/concerns/rrule_humanizer.rb
index ca29648..07141c1 100644
--- a/app/models/concerns/rrule_humanizer.rb
+++ b/app/models/concerns/rrule_humanizer.rb
@@ -46,19 +46,30 @@ module RruleHumanizer
46 46
47 excluded_monthly_ordinal = nil 47 excluded_monthly_ordinal = nil
48 excluded_monthly_weekday = nil 48 excluded_monthly_weekday = nil
49 selected_monthly_ordinals = nil
50 selected_monthly_weekday = nil
49 if freq == "MONTHLY" && byday_values.present? 51 if freq == "MONTHLY" && byday_values.present?
50 ordinal_days = byday_values.map { |d| d.match(/^([1-5])([A-Z]{2})$/) } 52 ordinal_days = byday_values.map { |d| d.match(/^([1-5])([A-Z]{2})$/) }
51 if ordinal_days.all? 53 if ordinal_days.all?
52 positions = ordinal_days.map { |match| match[1].to_i }.uniq.sort 54 positions = ordinal_days.map { |match| match[1].to_i }.uniq.sort
53 weekdays_in_rule = ordinal_days.map { |match| match[2] }.uniq 55 weekdays_in_rule = ordinal_days.map { |match| match[2] }.uniq
54 missing_positions = (1..5).to_a - positions 56 if weekdays_in_rule.size == 1
55 if positions.size == 4 && weekdays_in_rule.size == 1 && missing_positions.size == 1 57 selected_monthly_ordinals = positions
56 excluded_monthly_ordinal = missing_positions.first 58 selected_monthly_weekday = weekdays_in_rule.first
57 excluded_monthly_weekday = weekdays_in_rule.first 59 missing_positions = (1..5).to_a - positions
60 if positions.size == 4 && missing_positions.size == 1
61 excluded_monthly_ordinal = missing_positions.first
62 excluded_monthly_weekday = selected_monthly_weekday
63 end
58 end 64 end
59 end 65 end
60 end 66 end
61 67
68 join_ordinals = lambda do |ordinal_values, conjunction|
69 names = ordinal_values.map { |ordinal| ordinals[ordinal] }
70 names.size > 1 ? "#{names[0..-2].join(', ')} #{conjunction} #{names.last}" : names.first
71 end
72
62 base = 73 base =
63 case loc 74 case loc
64 when :de 75 when :de
@@ -77,6 +88,8 @@ module RruleHumanizer
77 when "MONTHLY" 88 when "MONTHLY"
78 if excluded_monthly_ordinal 89 if excluded_monthly_ordinal
79 "Jeden #{weekdays[excluded_monthly_weekday]} im Monat, außer dem #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}" 90 "Jeden #{weekdays[excluded_monthly_weekday]} im Monat, außer dem #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}"
91 elsif selected_monthly_ordinals
92 "Jeden #{join_ordinals.call(selected_monthly_ordinals, 'und')} #{weekdays[selected_monthly_weekday]} im Monat"
80 else 93 else
81 days ? "Jeden #{days.join(' und ')} im Monat" : "Monatlich" 94 days ? "Jeden #{days.join(' und ')} im Monat" : "Monatlich"
82 end 95 end
@@ -88,6 +101,8 @@ module RruleHumanizer
88 when "MONTHLY" 101 when "MONTHLY"
89 if excluded_monthly_ordinal 102 if excluded_monthly_ordinal
90 "Every #{weekdays[excluded_monthly_weekday]} of the month, except the #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}" 103 "Every #{weekdays[excluded_monthly_weekday]} of the month, except the #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}"
104 elsif selected_monthly_ordinals
105 "Every #{join_ordinals.call(selected_monthly_ordinals, 'and')} #{weekdays[selected_monthly_weekday]} of the month"
91 else 106 else
92 days ? "Every #{days.join(' and ')} of the month" : "Monthly" 107 days ? "Every #{days.join(' and ')} of the month" : "Monthly"
93 end 108 end
diff --git a/test/models/concerns/rrule_humanizer_test.rb b/test/models/concerns/rrule_humanizer_test.rb
index c4e78d4..54c4c22 100644
--- a/test/models/concerns/rrule_humanizer_test.rb
+++ b/test/models/concerns/rrule_humanizer_test.rb
@@ -48,10 +48,10 @@ class RruleHumanizerTest < ActiveSupport::TestCase
48 end 48 end
49 49
50 test "monthly selected weeks" do 50 test "monthly selected weeks" do
51 assert_equal "Jeden ersten Dienstag und dritten Dienstag im Monat", 51 assert_equal "Jeden ersten, zweiten und fünften Dienstag im Monat",
52 humanize("FREQ=MONTHLY;BYDAY=1TU,3TU") 52 humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU")
53 assert_equal "Every first Tuesday and third Tuesday of the month", 53 assert_equal "Every first, second and fifth Tuesday of the month",
54 humanize("FREQ=MONTHLY;BYDAY=1TU,3TU", :en) 54 humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU", :en)
55 end 55 end
56 56
57 test "monthly fifth weekday" do 57 test "monthly fifth weekday" do