recurrence_ui 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +104 -82
- data/app/components/recurrence_ui/recurrence_picker_component.html.erb +96 -169
- data/app/components/recurrence_ui/recurrence_picker_component.rb +76 -32
- data/app/javascript/recurrence_ui/recurrence_ui_controller.js +24 -7
- data/app/models/concerns/recurrence_ui/has_recurrence.rb +113 -155
- data/app/services/recurrence_ui/recurrence_formatter_service.rb +1 -2
- data/app/views/recurrence_ui/_duration.html.erb +40 -0
- data/app/views/recurrence_ui/_end_at.html.erb +14 -0
- data/app/views/recurrence_ui/_multiple_rules.html.erb +0 -0
- data/app/views/recurrence_ui/_single_rule.html.erb +36 -0
- data/app/views/recurrence_ui/_start_at.html.erb +21 -0
- data/app/views/recurrence_ui/_termination.html.erb +1 -1
- data/app/views/recurrence_ui/_time_zone.html.erb +9 -0
- data/config/locales/recurrence_ui.be.yml +6 -6
- data/config/locales/recurrence_ui.en.yml +3 -3
- data/config/locales/recurrence_ui.ru.yml +4 -4
- data/config/locales/recurrence_ui.uk.yml +7 -7
- data/lib/generators/recurrence_ui/install/install_generator.rb +48 -11
- data/lib/recurrence_ui/configuration.rb +80 -11
- data/lib/recurrence_ui/engine.rb +7 -7
- data/lib/recurrence_ui/version.rb +1 -1
- metadata +8 -2
|
@@ -2,7 +2,27 @@
|
|
|
2
2
|
module RecurrenceUi
|
|
3
3
|
class RecurrencePickerComponent < ViewComponent::Base
|
|
4
4
|
include ActionView::Helpers::TranslationHelper
|
|
5
|
-
attr_reader
|
|
5
|
+
attr_reader(
|
|
6
|
+
:record,
|
|
7
|
+
:i18n_prefix,
|
|
8
|
+
:identifier,
|
|
9
|
+
:locale,
|
|
10
|
+
:time_zone,
|
|
11
|
+
:week_start,
|
|
12
|
+
:start_time,
|
|
13
|
+
:end_time,
|
|
14
|
+
:duration,
|
|
15
|
+
:enabled_rule_types,
|
|
16
|
+
# UI display options
|
|
17
|
+
:allow_multiple_rules, #show multiple rules in the UI if false - you can only have one rule at a time
|
|
18
|
+
:show_duration, # show duration field in the UI
|
|
19
|
+
:show_start_date_time, # show start time field in the UI
|
|
20
|
+
:show_end_date_time, # show end time (for one occurrence) field in the UI
|
|
21
|
+
:show_exceptions, # show exception dates field in the UI
|
|
22
|
+
:show_rdates, # show additional date field in the UI
|
|
23
|
+
:show_termination, # show termination field in the UI (end date or count of occurrences)
|
|
24
|
+
:show_time_zone # show time zone field in the UI
|
|
25
|
+
)
|
|
6
26
|
|
|
7
27
|
def initialize(
|
|
8
28
|
record:,
|
|
@@ -11,7 +31,19 @@ module RecurrenceUi
|
|
|
11
31
|
locale: nil,
|
|
12
32
|
time_zone: nil,
|
|
13
33
|
week_start: nil,
|
|
14
|
-
|
|
34
|
+
start_time: nil,
|
|
35
|
+
duration: nil,
|
|
36
|
+
# selected rule types for the UI, if not specified - all types will be shown
|
|
37
|
+
enabled_rule_types: nil,
|
|
38
|
+
# UI display options
|
|
39
|
+
allow_multiple_rules: nil,
|
|
40
|
+
show_duration: nil,
|
|
41
|
+
show_start_date_time: nil,
|
|
42
|
+
show_end_date_time: nil,
|
|
43
|
+
show_exceptions: nil,
|
|
44
|
+
show_rdates: nil,
|
|
45
|
+
show_termination: nil,
|
|
46
|
+
show_time_zone: nil
|
|
15
47
|
)
|
|
16
48
|
@raw_form = form
|
|
17
49
|
@record = record
|
|
@@ -25,6 +57,19 @@ module RecurrenceUi
|
|
|
25
57
|
@locale = RecurrenceUi.config.resolved_locale(locale)
|
|
26
58
|
@time_zone = RecurrenceUi.config.resolved_time_zone(time_zone)
|
|
27
59
|
@week_start = RecurrenceUi.config.resolved_week_start(week_start)
|
|
60
|
+
@start_time = RecurrenceUi.config.resolved_start_time(start_time)
|
|
61
|
+
@duration = RecurrenceUi.config.resolved_duration(duration)
|
|
62
|
+
@end_time = RecurrenceUi.config.resolved_end_time(end_time)
|
|
63
|
+
|
|
64
|
+
# Resolve UI display options with fallbacks
|
|
65
|
+
@allow_multiple_rules = RecurrenceUi.config.resolved_allow_multiple_rules(allow_multiple_rules)
|
|
66
|
+
@show_duration = RecurrenceUi.config.resolved_show_duration(show_duration)
|
|
67
|
+
@show_start_date_time = RecurrenceUi.config.resolved_show_start_date_time(show_start_date_time)
|
|
68
|
+
@show_end_date_time = RecurrenceUi.config.resolved_show_end_date_time(show_end_date_time)
|
|
69
|
+
@show_exceptions = RecurrenceUi.config.resolved_show_exceptions(show_exceptions)
|
|
70
|
+
@show_rdates = RecurrenceUi.config.resolved_show_rdates(show_rdates)
|
|
71
|
+
@show_termination = RecurrenceUi.config.resolved_show_termination(show_termination)
|
|
72
|
+
@show_time_zone = RecurrenceUi.config.resolved_show_time_zone(show_time_zone)
|
|
28
73
|
|
|
29
74
|
log_configuration_details if Rails.env.development?
|
|
30
75
|
end
|
|
@@ -72,50 +117,49 @@ module RecurrenceUi
|
|
|
72
117
|
|
|
73
118
|
:duration # дефолт
|
|
74
119
|
end
|
|
120
|
+
# Есть ли настройка для отображения стартового времени и есть ли поле в базе.
|
|
121
|
+
def show_start_date_time?
|
|
122
|
+
return false unless @show_start_date_time
|
|
123
|
+
|
|
124
|
+
# Проверяем, задано ли имя поля и отвечает ли за него рекорд
|
|
125
|
+
start_time_field.present? && record.respond_to?(start_time_field)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def show_duration?
|
|
129
|
+
return false unless @show_duration
|
|
130
|
+
|
|
131
|
+
duration_field.present? && record.respond_to?(duration_field)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def show_end_date_time?
|
|
135
|
+
return false unless @show_end_date_time
|
|
136
|
+
end_time_field.present? && record.respond_to?(end_time_field)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Показывать ли блок часового пояса
|
|
140
|
+
def show_time_zone?
|
|
141
|
+
return false unless @show_time_zone
|
|
142
|
+
|
|
143
|
+
time_zone_field.present? && record.respond_to?(time_zone_field)
|
|
144
|
+
end
|
|
75
145
|
|
|
76
146
|
|
|
77
147
|
# --- Динамические хелперы для имён полей из модели ---
|
|
78
148
|
|
|
79
149
|
def start_time_field
|
|
80
|
-
@start_time_field ||= record.class.respond_to?(:recurrence_start_time_field) ? record.class.recurrence_start_time_field :
|
|
150
|
+
@start_time_field ||= record.class.respond_to?(:recurrence_start_time_field) ? record.class.recurrence_start_time_field : nil
|
|
81
151
|
end
|
|
82
152
|
|
|
83
153
|
def end_time_field
|
|
84
|
-
@end_time_field ||= record.class.respond_to?(:recurrence_end_time_field) ? record.class.recurrence_end_time_field :
|
|
154
|
+
@end_time_field ||= record.class.respond_to?(:recurrence_end_time_field) ? record.class.recurrence_end_time_field : nil
|
|
85
155
|
end
|
|
86
156
|
# Возвращает начало события в часовом поясе самого события.
|
|
87
|
-
def initial_start_at
|
|
88
|
-
value = record.public_send(start_time_field)
|
|
89
|
-
return if value.blank?
|
|
90
157
|
|
|
91
|
-
value.in_time_zone(event_time_zone)
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# Возвращает окончание события в часовом поясе самого события.
|
|
95
|
-
def initial_end_at
|
|
96
|
-
value = record.public_send(end_time_field)
|
|
97
|
-
return if value.blank?
|
|
98
|
-
|
|
99
|
-
value.in_time_zone(event_time_zone)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
# Определяет часовой пояс события.
|
|
103
|
-
def event_time_zone
|
|
104
|
-
if record.respond_to?(:event_time_zone) && record.event_time_zone.present?
|
|
105
|
-
record.event_time_zone
|
|
106
|
-
else
|
|
107
|
-
zone_name = record.respond_to?(time_zone_field) ? record.public_send(time_zone_field) : nil
|
|
108
|
-
|
|
109
|
-
ActiveSupport::TimeZone[zone_name.presence] ||
|
|
110
|
-
Time.zone ||
|
|
111
|
-
ActiveSupport::TimeZone[Rails.application.config.time_zone]
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
158
|
def duration_field
|
|
115
|
-
@duration_field ||= record.class.respond_to?(:recurrence_duration_field) ? record.class.recurrence_duration_field :
|
|
159
|
+
@duration_field ||= record.class.respond_to?(:recurrence_duration_field) ? record.class.recurrence_duration_field : nil
|
|
116
160
|
end
|
|
117
161
|
def time_zone_field
|
|
118
|
-
@time_zone_field ||= record.class.respond_to?(:recurrence_time_zone_field) ? record.class.recurrence_time_zone_field :
|
|
162
|
+
@time_zone_field ||= record.class.respond_to?(:recurrence_time_zone_field) ? record.class.recurrence_time_zone_field : nil
|
|
119
163
|
end
|
|
120
164
|
|
|
121
165
|
def rules
|
|
@@ -38,6 +38,11 @@ export default class extends Controller {
|
|
|
38
38
|
this.initializeState()
|
|
39
39
|
this.updateDebugField()
|
|
40
40
|
this.initializeDurationMode()
|
|
41
|
+
// Принудительно инициализируем видимость полей для режима «Одно правило»
|
|
42
|
+
|
|
43
|
+
this.element.querySelectorAll('select[data-action*="toggleCardFields"]').forEach(select => {
|
|
44
|
+
this.toggleCardFields({ currentTarget: select })
|
|
45
|
+
})
|
|
41
46
|
this.isInitializing = false
|
|
42
47
|
}
|
|
43
48
|
|
|
@@ -437,6 +442,7 @@ export default class extends Controller {
|
|
|
437
442
|
// 3. Добавление исключения (Exdate)
|
|
438
443
|
addExdate(event) {
|
|
439
444
|
if (event) event.preventDefault()
|
|
445
|
+
if (!this.hasExdateInputTarget || !this.hasExdatesContainerTarget || !this.hasExdateTemplateTarget) return
|
|
440
446
|
this._addDateFromTemplate(this.exdateInputTarget, this.exdatesContainerTarget, this.exdateTemplateTarget)
|
|
441
447
|
if (this.hasAddExdateBtnTarget) this.addExdateBtnTarget.disabled = true
|
|
442
448
|
}
|
|
@@ -444,6 +450,7 @@ export default class extends Controller {
|
|
|
444
450
|
// 4. Добавление дополнительной даты (Rdate)
|
|
445
451
|
addRdate(event) {
|
|
446
452
|
if (event) event.preventDefault()
|
|
453
|
+
if (!this.hasRdateInputTarget || !this.hasRdatesContainerTarget || !this.hasRdateTemplateTarget) return
|
|
447
454
|
this._addDateFromTemplate(this.rdateInputTarget, this.rdatesContainerTarget, this.rdateTemplateTarget)
|
|
448
455
|
if (this.hasAddRdateBtnTarget) this.addRdateBtnTarget.disabled = true
|
|
449
456
|
}
|
|
@@ -506,9 +513,14 @@ export default class extends Controller {
|
|
|
506
513
|
.map(card => this._buildSingleRuleHash(card))
|
|
507
514
|
.filter(Boolean)
|
|
508
515
|
|
|
509
|
-
// 2. Извлекаем списки дат
|
|
510
|
-
const exdates = this.
|
|
511
|
-
|
|
516
|
+
// 2. Извлекаем списки дат (безопасно проверяем наличие контейнеров)
|
|
517
|
+
const exdates = this.hasExdatesContainerTarget
|
|
518
|
+
? this._getDatesArray(this.exdatesContainerTarget, 'exdateValue')
|
|
519
|
+
: []
|
|
520
|
+
|
|
521
|
+
const rdates = this.hasRdatesContainerTarget
|
|
522
|
+
? this._getDatesArray(this.rdatesContainerTarget, 'rdateValue')
|
|
523
|
+
: []
|
|
512
524
|
|
|
513
525
|
// 3. Формируем итоговый объект расписания
|
|
514
526
|
const schedule = {}
|
|
@@ -529,11 +541,16 @@ export default class extends Controller {
|
|
|
529
541
|
}
|
|
530
542
|
|
|
531
543
|
// 4. Записываем в hidden_field чистый JSON (если всё пусто — сохранится "{}" вместо пустых структур)
|
|
544
|
+
if (this.hasHiddenFieldTarget) {
|
|
532
545
|
this.hiddenFieldTarget.value = JSON.stringify(schedule)
|
|
533
|
-
|
|
534
|
-
// 5. Обновляем текстовую сводку и отладчик
|
|
535
|
-
this.updateSummary
|
|
536
|
-
|
|
546
|
+
}
|
|
547
|
+
// 5. Обновляем текстовую сводку и отладчик (с проверкой наличия методов/таргетов)
|
|
548
|
+
if (typeof this.updateSummary === 'function') {
|
|
549
|
+
this.updateSummary()
|
|
550
|
+
}
|
|
551
|
+
if (typeof this.updateDebugField === 'function') {
|
|
552
|
+
this.updateDebugField()
|
|
553
|
+
}
|
|
537
554
|
}
|
|
538
555
|
|
|
539
556
|
|
|
@@ -3,12 +3,13 @@ module RecurrenceUi
|
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
5
|
included do
|
|
6
|
-
#
|
|
7
|
-
class_attribute :recurrence_rules_field, default:
|
|
8
|
-
class_attribute :recurrence_start_time_field, default:
|
|
9
|
-
class_attribute :recurrence_end_time_field, default:
|
|
10
|
-
class_attribute :recurrence_time_zone_field, default:
|
|
11
|
-
class_attribute :recurrence_duration_field, default:
|
|
6
|
+
# Только поле правил имеет разумный дефолт, остальные по умолчанию nil (отключены)
|
|
7
|
+
class_attribute :recurrence_rules_field, default: :recurring_rules
|
|
8
|
+
class_attribute :recurrence_start_time_field, default: nil
|
|
9
|
+
class_attribute :recurrence_end_time_field, default: nil
|
|
10
|
+
class_attribute :recurrence_time_zone_field, default: nil
|
|
11
|
+
class_attribute :recurrence_duration_field, default: nil
|
|
12
|
+
|
|
12
13
|
attr_accessor :form_start_date, :form_start_time,
|
|
13
14
|
:form_duration_mode, :form_duration_days, :form_duration_hours, :form_duration_minutes,
|
|
14
15
|
:form_end_date, :form_end_time
|
|
@@ -17,14 +18,7 @@ module RecurrenceUi
|
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
class_methods do
|
|
20
|
-
# DSL
|
|
21
|
-
#
|
|
22
|
-
# @param rules [Symbol, String, nil] Field name for storing rules (JSON/Hash).
|
|
23
|
-
# @param start_time [Symbol, String, nil] Field name for start date/time.
|
|
24
|
-
# @param end_time [Symbol, String, nil] Field name for end date/time.
|
|
25
|
-
# @param time_zone [Symbol, String, nil] Field name for time zone.
|
|
26
|
-
# @param duration [Symbol, String, nil] Field name for duration in seconds.
|
|
27
|
-
# @return [void]
|
|
21
|
+
# DSL для явной конфигурации полей модели.
|
|
28
22
|
def has_recurrence(rules: nil, start_time: nil, end_time: nil, time_zone: nil, duration: nil)
|
|
29
23
|
self.recurrence_rules_field = rules if rules.present?
|
|
30
24
|
self.recurrence_start_time_field = start_time if start_time.present?
|
|
@@ -34,11 +28,8 @@ module RecurrenceUi
|
|
|
34
28
|
|
|
35
29
|
field_name = recurrence_rules_field.to_s
|
|
36
30
|
|
|
37
|
-
# READ: Returns a Hash containing rules and UI metadata
|
|
38
31
|
define_method(field_name) do
|
|
39
|
-
raw_val =
|
|
40
|
-
|
|
41
|
-
# What radio button mode was selected in the form? 'duration' or 'end_at'
|
|
32
|
+
raw_val = safe_read_attribute(field_name)
|
|
42
33
|
return { 'form_duration_mode' => 'duration', 'rrules' => [] }.with_indifferent_access if raw_val.blank?
|
|
43
34
|
|
|
44
35
|
parsed = if raw_val.is_a?(Hash)
|
|
@@ -49,12 +40,10 @@ module RecurrenceUi
|
|
|
49
40
|
{}
|
|
50
41
|
end
|
|
51
42
|
|
|
52
|
-
# Ensure that the form_duration_mode key is always present, defaulting to 'duration' if missing
|
|
53
43
|
parsed[:form_duration_mode] ||= 'duration'
|
|
54
44
|
parsed
|
|
55
45
|
end
|
|
56
46
|
|
|
57
|
-
# WRITE: Accepts a Hash or JSON string and stores it in the database (it could contain both rules and UI metadata)
|
|
58
47
|
define_method("#{field_name}=") do |value|
|
|
59
48
|
parsed_value = case value
|
|
60
49
|
when String
|
|
@@ -65,125 +54,101 @@ module RecurrenceUi
|
|
|
65
54
|
{}
|
|
66
55
|
end
|
|
67
56
|
|
|
68
|
-
|
|
57
|
+
safe_write_attribute(field_name, parsed_value.deep_stringify_keys)
|
|
69
58
|
end
|
|
70
59
|
end
|
|
71
60
|
|
|
72
|
-
# Method to return the list of permitted parameters for recurrence fields, useful for strong parameters in controllers.
|
|
73
61
|
def recurrence_permitted_params
|
|
74
|
-
[
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
:form_start_time,
|
|
82
|
-
:form_duration_mode,
|
|
83
|
-
:
|
|
84
|
-
:form_duration_hours,
|
|
85
|
-
:form_duration_minutes,
|
|
86
|
-
:form_end_date,
|
|
87
|
-
:form_end_time
|
|
62
|
+
params = [recurrence_rules_field.to_sym]
|
|
63
|
+
params << recurrence_start_time_field.to_sym if recurrence_start_time_field.present?
|
|
64
|
+
params << recurrence_end_time_field.to_sym if recurrence_end_time_field.present?
|
|
65
|
+
params << recurrence_time_zone_field.to_sym if recurrence_time_zone_field.present?
|
|
66
|
+
params << recurrence_duration_field.to_sym if recurrence_duration_field.present?
|
|
67
|
+
|
|
68
|
+
params + [
|
|
69
|
+
:form_start_date, :form_start_time,
|
|
70
|
+
:form_duration_mode, :form_duration_days, :form_duration_hours, :form_duration_minutes,
|
|
71
|
+
:form_end_date, :form_end_time
|
|
88
72
|
]
|
|
73
|
+
end
|
|
89
74
|
end
|
|
90
|
-
|
|
91
|
-
end
|
|
92
|
-
# Returns a RecurrenceFormatterService instance for formatting recurrence data.
|
|
93
|
-
# @param options [Hash] Options to customize formatting (e.g., :zone_type, :show_zone)
|
|
94
|
-
# @return [RecurrenceFormatterService]
|
|
95
|
-
def summary(options = {})
|
|
96
|
-
RecurrenceFormatterService.new(self, options)
|
|
97
|
-
end
|
|
98
75
|
|
|
76
|
+
def summary(options = {})
|
|
77
|
+
RecurrenceFormatterService.new(self, options)
|
|
78
|
+
end
|
|
99
79
|
|
|
80
|
+
# --- Instance Methods с защитой от отсутствующих полей ---
|
|
100
81
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def recurrence_raw_rules
|
|
105
|
-
public_send(self.class.recurrence_rules_field)
|
|
106
|
-
end
|
|
82
|
+
def recurrence_raw_rules
|
|
83
|
+
public_send(self.class.recurrence_rules_field)
|
|
84
|
+
end
|
|
107
85
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
ActiveSupport::TimeZone[tz_name] || Time.zone || ActiveSupport::TimeZone[Rails.application.config.time_zone]
|
|
119
|
-
end
|
|
86
|
+
# Определяет часовой пояс события.
|
|
87
|
+
def event_time_zone
|
|
88
|
+
tz_name = nil
|
|
89
|
+
|
|
90
|
+
# Если поле таймзоны настроено через has_recurrence и существует у модели
|
|
91
|
+
attr = self.class.recurrence_time_zone_field
|
|
92
|
+
if attr.present?
|
|
93
|
+
tz_name = public_send(attr) if respond_to?(attr)
|
|
94
|
+
end
|
|
120
95
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
raw_start = nil
|
|
125
|
-
if self.class.recurrence_start_time_field && respond_to?(self.class.recurrence_start_time_field)
|
|
126
|
-
raw_start = public_send(self.class.recurrence_start_time_field)
|
|
96
|
+
# Фоллбэки: значение из поля -> таймзона приложения -> глобальный конфиг Rails
|
|
97
|
+
zone_name = tz_name.presence || Time.zone&.name
|
|
98
|
+
ActiveSupport::TimeZone[zone_name] || Time.zone || ActiveSupport::TimeZone[Rails.application.config.time_zone]
|
|
127
99
|
end
|
|
128
|
-
(raw_start || Time.current).in_time_zone(event_time_zone)
|
|
129
|
-
end
|
|
130
100
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
return nil unless end_field && respond_to?(end_field)
|
|
101
|
+
|
|
102
|
+
def recurrence_start_time
|
|
103
|
+
attr = self.class.recurrence_start_time_field
|
|
104
|
+
return nil unless attr.present?
|
|
136
105
|
|
|
137
|
-
|
|
138
|
-
|
|
106
|
+
raw_start = safe_read_attribute(attr)
|
|
107
|
+
return nil unless raw_start.present? # <--- если в базе пусто, возвращаем nil
|
|
139
108
|
|
|
140
|
-
|
|
141
|
-
|
|
109
|
+
raw_start.in_time_zone(event_time_zone)
|
|
110
|
+
end
|
|
142
111
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
def start_time_in_zone(zone_type = :event)
|
|
147
|
-
time_in_zone(recurrence_start_time, zone_type)
|
|
148
|
-
end
|
|
112
|
+
def recurrence_end_time #возвращает Time в часовом поясе события, либо nil, если поле не настроено или не существует у модели
|
|
113
|
+
attr = self.class.recurrence_end_time_field
|
|
114
|
+
return nil unless attr.present?
|
|
149
115
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
# @return [ActiveSupport::TimeWithZone, nil] The end time of the recurrence in the specified time zone, or nil
|
|
153
|
-
def end_time_in_zone(zone_type = :event)
|
|
154
|
-
time_in_zone(recurrence_end_time, zone_type)
|
|
155
|
-
end
|
|
116
|
+
raw_end = safe_read_attribute(attr)
|
|
117
|
+
return nil unless raw_end.present?
|
|
156
118
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
119
|
+
raw_end.in_time_zone(event_time_zone)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def start_time_in_zone(zone_type = :event)
|
|
123
|
+
return nil unless recurrence_start_time
|
|
124
|
+
time_in_zone(recurrence_start_time, zone_type)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def end_time_in_zone(zone_type = :event)
|
|
128
|
+
return nil unless recurrence_end_time
|
|
129
|
+
time_in_zone(recurrence_end_time, zone_type)
|
|
164
130
|
end
|
|
165
|
-
|
|
131
|
+
|
|
132
|
+
def current_duration_seconds
|
|
133
|
+
attr = self.class.recurrence_duration_field
|
|
134
|
+
return nil unless attr.present? && respond_to?(attr)
|
|
135
|
+
|
|
136
|
+
val = safe_read_attribute(attr)
|
|
137
|
+
val.present? ? val.to_i : nil # Возвращаем nil, если в базе пусто, а не 0
|
|
166
138
|
end
|
|
167
|
-
|
|
168
139
|
|
|
169
|
-
#
|
|
170
|
-
# Returns an IceCube::Schedule object based on the stored recurrence rules and start time.
|
|
171
|
-
# @param start_time_override [Time, nil] Optional override for the start time of the schedule.
|
|
172
|
-
# @return [IceCube::Schedule] The IceCube schedule object representing the recurrence rules.
|
|
140
|
+
# Сборка IceCube (остается без изменений, так как опирается на безопасные методы выше)
|
|
173
141
|
def schedule(start_time_override = nil)
|
|
174
|
-
base_time = start_time_override || recurrence_start_time
|
|
142
|
+
base_time = start_time_override || recurrence_start_time || Time.current.in_time_zone(event_time_zone)
|
|
175
143
|
s = IceCube::Schedule.new(base_time)
|
|
176
144
|
|
|
177
145
|
dur = current_duration_seconds
|
|
178
|
-
s.duration = dur if dur > 0
|
|
146
|
+
s.duration = dur if dur.present? && dur > 0
|
|
179
147
|
|
|
180
148
|
raw_data = recurrence_raw_rules
|
|
181
149
|
return s if raw_data.blank?
|
|
182
150
|
|
|
183
|
-
# Превращаем в хэш с строковыми ключами на всякий случай
|
|
184
151
|
raw_data = raw_data.deep_stringify_keys if raw_data.is_a?(Hash)
|
|
185
|
-
|
|
186
|
-
# Достаем массив правил безопасно, игнорируя UI-ключи типа form_duration_mode
|
|
187
152
|
rules_array = raw_data['rrules'] || (raw_data['rule_type'] ? [raw_data] : [])
|
|
188
153
|
|
|
189
154
|
rules_array.each do |rule_hash|
|
|
@@ -213,11 +178,7 @@ module RecurrenceUi
|
|
|
213
178
|
parsed_date = Date.parse(exdate.to_s) rescue nil
|
|
214
179
|
next unless parsed_date
|
|
215
180
|
|
|
216
|
-
exact_datetime = base_time.change(
|
|
217
|
-
year: parsed_date.year,
|
|
218
|
-
month: parsed_date.month,
|
|
219
|
-
day: parsed_date.day
|
|
220
|
-
)
|
|
181
|
+
exact_datetime = base_time.change(year: parsed_date.year, month: parsed_date.month, day: parsed_date.day)
|
|
221
182
|
s.add_exception_time(exact_datetime)
|
|
222
183
|
end
|
|
223
184
|
|
|
@@ -226,96 +187,96 @@ module RecurrenceUi
|
|
|
226
187
|
parsed_date = Date.parse(rdate.to_s) rescue nil
|
|
227
188
|
next unless parsed_date
|
|
228
189
|
|
|
229
|
-
exact_datetime = base_time.change(
|
|
230
|
-
year: parsed_date.year,
|
|
231
|
-
month: parsed_date.month,
|
|
232
|
-
day: parsed_date.day
|
|
233
|
-
)
|
|
190
|
+
exact_datetime = base_time.change(year: parsed_date.year, month: parsed_date.month, day: parsed_date.day)
|
|
234
191
|
s.add_recurrence_time(exact_datetime)
|
|
235
192
|
end
|
|
236
193
|
|
|
237
194
|
s
|
|
238
195
|
end
|
|
239
196
|
|
|
240
|
-
# Returns the next occurrences of the event based on the schedule, limited to a specified number and starting from a given time.
|
|
241
|
-
# @param limit [Integer] The maximum number of occurrences to return.
|
|
242
|
-
# @param from_time [Time] The time from which to start looking for occurrences.
|
|
243
|
-
# @return [Array<Time>] An array of the next occurrence times in the event's time zone.
|
|
244
197
|
def upcoming_occurrences(limit = 5, from_time = Time.current)
|
|
245
198
|
schedule.next_occurrences(limit, from_time.in_time_zone(event_time_zone))
|
|
246
199
|
end
|
|
247
200
|
|
|
248
|
-
# Returns the next occurrences of the event in a specified time zone, limited to a specified number and starting from a given time.
|
|
249
|
-
# @param limit [Integer] The maximum number of occurrences to return.
|
|
250
|
-
# @param from_time [Time] The time from which to start looking for occurrences.
|
|
251
|
-
# @param zone_type [Symbol, String] The type of time zone to convert to (:event, :application, or a specific zone name).
|
|
252
|
-
# @return [Array<Time>] An array of the next occurrence times in the specified time zone.
|
|
253
201
|
def upcoming_occurrences_in_zone(limit = 5, from_time = Time.current, zone_type = :event)
|
|
254
202
|
target_zone = resolve_zone_type(zone_type)
|
|
255
203
|
schedule.next_occurrences(limit, from_time.in_time_zone(target_zone))
|
|
256
204
|
end
|
|
257
205
|
|
|
258
|
-
# Returns the first occurrence of the event based on the schedule.
|
|
259
|
-
# @return [Time, nil] The time of the first occurrence in the event's time zone, or nil if there are no occurrences.
|
|
260
206
|
def first_occurrence
|
|
261
207
|
schedule.first
|
|
262
208
|
end
|
|
263
209
|
|
|
264
|
-
# Returns the rule object for the first recurrence rule in the schedule, if any.
|
|
265
|
-
# @return [IceCube::Rule, nil] The first recurrence rule, or nil if there are no rules.
|
|
266
210
|
def rule
|
|
267
211
|
schedule.recurrence_rules.first
|
|
268
212
|
end
|
|
269
213
|
|
|
270
|
-
# Delegate formatting methods to the summary service
|
|
271
214
|
delegate :formatted_start_time, :formatted_end_time, :formatted_duration, :formatted_upcoming_occurrences, :to_s, to: :summary
|
|
272
215
|
|
|
273
216
|
private
|
|
274
|
-
|
|
275
|
-
#
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
217
|
+
|
|
218
|
+
# --- Приватные методы безопасности (Safe Attributes) ---
|
|
219
|
+
|
|
220
|
+
def recurrence_attribute_present?(field_name)
|
|
221
|
+
return false if field_name.blank?
|
|
222
|
+
respond_to?(field_name) && (respond_to?(:has_attribute?) ? has_attribute?(field_name) : true)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def safe_read_attribute(field_name)
|
|
226
|
+
return nil unless recurrence_attribute_present?(field_name)
|
|
227
|
+
has_attribute?(field_name) ? read_attribute(field_name) : public_send(field_name)
|
|
228
|
+
rescue StandardError
|
|
229
|
+
nil
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def safe_write_attribute(field_name, value)
|
|
233
|
+
return false unless recurrence_attribute_present?(field_name)
|
|
234
|
+
|
|
235
|
+
if respond_to?(:write_attribute) && has_attribute?(field_name)
|
|
236
|
+
write_attribute(field_name, value) if read_attribute(field_name) != value
|
|
237
|
+
elsif respond_to?("#{field_name}=")
|
|
238
|
+
public_send("#{field_name}=", value)
|
|
239
|
+
end
|
|
240
|
+
rescue StandardError => e
|
|
241
|
+
Rails.logger.warn("[RecurrenceUi] Could not write to attribute #{field_name}: #{e.message}")
|
|
242
|
+
false
|
|
243
|
+
end
|
|
244
|
+
|
|
279
245
|
def time_in_zone(time, zone_type = :event)
|
|
280
246
|
return nil unless time
|
|
281
|
-
|
|
282
247
|
target_zone = resolve_zone_type(zone_type)
|
|
283
248
|
time.in_time_zone(target_zone)
|
|
284
249
|
end
|
|
285
250
|
|
|
286
|
-
# Processes the form attributes related to recurrence time and updates the model's fields accordingly.
|
|
287
|
-
# This method is called before saving the record to ensure that the recurrence start time, end time, and duration are correctly calculated and stored based on the form inputs.
|
|
288
251
|
def process_recurrence_time_attributes
|
|
289
252
|
tz = event_time_zone
|
|
290
253
|
|
|
291
254
|
if respond_to?(:form_start_date=) && form_start_date.present?
|
|
292
255
|
time_str = form_start_time.presence || '09:00'
|
|
293
256
|
parsed_start = tz.parse("#{form_start_date} #{time_str}")
|
|
294
|
-
|
|
257
|
+
safe_write_attribute(self.class.recurrence_start_time_field, parsed_start) if parsed_start
|
|
295
258
|
end
|
|
296
259
|
|
|
297
|
-
start_val =
|
|
260
|
+
start_val = recurrence_start_time
|
|
298
261
|
return unless start_val.present?
|
|
299
262
|
|
|
300
263
|
dur_field = self.class.recurrence_duration_field
|
|
301
264
|
end_field = self.class.recurrence_end_time_field
|
|
302
265
|
|
|
303
|
-
# Получаем текущий хэш (он уже содержит и правила, и метаданные)
|
|
304
266
|
current_rules = recurrence_raw_rules.deep_dup
|
|
305
267
|
|
|
306
|
-
# Сохраняем выбранный в форме режим в общие метаданные правил
|
|
307
268
|
if form_duration_mode.present?
|
|
308
269
|
current_rules[:form_duration_mode] = form_duration_mode
|
|
309
270
|
end
|
|
310
271
|
|
|
311
272
|
if form_duration_mode == 'end_at' && form_end_date.present?
|
|
312
|
-
end_time_str = form_end_time.presence ||
|
|
273
|
+
end_time_str = form_end_time.presence || DEFAULT_END_TIME
|
|
313
274
|
parsed_end = tz.parse("#{form_end_date} #{end_time_str}")
|
|
314
275
|
|
|
315
276
|
if parsed_end.present?
|
|
316
|
-
|
|
277
|
+
safe_write_attribute(end_field, parsed_end)
|
|
317
278
|
calculated_dur = (parsed_end - start_val).to_i
|
|
318
|
-
|
|
279
|
+
safe_write_attribute(dur_field, calculated_dur)
|
|
319
280
|
end
|
|
320
281
|
else
|
|
321
282
|
days = form_duration_days.to_i
|
|
@@ -324,20 +285,17 @@ module RecurrenceUi
|
|
|
324
285
|
total_seconds = (days * 1.day + hours * 1.hour + minutes * 1.minute).to_i
|
|
325
286
|
total_seconds = 1.hour if total_seconds <= 0
|
|
326
287
|
|
|
327
|
-
|
|
288
|
+
safe_write_attribute(dur_field, total_seconds)
|
|
328
289
|
|
|
329
290
|
calculated_end = start_val + total_seconds.seconds
|
|
330
|
-
|
|
291
|
+
safe_write_attribute(end_field, calculated_end)
|
|
331
292
|
end
|
|
332
293
|
|
|
333
|
-
# Записываем обновленный хэш (вместе с form_duration_mode) обратно через наш сеттер
|
|
334
294
|
public_send("#{self.class.recurrence_rules_field}=", current_rules)
|
|
335
295
|
end
|
|
336
|
-
|
|
337
|
-
# Resolves the appropriate time zone based on the provided zone type.
|
|
338
|
-
# @param zone_type [Symbol, String] The type of time zone to resolve (:event, :application, or a specific zone name).
|
|
339
|
-
# @return [ActiveSupport::TimeZone] The resolved time zone object.
|
|
296
|
+
|
|
340
297
|
def resolve_zone_type(zone_type)
|
|
298
|
+
zone_type = :application if event_time_zone.nil?
|
|
341
299
|
case zone_type
|
|
342
300
|
when :event
|
|
343
301
|
event_time_zone
|