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 Active Record
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 coming from data source.
-
#form_active_errors(resource) ⇒ ActiveModel::Errors
Errors for resource.
-
#form_field_names ⇒ Array
Fields name for form page.
-
#form_fields ⇒ Hash
A copy of all the fields for form page.
-
#guess_title(resource) ⇒ String
To guess the title for resource.
-
#index_field_names ⇒ Array
Fields name for index page.
-
#index_fields ⇒ Hash
A copy of all the fields for index page.
-
#primary_key ⇒ String
Primary key for the resource.
-
#show_fields ⇒ Hash
A copy of all the 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 coming from data source. 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 Active Record 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
Errors for resource
87 88 89 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 87 def form_active_errors(resource) resource.errors end |
#form_field_names ⇒ Array
Fields name for form page
75 76 77 78 79 80 81 82 83 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 75 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 all the fields for form 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.
103 104 105 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 103 def guess_title(resource) resource.public_send title_field_finder.find end |
#index_field_names ⇒ Array
Fields name for index page
64 65 66 67 68 69 70 71 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 64 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 all the 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
Primary key for the resource
93 94 95 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 93 def primary_key @model_class.primary_key end |