Sha256: 15b9135179ba56cc33fea03d02b873a490cc32dafc4245d1915b1758097e90de

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simpleadmin-1.5.0 lib/simpleadmin/decorators/fields_decorator.rb