Class: Wallaby::AbstractResourceDecorator
- Inherits:
-
Object
- Object
- Wallaby::AbstractResourceDecorator
- Defined in:
- lib/decorators/wallaby/abstract_resource_decorator.rb
Overview
Resource Decorator base class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#model_decorator ⇒ Object
readonly
Returns the value of attribute model_decorator.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
-
.model_class ⇒ Class
Guess the model class from class name.
-
.model_decorator ⇒ Wallaby::ModelDecorator
Get the model decorator for the model class It should be the same as #model_decorator.
Instance Method Summary collapse
-
#errors ⇒ Hash, Array
Return the validation errors.
-
#initialize(resource) ⇒ AbstractResourceDecorator
constructor
A new instance of AbstractResourceDecorator.
-
#method_missing(method_id, *args) ⇒ Object
We delegate missing methods to resource.
-
#model_class ⇒ Class
Resource’s class.
-
#primary_key_value ⇒ Object
Primary key value.
- #respond_to_missing?(method_id, _include_private) ⇒ Boolean
-
#to_label ⇒ String
Guess the title for given resource It falls back to primary key value when no text field is found ‘.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.
Constructor Details
#initialize(resource) ⇒ AbstractResourceDecorator
Returns a new instance of AbstractResourceDecorator.
29 30 31 32 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 29 def initialize(resource) @resource = resource @model_decorator = Map.model_decorator_map model_class end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
We delegate missing methods to resource
68 69 70 71 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 68 def method_missing(method_id, *args) return super unless @resource.respond_to? method_id @resource.public_send method_id, *args end |
Instance Attribute Details
#model_decorator ⇒ Object (readonly)
Returns the value of attribute model_decorator.
26 27 28 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 26 def model_decorator @model_decorator end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
26 27 28 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 26 def resource @resource end |
Class Method Details
.model_class ⇒ Class
Guess the model class from class name
7 8 9 10 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 7 def model_class return unless self < ::Wallaby.configuration.mapping.resource_decorator Map.model_class_map name.gsub('Decorator', EMPTY_STRING) end |
.model_decorator ⇒ Wallaby::ModelDecorator
Get the model decorator for the model class It should be the same as #model_decorator
15 16 17 18 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 15 def model_decorator return unless self < ::Wallaby.configuration.mapping.resource_decorator Map.model_decorator_map model_class end |
Instance Method Details
#errors ⇒ Hash, Array
Return the validation errors
50 51 52 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 50 def errors @model_decorator.form_active_errors(@resource) end |
#model_class ⇒ Class
Returns resource’s class.
35 36 37 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 35 def model_class @resource.class end |
#primary_key_value ⇒ Object
Returns primary key value.
55 56 57 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 55 def primary_key_value @resource.public_send primary_key end |
#respond_to_missing?(method_id, _include_private) ⇒ Boolean
75 76 77 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 75 def respond_to_missing?(method_id, _include_private) @resource.respond_to?(method_id) || super end |
#to_label ⇒ String
Guess the title for given resource It falls back to primary key value when no text field is found ‘.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.
44 45 46 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 44 def to_label (@model_decorator.guess_title(@resource) || primary_key_value).to_s end |