# frozen_string_literal: true require 'simpleadmin/decorators/fields/base' require 'simpleadmin/decorators/fields/image' module Simpleadmin module Decorators module FieldDecorator FIELDS_MAPPER = { 'image' => Fields::Image }.freeze 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 module_function :call private def table_field_match?(table_field_type) FIELDS_MAPPER.key?(table_field_type) end module_function :table_field_match? end end end