Class: Wallaby::Custom::ModelDecorator
- Inherits:
-
ModelDecorator
- Object
- ModelDecorator
- Wallaby::Custom::ModelDecorator
- Defined in:
- lib/adaptors/wallaby/custom/model_decorator.rb
Overview
Wallaby::Custom mode decorator that only pulls out all the attributes from setter/getter pair methods.
Constant Summary
Constants inherited from ModelDecorator
ModelDecorator::MISSING_METHODS_RELATED_TO_FIELDS
Instance Attribute Summary
Attributes inherited from ModelDecorator
#all_fields, #filters, #form_field_names, #model_class
Instance Method Summary collapse
-
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
Retrieve the attributes from the setter/getter pair methods, e.g.
-
#form_active_errors(resource) ⇒ ActiveModel::Errors
It returns an ActiveModel::Errors instance.
-
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for form (new/edit) page.
- #guess_title(resource) ⇒ String?
-
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for index page.
-
#primary_key ⇒ String, Symbole
Default to ‘:id`.
-
#show_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for show page.
Methods inherited from ModelDecorator
#initialize, #method_missing, #resources_name, #respond_to_missing?
Methods included from Fieldable
#label_of, #metadata_of, #prefix_field_names, #prefix_field_names=, #prefix_fields, #prefix_fields=, #type_of
Constructor Details
This class inherits a constructor from Wallaby::ModelDecorator
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Wallaby::ModelDecorator
Instance Method Details
#fields ⇒ ActiveSupport::HashWithIndifferentAccess
Retrieve the attributes from the setter/getter pair methods, e.g. ‘name=` and `name`
9 10 11 12 13 14 15 16 17 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 9 def fields @fields ||= ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash| methods = model_class.public_instance_methods.map(&:to_s) methods .grep(/(\A[^!=]|[^!=]\Z)/).select { |method_id| methods.include? "#{method_id}=" } .each { |attribute| hash[attribute] = { label: attribute.humanize, type: 'string' } } end.freeze end |
#form_active_errors(resource) ⇒ ActiveModel::Errors
It returns an ActiveModel::Errors instance. However, this instance does not contain any errors. You might want to override this method in the custom resource decorator and get the errors out from the given resource.
42 43 44 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 42 def form_active_errors(resource) @form_active_errors ||= ActiveModel::Errors.new resource end |
#form_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for form (new/edit) page
33 34 35 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 33 def form_fields @form_fields ||= Utils.clone fields end |
#guess_title(resource) ⇒ String?
53 54 55 56 57 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 53 def guess_title(resource) FieldUtils .first_field_by({ name: /name|title|subject/ }, fields) .try { |field_name| resource.try field_name } end |
#index_fields ⇒ ActiveSupport::HashWithIndifferentAccess
A copy of #fields for index page
21 22 23 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 21 def index_fields @index_fields ||= Utils.clone fields end |
#primary_key ⇒ String, Symbole
Returns default to ‘:id`.
47 48 49 |
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 47 def primary_key @primary_key ||= fields.key?(:id) ? :id : fields.keys.first.try(:to_sym) end |