Sha256: ea61170e1b6befedc10d5debbb4afda28d177f0cb30e5fe941794e1a5a2b9fa3

Contents?: true

Size: 1.44 KB

Versions: 22

Compression:

Stored size: 1.44 KB

Contents

require "simple_form/map_type"

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

          map_type :has_many, to: Plutonium::Core::Associations::Renderers::HasManyRenderer

          def self.build(name, type:, **)
            mapping = mappings[type]
            raise ArgumentError, "Unknown association renderer type #{type}" unless mapping.present?

            mapping.new(name, **)
          end

          def self.for_resource_association(resource_class, attr_name, **options)
            association = resource_class.try(:reflect_on_association, attr_name)
            raise ArgumentError, "#{attr_name} is not a valid association of #{resource_class}" unless association.present?
            # # TODO: fix constant being out of sync after reload during development
            # valid_resource_record = Plutonium.configuration.development? ? association.klass.respond_to?(:resource_field_names) : association.klass.include?(Plutonium: :Resource: :Record)
            # raise ArgumentError, "#{association.klass} is not a resource record" unless valid_resource_record

            type = association.macro
            raise NotImplementedError, "#{macro} associations are currently not supported." unless type == :has_many

            options[:reflection] = association
            build(attr_name, type:, **options)
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

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