Module: Simpleadmin::Decorators::FieldDecorator

Defined in:
lib/simpleadmin/decorators/fields_decorator.rb

Overview

Since:

  • 1.0.0

Constant Summary collapse

FIELDS_MAPPER =

Since:

  • 1.0.0

{
  'image' => Fields::Image
}.freeze

Class Method Summary collapse

Class Method Details

.call(resources, table_name, table_fields) ⇒ Object

Since:

  • 1.0.0



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simpleadmin/decorators/fields_decorator.rb', line 13

def call(resources, table_name, table_fields)
  resources.map do |resource|
    resource_attributes = {}

    table_fields.each do |table_field|
      table_field_name, table_field_type = *table_field.values

      if table_field_match?(table_field_type)
        field_class = FIELDS_MAPPER[table_field_type]

        resource_attributes[table_field_name] = field_class.new(table_name, table_field_name, resource).call
      else
        resource_attributes[table_field_name] = resource[table_field_name.to_sym]
      end
    end

    resource_attributes
  end
end

.table_field_match?(table_field_type) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



37
38
39
# File 'lib/simpleadmin/decorators/fields_decorator.rb', line 37

def table_field_match?(table_field_type)
  FIELDS_MAPPER.key?(table_field_type)
end