Class: Wallaby::ActiveRecord::ModelDecorator
- Inherits:
-
ModelDecorator
- Object
- ModelDecorator
- Wallaby::ActiveRecord::ModelDecorator
- Defined in:
- lib/adaptors/wallaby/active_record/model_decorator.rb,
lib/adaptors/wallaby/active_record/model_decorator/fields_builder.rb,
lib/adaptors/wallaby/active_record/model_decorator/title_field_finder.rb,
lib/adaptors/wallaby/active_record/model_decorator/fields_builder/sti_builder.rb,
lib/adaptors/wallaby/active_record/model_decorator/fields_builder/association_builder.rb,
lib/adaptors/wallaby/active_record/model_decorator/fields_builder/polymorphic_builder.rb
Overview
Modal decorator for ActiveRecord
Defined Under Namespace
Classes: FieldsBuilder, TitleFieldFinder
Constant Summary collapse
- INDEX_EXCLUSIVE_DATA_TYPES =
Data types to exclude for index page
%w(binary citext hstore json jsonb text tsvector xml).freeze
- FORM_EXCLUSIVE_DATA_TYPES =
Data types to exclude for form page
%w(created_at updated_at).freeze
Instance Attribute Summary
Attributes inherited from ModelDecorator
#field_names, #model_class, #show_field_names
Instance Method Summary collapse
-
#fields ⇒ Hash
Origin metadata directly coming from ActiveRecord.
-
#form_active_errors(resource) ⇒ ActiveModel::Errors
Errors for resource.
-
#form_field_names ⇒ Array<String>
A list of field names for form (new/edit) page.
-
#form_fields ⇒ Hash
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 ⇒ Hash
A copy of ‘fields` for index page.
-
#primary_key ⇒ String
Primary key for the resource.
-
#show_fields ⇒ Hash
A copy of ‘fields` for show page.
Methods inherited from ModelDecorator
#fields=, #filters, #form_fields=, #index_fields=, #initialize, #resources_name, #show_fields=
Methods included from ModelDecorator::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
This class inherits a constructor from Wallaby::ModelDecorator
Instance Method Details
#fields ⇒ Hash
Origin metadata directly coming from ActiveRecord. It needs to be frozen so that we can keep the metadata integrity
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 31 def fields @fields ||= ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash| # NOTE: There is a chance that people create ActiveRecord class # before they do the migration, so initialising the fields will raise # all kinds of error. Therefore, we need to check the table existence if @model_class.table_exists? hash.merge! general_fields hash.merge! association_fields hash.except!(*foreign_keys_from_associations) end end.freeze end |
#form_active_errors(resource) ⇒ ActiveModel::Errors
Returns errors for resource.
84 85 86 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 84 def form_active_errors(resource) resource.errors end |
#form_field_names ⇒ Array<String>
Returns a list of field names for form (new/edit) page.
73 74 75 76 77 78 79 80 81 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 73 def form_field_names @form_field_names ||= begin form_fields.reject do |field_name, | field_name == primary_key \ || FORM_EXCLUSIVE_DATA_TYPES.include?(field_name) \ || [:has_scope] || [:is_through] end.keys end end |
#form_fields ⇒ Hash
A copy of ‘fields` for form (new/edit) page
58 59 60 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 58 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.
101 102 103 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 101 def guess_title(resource) resource.public_send title_field_finder.find end |
#index_field_names ⇒ Array<String>
Returns a list of field names for index page.
63 64 65 66 67 68 69 70 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 63 def index_field_names @index_field_names ||= begin index_fields.reject do |_field_name, | [:is_association] \ || INDEX_EXCLUSIVE_DATA_TYPES.include?([:type]) end.keys end end |
#index_fields ⇒ Hash
A copy of ‘fields` for index page
46 47 48 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 46 def index_fields @index_fields ||= Utils.clone fields end |
#primary_key ⇒ String
Returns primary key for the resource.
89 90 91 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 89 def primary_key @model_class.primary_key end |
#show_fields ⇒ Hash
A copy of ‘fields` for show page
52 53 54 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 52 def show_fields @show_fields ||= Utils.clone fields end |