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 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

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

#fieldsHash

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

Returns:

  • (Hash)

    metadata example:

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


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.

Returns:

  • (ActiveModel::Errors)

    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_namesArray<String>

Returns a list of field names for form (new/edit) page.

Returns:

  • (Array<String>)

    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_fieldsHash

A copy of ‘fields` for form (new/edit) page

Returns:

  • (Hash)

    metadata



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.

Parameters:

  • resource (Object)

Returns:

  • (String)

    the title of given resource



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_namesArray<String>

Returns a list of field names for index page.

Returns:

  • (Array<String>)

    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_fieldsHash

A copy of ‘fields` for index page

Returns:

  • (Hash)

    metadata



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

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

#primary_keyString

Returns primary key for the resource.

Returns:

  • (String)

    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_fieldsHash

A copy of ‘fields` for show page

Returns:

  • (Hash)

    metadata



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

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