Sha256: b5fbf7137f66e1c6d5a9c57dea0885ade7665ddc126ac7ac8a6adff816c350e5

Contents?: true

Size: 1.28 KB

Versions: 16

Compression:

Stored size: 1.28 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
            options[:label] ||= resource_class.human_attribute_name(attr_name)

            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

16 entries across 16 versions & 1 rubygems

Version Path
plutonium-0.12.9 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.8 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.7 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.5 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.4 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.3 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.2 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.1 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.12.0 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.11.2 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.11.1 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.11.0 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.8.0 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.6.2 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.6.1 lib/plutonium/core/fields/renderers/factory.rb
plutonium-0.6.0 lib/plutonium/core/fields/renderers/factory.rb