Class: Wallaby::ActiveRecord::ModelDecorator::FieldsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb,
lib/adapters/wallaby/active_record/model_decorator/fields_builder/sti_builder.rb,
lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb,
lib/adapters/wallaby/active_record/model_decorator/fields_builder/polymorphic_builder.rb

Overview

To build the metadata for fields

Defined Under Namespace

Classes: AssociationBuilder, PolymorphicBuilder, StiBuilder

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ FieldsBuilder

Returns a new instance of FieldsBuilder.

Parameters:

  • model_class (Class)


9
10
11
# File 'lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb', line 9

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#association_fieldsHash<String, Hash>

Returns a hash for association fields (e.g. belongs_to / has_one / has_many / has_and_belongs_to_many).

Returns:

  • (Hash<String, Hash>)

    a hash for association fields (e.g. belongs_to / has_one / has_many / has_and_belongs_to_many)



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb', line 28

def association_fields
  @association_fields ||=
    @model_class.reflections.each_with_object({}) do |(name, reflection), fields|
       = {
        type: reflection.macro.to_s, # association type
        label: @model_class.human_attribute_name(name)
      }
      association_builder.update(, reflection)
      polymorphic_builder.update(, reflection)
      update_general_fields_with()
      fields[name] = 
    end
end

#general_fieldsHash<String, Hash>

Returns a hash for general fields.

Returns:

  • (Hash<String, Hash>)

    a hash for general fields



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb', line 14

def general_fields
  @general_fields ||=
    @model_class.columns.each_with_object({}) do |column, fields|
       = {
        type: to_type(column).freeze,
        label: @model_class.human_attribute_name(column.name)
      }
      sti_builder.update(, column)
      fields[column.name] = 
    end
end