# File lib/decorators/wallaby/resource_decorator.rb, line 134 def primary_key_value resource.try primary_key end
class Wallaby::ResourceDecorator
Decorator base class. It's designed to be used as the decorator (AKA presenter/view object) for the associated model instance (which means it should be used in the views only).
And it holds the following metadata information for associated model class:
-
{#fields}
-
{#field_names}
-
{#index_fields}
-
{#index_field_names}
-
{#show_fields}
-
{#show_field_names}
-
{#form_fields}
-
{#form_field_names}
For better practice, please create an application decorator class (see example) to better control the functions shared between different resource decorators. @example Create an application class for Admin Interface usage
class Admin::ApplicationDecorator < Wallaby::ResourceDecorator base_class! end
Constants
- DELEGATE_METHODS
@!attribute form_field_names
(see Wallaby::ModelDecorator#form_field_names)
Attributes
@!attribute [w] h
@!attribute [r] model_decorator
@return [Wallaby::ModelDecorator]
@!attribute [r] resource @return [Object]
Public Class Methods
@!attribute [r] h @return [ActionView::Base]
{Wallaby::Configuration::Mapping#resources_controller resources controller}'s helpers
# File lib/decorators/wallaby/resource_decorator.rb, line 75 def h @h ||= Wallaby.configuration.mapping.resources_controller.helpers end
Return associated model decorator. It is the instance that pull out all the metadata information for the associated model. @param model_class
[Class] @return [Wallaby::ModelDecorator] @return [nil] if itself is a base class or the given model_class
is blank
# File lib/decorators/wallaby/resource_decorator.rb, line 63 def model_decorator(model_class = self.model_class) return if model_class.blank? Map.model_decorator_map model_class, base_class end
@param resource [Object]
# File lib/decorators/wallaby/resource_decorator.rb, line 100 def initialize(resource) @resource = resource @model_decorator = self.class.model_decorator(model_class) end
Public Instance Methods
@return [Hash, Array] validation/result errors
# File lib/decorators/wallaby/resource_decorator.rb, line 129 def errors model_decorator.form_active_errors(resource) end
@return [ActionView::Base]
{Wallaby::Configuration::Mapping#resources_controller resources controller}'s helpers
@see .h
# File lib/decorators/wallaby/resource_decorator.rb, line 91 def h self.class.h end
Delegate missing method to {#resource}
# File lib/decorators/wallaby/resource_decorator.rb, line 151 def method_missing(method_id, *args, &block) return super unless resource.respond_to? method_id resource.try method_id, *args, &block end
@return [Class] resource's class
# File lib/decorators/wallaby/resource_decorator.rb, line 106 def model_class resource.class end
@return [ActiveModel::Name]
# File lib/decorators/wallaby/resource_decorator.rb, line 139 def model_name resource.try(:model_name) || ActiveModel::Name.new(model_class) end
@return [Object] primary key value
Delegate missing method check to {#resource}
# File lib/decorators/wallaby/resource_decorator.rb, line 158 def respond_to_missing?(method_id, _include_private) resource.respond_to?(method_id) || super end
@return [nil] if no primary key @return [Array<String>] primary key
# File lib/decorators/wallaby/resource_decorator.rb, line 145 def to_key key = resource.try primary_key key ? [key] : nil end
Guess the title for given resource.
It falls back to primary key value when no text field is found. @return [String] a label
# File lib/decorators/wallaby/resource_decorator.rb, line 122 def to_label # NOTE: `.to_s` at the end is to ensure String is returned that won't cause any # issue when `#to_label` is used in a link_to block. Coz integer is ignored. (model_decorator.guess_title(resource) || primary_key_value).to_s end
@param field_name [String, Symbol] @return [Object] value of given field name
# File lib/decorators/wallaby/resource_decorator.rb, line 112 def value_of(field_name) return unless field_name resource.try field_name end