Class: Wallaby::ModelDecorator
- Inherits:
-
Object
- Object
- Wallaby::ModelDecorator
- Includes:
- Fieldable
- Defined in:
- lib/interfaces/wallaby/model_decorator.rb
Overview
Model Decorator interface, designed to hold and maintain metadata for all fields from the data source (database/api)
Direct Known Subclasses
Constant Summary collapse
- MISSING_METHODS_RELATED_TO_FIELDS =
/\A(?<method_prefix>[a-zA-Z]\w*_)(?<method_suffix>fields=?|field_names=?|metadata_of|label_of|type_of)\Z/.freeze
Instance Attribute Summary collapse
- #all_fields ⇒ Object readonly
-
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
Origin fields metadata.
-
#filters ⇒ ActiveSupport::HashWithIndifferentAccess
readonly
Filter metadata for index page.
-
#form_field_names ⇒ Array<String, Symbol>
readonly
A list of field names for form (‘new`/`edit`) page.
-
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess
readonly
Fields metadata for form (‘new`/`edit`) page.
-
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess
readonly
Fields metadata for ‘index` page.
- #model_class ⇒ Class
-
#primary_key ⇒ String
Primary key name.
-
#show_fields ⇒ ActiveSupport::HashWithIndifferentAccess
readonly
Fields metadata for ‘show` page.
Instance Method Summary collapse
-
#form_active_errors(_resource) ⇒ ActiveModel::Errors, Hash
Validation error for given resource.
-
#guess_title(_resource) ⇒ String
To guess the title for a resource.
-
#initialize(model_class) ⇒ ModelDecorator
constructor
Initialize with model class.
-
#method_missing(method_id, *args, &block) ⇒ Object
Delegate missing methods to Fieldable if possible.
- #resources_name ⇒ String
-
#respond_to_missing?(method_id, _include_private) ⇒ Boolean
Check if method looks like: ‘_fields`, `_field_names` that can be processed by Fieldable.
Methods included from Fieldable
#label_of, #metadata_of, #prefix_field_names, #prefix_field_names=, #prefix_fields, #prefix_fields=, #type_of
Constructor Details
#initialize(model_class) ⇒ ModelDecorator
Initialize with model class
14 15 16 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 14 def initialize(model_class) @model_class = model_class end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args, &block) ⇒ Object
Delegate missing methods to Fieldable if possible
121 122 123 124 125 126 127 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 121 def method_missing(method_id, *args, &block) method_name = method_id.to_s return super unless method_name.match?(MISSING_METHODS_RELATED_TO_FIELDS) matched = MISSING_METHODS_RELATED_TO_FIELDS.match(method_name) try("prefix_#{matched[:method_suffix]}", *args, matched[:method_prefix], &block) end |
Instance Attribute Details
#all_fields ⇒ Object (readonly)
24 25 26 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 24 def all_fields AllFields.new self end |
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
to be implemented in subclasses.
Origin fields metadata.
Initially, #index_fields, #show_fields and #form_fields are copies of it.
34 35 36 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 34 def fields raise NotImplemented end |
#filters ⇒ ActiveSupport::HashWithIndifferentAccess (readonly)
to be implemented in subclasses.
Filter metadata for index page.
78 79 80 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 78 def filters @filters ||= ::ActiveSupport::HashWithIndifferentAccess.new end |
#form_field_names ⇒ Array<String, Symbol> (readonly)
A list of field names for form (‘new`/`edit`) page
70 71 72 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 70 def form_field_names @form_field_names ||= form_fields.keys - [primary_key.to_s] end |
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess (readonly)
to be implemented in subclasses.
Fields metadata for form (‘new`/`edit`) page.
63 64 65 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 63 def form_fields raise NotImplemented end |
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess (readonly)
to be implemented in subclasses.
Fields metadata for ‘index` page.
47 48 49 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 47 def index_fields raise NotImplemented end |
#model_class ⇒ Class
20 21 22 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 20 def model_class @model_class end |
#primary_key ⇒ String
to be implemented in subclasses.
Returns primary key name.
96 97 98 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 96 def primary_key raise NotImplemented end |
#show_fields ⇒ ActiveSupport::HashWithIndifferentAccess (readonly)
to be implemented in subclasses.
Fields metadata for ‘show` page.
55 56 57 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 55 def show_fields raise NotImplemented end |
Instance Method Details
#form_active_errors(_resource) ⇒ ActiveModel::Errors, Hash
to be implemented in subclasses.
Validation error for given resource
86 87 88 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 86 def form_active_errors(_resource) raise NotImplemented end |
#guess_title(_resource) ⇒ String
to be implemented in subclasses.
To guess the title for a resource.
This title will be used on the following places:
-
page title on show page
-
in the response for autocomplete association field
-
title of show link for a resource
110 111 112 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 110 def guess_title(_resource) raise NotImplemented end |
#resources_name ⇒ String
116 117 118 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 116 def resources_name Map.resources_name_map model_class end |
#respond_to_missing?(method_id, _include_private) ⇒ Boolean
Check if method looks like: ‘_fields`, `_field_names` that can be processed by Fieldable
130 131 132 133 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 130 def respond_to_missing?(method_id, _include_private) method_name = method_id.to_s method_name.match?(MISSING_METHODS_RELATED_TO_FIELDS) || super end |