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

Inherits:
Object
  • Object
show all
Defined in:
lib/adaptors/wallaby/active_record/model_decorator/fields_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ FieldsBuilder

Returns a new instance of FieldsBuilder.



2
3
4
# File 'lib/adaptors/wallaby/active_record/model_decorator/fields_builder.rb', line 2

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#association_fieldsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adaptors/wallaby/active_record/model_decorator/fields_builder.rb', line 18

def association_fields
  @model_class.reflections.inject({}) do |fields, (field_name, reflection)|
    type = extract_type_from reflection

    fields[field_name] = {
      name:             field_name,
      type:             type,
      label:            field_name.titleize,
      is_origin:        true,
      is_association:   true,
      is_polymorphic:   is_polymorphic?(reflection),
      is_through:       is_through?(reflection),
      has_scope:        has_scope?(reflection),
      foreign_key:      foreign_key_for(reflection, type),
      polymorphic_type: polymorphic_type_for(reflection, type),
      polymorphic_list: polymorphic_list_for(reflection),
      class:            class_for(reflection)
    }
    fields
  end
end

#general_fieldsObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/adaptors/wallaby/active_record/model_decorator/fields_builder.rb', line 6

def general_fields
  @model_class.columns.inject({}) do |fields, column|
    fields[column.name] = {
      name:       column.name,
      type:       column.type.to_s,
      label:      @model_class.human_attribute_name(column.name),
      is_origin:  true
    }
    fields
  end
end