Module: Wallaby::Decoratable

Included in:
ResourcesConcern, ResourcesHelper
Defined in:
lib/concerns/wallaby/decoratable.rb

Overview

Decorator related

Instance Method Summary collapse

Instance Method Details

#current_decoratorResourceDecorator

Get current resource decorator. It comes from

Returns:



24
25
26
27
28
29
# File 'lib/concerns/wallaby/decoratable.rb', line 24

def current_decorator
  @current_decorator ||=
    decorator_of(current_model_class).tap do |decorator|
      Logger.debug %(Current decorator: #{decorator}), sourcing: false
    end
end

#current_fieldsHash

Get current fields metadata for current action name.

Returns:

  • (Hash)

    current fields metadata



33
34
35
36
# File 'lib/concerns/wallaby/decoratable.rb', line 33

def current_fields
  @current_fields ||=
    current_model_decorator.try(:"#{action_name}_fields")
end

#current_model_decoratorModelDecorator

Get current model decorator. It comes from

Model decorator stores the information of metadata and field_names for index/show/form action.

Returns:



13
14
15
16
17
# File 'lib/concerns/wallaby/decoratable.rb', line 13

def current_model_decorator
  @current_model_decorator ||=
    current_decorator.try(:model_decorator) || \
    Map.model_decorator_map(current_model_class, wallaby_controller.application_decorator)
end

#decorate(resource) ⇒ ResourceDecorator, Enumerable<Wallaby::ResourceDecorator>

Wrap resource(s) with decorator(s).

Parameters:

  • resource (Object, Enumerable)

Returns:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/concerns/wallaby/decoratable.rb', line 50

def decorate(resource)
  return resource if resource.is_a?(ResourceDecorator)
  return resource.map { |item| decorate(item) } if resource.respond_to?(:map)
  return resource unless Map.mode_map[resource.class]

  DecoratorFinder.new(
    script_name: script_name,
    model_class: resource.class,
    current_controller_class: wallaby_controller
  ).execute.new(resource)
end

#decorator_of(klass) ⇒ Object

Get the decorator of a klass



39
40
41
42
43
44
45
# File 'lib/concerns/wallaby/decoratable.rb', line 39

def decorator_of(klass)
  DecoratorFinder.new(
    script_name: script_name,
    model_class: klass,
    current_controller_class: wallaby_controller
  ).execute
end

#extract(resource) ⇒ Object

Returns the unwrapped resource object.

Parameters:

Returns:

  • (Object)

    the unwrapped resource object



64
65
66
67
68
# File 'lib/concerns/wallaby/decoratable.rb', line 64

def extract(resource)
  return resource.resource if resource.is_a?(ResourceDecorator)

  resource
end