module Wallaby::Decoratable

Decorator related attributes

Public Instance Methods

current_decorator() click to toggle source

Get current resource decorator. It comes from

  • {Wallaby::Decoratable::ClassMethods#resource_decorator resource_decorator}

  • otherwise, {Wallaby::Decoratable::ClassMethods#application_decorator application_decorator}

@return [Wallaby::ResourceDecorator] current resource decorator for this request

# File lib/concerns/wallaby/decoratable.rb, line 67
def current_decorator
  @current_decorator ||=
    (controller_to_get(:resource_decorator) || \
    Map.resource_decorator_map(current_model_class, controller_to_get(:application_decorator))).tap do |decorator|
      Logger.debug %(Current decorator: #{decorator}), sourcing: false
    end
end
current_fields() click to toggle source

Get current fields metadata for current action name. @return [Hash] current fields metadata

# File lib/concerns/wallaby/decoratable.rb, line 77
def current_fields
  @current_fields ||=
    ModuleUtils.try_to current_model_decorator, :"#{FORM_ACTIONS[action_name] || action_name}_fields"
end
current_model_decorator() click to toggle source

Get current model decorator. It comes from

  • model decorator for {Wallaby::Decoratable::ClassMethods#resource_decorator resource_decorator}

  • otherwise, model decorator for {Wallaby::Decoratable::ClassMethods#application_decorator application_decorator}

Model decorator stores the information of metadata and field_names for index/*show*/*form* action. @return [Wallaby::ModelDecorator] current model decorator for this request

# File lib/concerns/wallaby/decoratable.rb, line 56
def current_model_decorator
  @current_model_decorator ||=
    current_decorator.try(:model_decorator) || \
    Map.model_decorator_map(current_model_class, controller_to_get(:application_decorator))
end
decorate(resource) click to toggle source

Wrap resource(s) with decorator(s). @param resource [Object, Enumerable] @return [Wallaby::ResourceDecorator, Enumerable<Wallaby::ResourceDecorator>] decorator(s)

# File lib/concerns/wallaby/decoratable.rb, line 85
def decorate(resource)
  return resource if resource.is_a? ResourceDecorator
  return resource.map { |item| decorate item } if resource.respond_to? :map

  decorator = Map.resource_decorator_map resource.class, controller_to_get(:application_decorator)
  decorator ? decorator.new(resource) : resource
end
extract(resource) click to toggle source

@param resource [Object, Wallaby::ResourceDecorator] @return [Object] the unwrapped resource object

# File lib/concerns/wallaby/decoratable.rb, line 95
def extract(resource)
  return resource.resource if resource.is_a? ResourceDecorator

  resource
end