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:

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

h[W]

@!attribute [w] h

model_decorator[R]

@!attribute [r] model_decorator @return [Wallaby::ModelDecorator]

resource[R]

@!attribute [r] resource @return [Object]

Public Class Methods

h() click to toggle source

@!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
model_decorator(model_class = self.model_class) click to toggle source

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
new(resource) click to toggle source

@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

errors() click to toggle source

@return [Hash, Array] validation/result errors

# File lib/decorators/wallaby/resource_decorator.rb, line 129
def errors
  model_decorator.form_active_errors(resource)
end
h() click to toggle source

@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
method_missing(method_id, *args, &block) click to toggle source

Delegate missing method to {#resource}

Calls superclass method
# 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
model_class() click to toggle source

@return [Class] resource's class

# File lib/decorators/wallaby/resource_decorator.rb, line 106
def model_class
  resource.class
end
model_name() click to toggle source

@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
primary_key_value() click to toggle source

@return [Object] primary key value

# File lib/decorators/wallaby/resource_decorator.rb, line 134
def primary_key_value
  resource.try primary_key
end
respond_to_missing?(method_id, _include_private) click to toggle source

Delegate missing method check to {#resource}

Calls superclass method
# File lib/decorators/wallaby/resource_decorator.rb, line 158
def respond_to_missing?(method_id, _include_private)
  resource.respond_to?(method_id) || super
end
to_key() click to toggle source

@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
to_label() click to toggle source

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
value_of(field_name) click to toggle source

@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