diff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rb index 70d293f6b1..ded3fa5661 100644 --- a/lib/api/decorators/single.rb +++ b/lib/api/decorators/single.rb @@ -53,6 +53,10 @@ module API private + def datetime_formatter + API::V3::Utilities::DateTimeFormatter + end + def _type; end end end diff --git a/lib/api/v3/activities/activity_representer.rb b/lib/api/v3/activities/activity_representer.rb index 1e368540c3..17a25924b1 100644 --- a/lib/api/v3/activities/activity_representer.rb +++ b/lib/api/v3/activities/activity_representer.rb @@ -29,7 +29,8 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' + +API::V3::Utilities::DateTimeFormatter module API module V3 diff --git a/lib/api/v3/attachments/attachment_representer.rb b/lib/api/v3/attachments/attachment_representer.rb index 6c1726e29d..83cc23ec76 100644 --- a/lib/api/v3/attachments/attachment_representer.rb +++ b/lib/api/v3/attachments/attachment_representer.rb @@ -29,7 +29,6 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb index 28d4b02838..feaa079b6b 100644 --- a/lib/api/v3/projects/project_representer.rb +++ b/lib/api/v3/projects/project_representer.rb @@ -29,13 +29,11 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 module Projects class ProjectRepresenter < ::API::Decorators::Single - include API::V3::Utilities link :self do { @@ -61,10 +59,12 @@ module API property :created_on, as: 'createdAt', - getter: -> (*) { DateTimeFormatter::format_datetime(created_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.created_on) } property :updated_on, as: 'updatedAt', - getter: -> (*) { DateTimeFormatter::format_datetime(updated_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.updated_on) } property :type, getter: -> (*) { project_type.try(:name) }, render_nil: true diff --git a/lib/api/v3/users/user_representer.rb b/lib/api/v3/users/user_representer.rb index 92b83bd09b..e55740cbe0 100644 --- a/lib/api/v3/users/user_representer.rb +++ b/lib/api/v3/users/user_representer.rb @@ -29,14 +29,12 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 module Users class UserRepresenter < ::API::Decorators::Single include AvatarHelper - include API::V3::Utilities link :self do { @@ -89,10 +87,12 @@ module API exec_context: :decorator property :created_on, as: 'createdAt', - getter: -> (*) { DateTimeFormatter::format_datetime(created_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.created_on) } property :updated_on, as: 'updatedAt', - getter: -> (*) { DateTimeFormatter::format_datetime(updated_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.updated_on) } property :status, getter: -> (*) { status_name }, render_nil: true def _type diff --git a/lib/api/v3/versions/version_representer.rb b/lib/api/v3/versions/version_representer.rb index 405ee2dd38..2f5f8d5e19 100644 --- a/lib/api/v3/versions/version_representer.rb +++ b/lib/api/v3/versions/version_representer.rb @@ -29,13 +29,11 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 module Versions class VersionRepresenter < ::API::Decorators::Single - include API::V3::Utilities link :self do { @@ -71,19 +69,27 @@ module API render_nil: true property :start_date, - getter: -> (*) { DateTimeFormatter::format_date(start_date, allow_nil: true) }, + exec_context: :decorator, + getter: -> (*) { + datetime_formatter.format_date(represented.start_date, allow_nil: true) + }, render_nil: true property :due_date, as: 'endDate', - getter: -> (*) { DateTimeFormatter::format_date(due_date, allow_nil: true) }, + exec_context: :decorator, + getter: -> (*) { + datetime_formatter.format_date(represented.due_date, allow_nil: true) + }, render_nil: true property :status, render_nil: true property :created_on, as: 'createdAt', - getter: -> (*) { DateTimeFormatter::format_datetime(created_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.created_on) } property :updated_on, as: 'updatedAt', - getter: -> (*) { DateTimeFormatter::format_datetime(updated_on) } + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.updated_on) } def _type 'Version' diff --git a/lib/api/v3/work_packages/form/work_package_payload_representer.rb b/lib/api/v3/work_packages/form/work_package_payload_representer.rb index 21567f920b..7d3fe4b48a 100644 --- a/lib/api/v3/work_packages/form/work_package_payload_representer.rb +++ b/lib/api/v3/work_packages/form/work_package_payload_representer.rb @@ -29,7 +29,6 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 @@ -38,7 +37,6 @@ module API class WorkPackagePayloadRepresenter < Roar::Decorator include Roar::JSON::HAL include Roar::Hypermedia - include API::V3::Utilities self.as_strategy = ::API::Utilities::CamelCasingStrategy.new @@ -86,10 +84,10 @@ module API property :start_date, exec_context: :decorator, getter: -> (*) { - DateTimeFormatter::format_date(represented.start_date, allow_nil: true) + datetime_formatter.format_date(represented.start_date, allow_nil: true) }, setter: -> (value, *) { - represented.start_date = DateTimeFormatter::parse_date(value, + represented.start_date = datetime_formatter.parse_date(value, 'startDate', allow_nil: true) }, @@ -97,10 +95,10 @@ module API property :due_date, exec_context: :decorator, getter: -> (*) { - DateTimeFormatter::format_date(represented.due_date, allow_nil: true) + datetime_formatter.format_date(represented.due_date, allow_nil: true) }, setter: -> (value, *) { - represented.due_date = DateTimeFormatter::parse_date(value, + represented.due_date = datetime_formatter.parse_date(value, 'dueDate', allow_nil: true) }, @@ -120,6 +118,10 @@ module API private + def datetime_formatter + API::V3::Utilities::DateTimeFormatter + end + def work_package_attribute_links_representer(represented) ::API::V3::WorkPackages::Form::WorkPackageAttributeLinksRepresenter.new represented end diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb index 2451cf0cbb..edc6388b90 100644 --- a/lib/api/v3/work_packages/work_package_representer.rb +++ b/lib/api/v3/work_packages/work_package_representer.rb @@ -29,13 +29,11 @@ require 'roar/decorator' require 'roar/json/hal' -require 'api/v3/utilities/date_time_formatter' module API module V3 module WorkPackages class WorkPackageRepresenter < ::API::Decorators::Single - include API::V3::Utilities link :self do { @@ -242,29 +240,31 @@ module API render_nil: true property :start_date, + exec_context: :decorator, getter: -> (*) do - DateTimeFormatter::format_date(start_date, allow_nil: true) + datetime_formatter.format_date(represented.start_date, allow_nil: true) end, render_nil: true property :due_date, + exec_context: :decorator, getter: -> (*) do - DateTimeFormatter::format_date(due_date, allow_nil: true) + datetime_formatter.format_date(represented.due_date, allow_nil: true) end, render_nil: true property :estimated_time, + exec_context: :decorator, getter: -> (*) do - DateTimeFormatter::format_duration_from_hours(represented.estimated_hours, + datetime_formatter.format_duration_from_hours(represented.estimated_hours, allow_nil: true) end, - exec_context: :decorator, render_nil: true, writeable: false property :spent_time, + exec_context: :decorator, getter: -> (*) do - DateTimeFormatter::format_duration_from_hours(represented.spent_hours) + datetime_formatter.format_duration_from_hours(represented.spent_hours) end, writeable: false, - exec_context: :decorator, if: -> (_) { current_user_allowed_to(:view_time_entries) } property :percentage_done, render_nil: true, @@ -279,8 +279,12 @@ module API property :project_id, getter: -> (*) { project.id } property :project_name, getter: -> (*) { project.try(:name) } property :parent_id, writeable: true - property :created_at, getter: -> (*) { DateTimeFormatter::format_datetime(created_at) } - property :updated_at, getter: -> (*) { DateTimeFormatter::format_datetime(updated_at) } + property :created_at, + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.created_at) } + property :updated_at, + exec_context: :decorator, + getter: -> (*) { datetime_formatter.format_datetime(represented.updated_at) } collection :custom_properties, exec_context: :decorator, render_nil: true diff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb index 08f1ac6910..7f49839c4a 100644 --- a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb +++ b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb @@ -27,7 +27,6 @@ #++ require 'spec_helper' -require 'api/v3/utilities/date_time_formatter' describe :DateTimeFormatter do subject { ::API::V3::Utilities::DateTimeFormatter }