Class: Wallaby::Her::ModelDecorator
- Inherits:
-
ModelDecorator
- Object
- ModelDecorator
- Wallaby::Her::ModelDecorator
- Defined in:
- lib/adapters/wallaby/her/model_decorator.rb
Overview
Modal decorator for Her
Constant Summary collapse
- ATTRIBUTE_SUFFIX =
Generally, in Her, attribute methods are generated with this suffix
'_will_change!'
Instance Method Summary collapse
-
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
Origin metadata directly coming from Her.
-
#form_active_errors(resource) ⇒ ActiveModel::Errors, Hash
Errors for resource.
-
#form_field_names ⇒ Array<String>
A list of field names for form (new/edit) page.
-
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for form (new/edit) page.
-
#guess_title(resource) ⇒ String
To guess the title for resource.
-
#index_field_names ⇒ Array<String>
A list of field names for index page.
-
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for index page.
-
#primary_key ⇒ String
Primary key for the resource.
-
#show_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for show page.
Instance Method Details
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
Origin metadata directly coming from Her.
It needs to be frozen so that we can keep the metadata integrity
27 28 29 30 31 32 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 27 def fields @fields ||= ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash| hash.merge! general_fields hash.merge! association_fields end.freeze end |
#form_active_errors(resource) ⇒ ActiveModel::Errors, Hash
Returns errors for resource.
70 71 72 73 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 70 def form_active_errors(resource) resource.errors.presence \ || resource.instance_variable_get(:@response_errors) end |
#form_field_names ⇒ Array<String>
Returns a list of field names for form (new/edit) page.
61 62 63 64 65 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 61 def form_field_names @form_field_names ||= Utils.clone(index_field_names) .delete_if { |field_name| field_name == primary_key.to_s } end |
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for form (new/edit) page
48 49 50 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 48 def form_fields @form_fields ||= Utils.clone fields end |
#guess_title(resource) ⇒ String
To guess the title for resource.
It will go through the fields and try to find out the one that looks like a name or text to represent this resource. Otherwise, it will fall back to primary key.
87 88 89 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 87 def guess_title(resource) ModuleUtils.try_to resource, possible_title_field end |
#index_field_names ⇒ Array<String>
Returns a list of field names for index page.
53 54 55 56 57 58 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 53 def index_field_names @index_field_names ||= index_fields.reject do |_field_name, | [:is_association] end.keys end |
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for index page
36 37 38 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 36 def index_fields @index_fields ||= Utils.clone fields end |
#primary_key ⇒ String
Returns primary key for the resource.
76 77 78 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 76 def primary_key @primary_key ||= @model_class.primary_key end |
#show_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for show page
42 43 44 |
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 42 def show_fields @show_fields ||= Utils.clone fields end |