Class: Wallaby::ActiveRecord::ModelDecorator

Inherits:
ModelDecorator show all
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

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

#fieldsHash

Origin metadata coming from data source. It needs to be frozen so that we can keep the metadata integrity

Returns:

  • (Hash)

    example:

    {
      # general field
      id: { name: 'id', type: 'integer', label: 'Id', is_origin: true },
      # association field
      category: {
        'name' => 'category',
        'type' => 'belongs_to',
        'label' => 'Category',
        'is_origin' => true,
        'is_association' => true,
        'is_through' => false,
        'has_scope' => false,
        'foreign_key' => 'category_id',
        'class' => Category
      }
    }
    


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

Returns:

  • (ActiveModel::Errors)


88
89
90
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 88

def form_active_errors(resource)
  resource.errors
end

#form_field_namesArray

Fields name for form page

Returns:

  • (Array)


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_fieldsHash

A copy of all the fields for form page

Returns:

  • (Hash)


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.

Parameters:

  • resource (Object)

Returns:

  • (String)

    the title of given resource



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_namesArray

Fields name for index page

Returns:

  • (Array)


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_fieldsHash

A copy of all the fields for index page

Returns:

  • (Hash)


47
48
49
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 47

def index_fields
  @index_fields ||= Utils.clone fields
end

#primary_keyString

Primary key for the resource

Returns:

  • (String)


94
95
96
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 94

def primary_key
  @model_class.primary_key
end

#show_fieldsHash

A copy of all the fields for show page

Returns:

  • (Hash)


53
54
55
# File 'lib/adaptors/wallaby/active_record/model_decorator.rb', line 53

def show_fields
  @show_fields  ||= Utils.clone fields
end