Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Create a duration_property helper in the API::Decorators::DateProperty

Dombi Attila40688d6304e7
diff --git a/lib/api/decorators/date_property.rb b/lib/api/decorators/date_property.rb
index a37381460b2..a2b5f716b90 100644
--- a/lib/api/decorators/date_property.rb
+++ b/lib/api/decorators/date_property.rb
@@ -66,6 +66,19 @@ module API
attributes.merge(args)
end
+ def duration_property(name,
+ getter: default_duration_getter(name),
+ setter: default_duration_setter(name),
+ **args)
+ attributes = {
+ getter:,
+ setter:
+ }
+
+ property name,
+ attributes.merge(args)
+ end
+
private
def default_date_getter(name)
@@ -91,6 +104,23 @@ module API
decorator.datetime_formatter.format_datetime(represented.send(name), allow_nil: true)
}
end
+
+ def default_duration_getter(name)
+ ->(represented:, decorator:, **) {
+ decorator.datetime_formatter.format_duration_from_hours(represented.send(name), allow_nil: true)
+ }
+ end
+
+ def default_duration_setter(name)
+ ->(fragment:, decorator:, **) {
+ hours = decorator
+ .datetime_formatter
+ .parse_duration_to_hours(fragment,
+ name.to_s.camelize(:lower),
+ allow_nil: true)
+ send(:"#{name}=", hours)
+ }
+ end
end
end
end
diff --git a/lib/api/v3/user_preferences/notification_setting_representer.rb b/lib/api/v3/user_preferences/notification_setting_representer.rb
index 44ac5df6f61..5aa77e2177d 100644
--- a/lib/api/v3/user_preferences/notification_setting_representer.rb
+++ b/lib/api/v3/user_preferences/notification_setting_representer.rb
@@ -31,30 +31,13 @@ module API
module UserPreferences
class NotificationSettingRepresenter < ::API::Decorators::Single
include API::Decorators::LinkedResource
+ include API::Decorators::DateProperty
NotificationSetting.all_settings.each do |setting|
- setting_configuration = {}
if setting.in?(NotificationSetting.duration_settings)
- setting_configuration = {
- exec_context: :decorator,
- getter: ->(*) do
- datetime_formatter.format_duration_from_hours(represented.send(setting),
- allow_nil: true)
- end,
- setter: ->(fragment:, **) {
- hours = datetime_formatter.parse_duration_to_hours(fragment, setting, allow_nil: true)
-
- send(:"#{setting}=", hours)
- }
- }
- end
-
- property setting, **setting_configuration
- end
-
- NotificationSetting.duration_settings.each do |setting|
- define_method "#{setting}=" do |value|
- represented[setting] = value
+ duration_property setting
+ else
+ property setting
end
end