Class: Wallaby::ModelDecorator
- Inherits:
-
Object
- Object
- Wallaby::ModelDecorator
- Includes:
- FieldHelpers
- Defined in:
- lib/interfaces/wallaby/model_decorator.rb,
lib/interfaces/wallaby/model_decorator/field_helpers.rb
Overview
Model Decorator interface, designed to maintain metadata for all the fields coming from data source (database/api)
Direct Known Subclasses
Defined Under Namespace
Modules: FieldHelpers
Instance Attribute Summary collapse
-
#field_names ⇒ Array<String>, Array<Symbol>
A list of field names.
-
#form_field_names ⇒ Array<String>, Array<Symbol>
A list field names displayed on form (new/edit) page.
-
#index_field_names ⇒ Array<String>, Array<Symbol>
A list of field names displayed on index page.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#show_field_names ⇒ Array<String>, Array<Symbol>
A list of field names displayed on show page.
Instance Method Summary collapse
-
#fields ⇒ Hash
Origin metadata for fields.
-
#fields=(fields) ⇒ Object
Set metadata as origin.
-
#filters ⇒ Hash
Metadata for custom filters used on index page.
-
#form_active_errors(_resource) ⇒ Hash
Validation errors for current resource.
-
#form_fields ⇒ Hash
Metadata for fields used on form (new/edit) page.
-
#form_fields=(fields) ⇒ Object
Set metadata for form (new/edit) page.
-
#guess_title(_resource) ⇒ String
To guess the title for a resource.
-
#index_fields ⇒ Hash
Metadata for fields used on index page.
-
#index_fields=(fields) ⇒ Object
Set metadata for index page.
-
#initialize(model_class) ⇒ ModelDecorator
constructor
Initialize with model class.
-
#primary_key ⇒ String
Primary key.
-
#resources_name ⇒ String
Resources name coverted from model class.
-
#show_fields ⇒ Hash
Metadata for fields used on show page.
-
#show_fields=(fields) ⇒ Object
Set metadata for show page.
Methods included from FieldHelpers
#form_label_of, #form_metadata_of, #form_type_of, #index_label_of, #index_metadata_of, #index_type_of, #label_of, #metadata_of, #show_label_of, #show_metadata_of, #show_type_of, #type_of
Constructor Details
#initialize(model_class) ⇒ ModelDecorator
Initialize with model class
15 16 17 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 15 def initialize(model_class) @model_class = model_class end |
Instance Attribute Details
#field_names ⇒ Array<String>, Array<Symbol>
Returns a list of field names.
31 32 33 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 31 def field_names @field_names ||= reposition fields.keys, primary_key end |
#form_field_names ⇒ Array<String>, Array<Symbol>
Returns a list field names displayed on form (new/edit) page.
82 83 84 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 82 def form_field_names @form_field_names ||= reposition form_fields.keys, primary_key end |
#index_field_names ⇒ Array<String>, Array<Symbol>
Returns a list of field names displayed on index page.
48 49 50 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 48 def index_field_names @index_field_names ||= reposition index_fields.keys, primary_key end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
9 10 11 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 9 def model_class @model_class end |
#show_field_names ⇒ Array<String>, Array<Symbol>
Returns a list of field names displayed on show page.
65 66 67 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 65 def show_field_names @show_field_names ||= reposition show_fields.keys, primary_key end |
Instance Method Details
#fields ⇒ Hash
Returns origin metadata for fields.
20 21 22 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 20 def fields raise NotImplemented end |
#fields=(fields) ⇒ Object
Set metadata as origin
26 27 28 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 26 def fields=(fields) @fields = ::ActiveSupport::HashWithIndifferentAccess.new fields end |
#filters ⇒ Hash
Returns metadata for custom filters used on index page.
87 88 89 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 87 def filters @filters ||= ::ActiveSupport::HashWithIndifferentAccess.new end |
#form_active_errors(_resource) ⇒ Hash
Returns validation errors for current resource.
92 93 94 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 92 def form_active_errors(_resource) raise NotImplemented end |
#form_fields ⇒ Hash
Returns metadata for fields used on form (new/edit) page.
70 71 72 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 70 def form_fields raise NotImplemented end |
#form_fields=(fields) ⇒ Object
Set metadata for form (new/edit) page
76 77 78 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 76 def form_fields=(fields) @form_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields end |
#guess_title(_resource) ⇒ String
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
109 110 111 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 109 def guess_title(_resource) raise NotImplemented end |
#index_fields ⇒ Hash
Returns metadata for fields used on index page.
36 37 38 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 36 def index_fields raise NotImplemented end |
#index_fields=(fields) ⇒ Object
Set metadata for index page
42 43 44 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 42 def index_fields=(fields) @index_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields end |
#primary_key ⇒ String
Returns primary key.
97 98 99 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 97 def primary_key raise NotImplemented end |
#resources_name ⇒ String
Returns resources name coverted from model class.
114 115 116 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 114 def resources_name Map.resources_name_map @model_class end |
#show_fields ⇒ Hash
Returns metadata for fields used on show page.
53 54 55 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 53 def show_fields raise NotImplemented end |
#show_fields=(fields) ⇒ Object
Set metadata for show page
59 60 61 |
# File 'lib/interfaces/wallaby/model_decorator.rb', line 59 def show_fields=(fields) @show_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields end |