use formatter from member instead of immediately

- allows to DI easier
- more elegant solution around autoloading issues
pull/2521/head
Jan Sandbrink 10 years ago
parent 83a42d82b9
commit 34f357f492
  1. 9
      lib/api/decorators/single.rb
  2. 6
      lib/api/v3/projects/project_representer.rb
  3. 6
      lib/api/v3/users/user_representer.rb
  4. 16
      lib/api/v3/versions/version_representer.rb
  5. 13
      lib/api/v3/work_packages/form/work_package_payload_representer.rb
  6. 22
      lib/api/v3/work_packages/work_package_representer.rb

@ -30,17 +30,12 @@
require 'roar/decorator'
require 'roar/json/hal'
# we need to help the rails autoloader, otherwise it MIGHT fail to resolve the DateTimeFormatter
# during development mode
API::V3::Utilities::DateTimeFormatter
module API
module Decorators
class Single < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::V3::Utilities::PathHelper
include API::V3::Utilities
attr_reader :context
class_attribute :as_strategy
@ -58,6 +53,10 @@ module API
private
def datetime_formatter
API::V3::Utilities::DateTimeFormatter
end
def _type; end
end
end

@ -59,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

@ -87,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

@ -69,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'

@ -37,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
@ -85,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)
},
@ -96,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)
},
@ -119,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

@ -240,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,
@ -277,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

Loading…
Cancel
Save