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=, #form_label_of, #form_metadata_of, #form_type_of, #index_fields=, #index_label_of, #index_metadata_of, #index_type_of, #initialize, #label_of, #metadata_of, #resources_name, #show_fields=, #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
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 32 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
88 89 90 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 88 def form_active_errors(resource) resource.errors end |
#form_field_names ⇒ Array
Fields name for form page
76 77 78 79 80 81 82 83 84 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 76 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
59 60 61 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 59 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.
104 105 106 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 104 def guess_title(resource) resource.public_send title_field_finder.find end |
#index_field_names ⇒ Array
Fields name for index page
65 66 67 68 69 70 71 72 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 65 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
47 48 49 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 47 def index_fields @index_fields ||= Utils.clone fields end |
#primary_key ⇒ String
Primary key for the resource
94 95 96 |
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 94 def primary_key @model_class.primary_key end |