Sha256: c5987d50ea375ff3de26d1776d49f998ab04b7df4c0f57126f30d255e73df40d

Contents?: true

Size: 1.2 KB

Versions: 33

Compression:

Stored size: 1.2 KB

Contents

require "simple_form/map_type"

module Plutonium
  module Core
    module Fields
      module Renderers
        class Factory
          extend ::SimpleForm::MapType

          map_type :belongs_to, :has_one, :has_many, to: Plutonium::Core::Fields::Renderers::AssociationRenderer
          map_type :attachment, to: Plutonium::Core::Fields::Renderers::AttachmentRenderer

          def self.build(name, type:, **)
            mapping = mappings[type] || Plutonium::Core::Fields::Renderers::BasicRenderer
            mapping.new(name, **)
          end

          def self.for_resource_attribute(resource_class, attr_name, **options)
            type = nil

            if (attachment = resource_class.try(:reflect_on_attachment, attr_name))
              type = :attachment
              options[:reflection] = attachment
            elsif (association = resource_class.try(:reflect_on_association, attr_name))
              type = association.macro
              options[:reflection] = association
            elsif (column = resource_class.try(:column_for_attribute, attr_name))
              type = column.type
            end

            build(attr_name, type:, **options)
          end
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
plutonium-0.15.18 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.17 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.16 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.15 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.14 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.13 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.12 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.11 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.10 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.9 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.8 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.7 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.6 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.5 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.4 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.3 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.2 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.1 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.0 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.15.0.pre.rc3 lib/plutonium/core/fields/renderers/factory.rb