Sha256: 2e996cd5681fc9efa17cc691f1306cc7ea7fdc960c298e7e525f370d3e62c485

Contents?: true

Size: 1.28 KB

Versions: 12

Compression:

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

            association = resource_class.try(:reflect_on_association, attr_name)
            raise ArgumentError, "#{attr_name} is not a valid association of #{resource_class}" unless association.present?
            raise ArgumentError, "#{association.klass} does is not a resource record" unless association.klass.include?(Plutonium::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

12 entries across 12 versions & 1 rubygems

Version Path
plutonium-0.12.9 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.8 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.7 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.5 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.4 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.3 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.2 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.1 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.12.0 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.11.2 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.11.1 lib/plutonium/core/associations/renderers/factory.rb
plutonium-0.11.0 lib/plutonium/core/associations/renderers/factory.rb