Sha256: f87dab49fb03f6f51831125ade732d16bc9d7bfedbfe94712480f25102acfad0

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

module Plutonium
  module Core
    module Fields
      module Inputs
        class PolymorphicBelongsToAssociationInput < SimpleFormAssociationInput
          def render
            form.input param, **options
          end

          private

          def param
            :"#{reflection.name}_sgid"
          end

          def input_options
            collection = @user_options.delete(:collection).presence || associated_classes
            {
              as: :grouped_select,
              collection:,
              label: reflection.name.to_s.humanize,
              group_label_method: :first,
              group_method: :last, include_blank: "Select One"
            }
          end

          def associated_classes
            Plutonium.eager_load_rails!

            associated_classes = []
            ActiveRecord::Base.descendants.each do |model|
              next unless model.table_exists?
              model.reflect_on_all_associations(:has_many).each do |association|
                if association.options[:as] == reflection.name
                  associated_classes << model.name
                end
              end
            end

            associated_classes.map { |klass|
              [klass.name, klass.all]
            }.to_h
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
plutonium-0.13.1 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.13.0 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.12.14 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.12.13 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.12.12 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.12.11 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
plutonium-0.12.10 lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb