Module: Wallaby::Decoratable
- Included in:
- ResourcesConcern, ResourcesHelper
- Defined in:
- lib/concerns/wallaby/decoratable.rb
Overview
Decorator related
Instance Method Summary collapse
-
#current_decorator ⇒ ResourceDecorator
Get current resource decorator.
-
#current_fields ⇒ Hash
Get current fields metadata for current action name.
-
#current_model_decorator ⇒ ModelDecorator
Get current model decorator.
-
#decorate(resource) ⇒ ResourceDecorator, Enumerable<Wallaby::ResourceDecorator>
Wrap resource(s) with decorator(s).
-
#decorator_of(klass) ⇒ Object
Get the decorator of a klass.
-
#extract(resource) ⇒ Object
The unwrapped resource object.
Instance Method Details
#current_decorator ⇒ ResourceDecorator
Get current resource decorator. It comes from
-
otherwise, application_decorator
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_fields ⇒ Hash
Get current fields metadata for current action name.
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_decorator ⇒ ModelDecorator
Get current model decorator. It comes from
-
model decorator for resource_decorator
-
otherwise, model decorator for application_decorator
Model decorator stores the information of metadata and field_names for index/show/form action.
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).
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.
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 |