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 ⇒ Object
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.
28 29 30 31 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 28 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
65 66 67 68 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 65 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::ResourceDecorator Map.model_class_map name.gsub 'Decorator', EMPTY_STRING end |
.model_decorator ⇒ Object
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::ResourceDecorator Map.model_decorator_map model_class end |
Instance Method Details
#errors ⇒ Hash, Array
Return the validation errors
49 50 51 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 49 def errors @model_decorator.form_active_errors(@resource) end |
#model_class ⇒ Class
Returns resource’s class.
34 35 36 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 34 def model_class @resource.class end |
#primary_key_value ⇒ Object
Returns primary key value.
54 55 56 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 54 def primary_key_value @resource.public_send primary_key end |
#respond_to_missing?(method_id, _include_private) ⇒ Boolean
70 71 72 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 70 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.
43 44 45 |
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 43 def to_label (@model_decorator.guess_title(@resource) || primary_key_value).to_s end |