Skip to content

Commit

Permalink
initial polymorphic input support
Browse files Browse the repository at this point in the history
  • Loading branch information
thedumbtechguy committed Apr 30, 2024
1 parent d9c9641 commit 280d5ff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/plutonium/core/fields/inputs/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Factory

map_type :has_one, to: Plutonium::Core::Fields::Inputs::NoopInput
map_type :belongs_to, to: Plutonium::Core::Fields::Inputs::BelongsToAssociationInput
map_type :polymorphic_belongs_to, to: Plutonium::Core::Fields::Inputs::PolymorphicBelongsToAssociationInput
map_type :has_many, to: Plutonium::Core::Fields::Inputs::HasManyAssociationInput
map_type :attachment, to: Plutonium::Core::Fields::Inputs::AttachmentInput
map_type :datetime, :date, :time, to: Plutonium::Core::Fields::Inputs::DateTimeInput
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Plutonium
module Core
module Fields
module Inputs
class PolymorphicBelongsToAssociationInput < SimpleFormAssociationInput
def render(f, record, **opts)
opts = options.deep_merge opts
f.input param, **opts
end

private

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

def input_options
{
as: :grouped_select,
label: reflection.name.to_s.humanize,
collection: associated_classes.map { |klass|
[klass.name, klass.all]
}.to_h,
group_label_method: :first,
group_method: :last, include_blank: "Select One"
}
end

def associated_classes
Rails.application.eager_load! unless Rails.application.config.eager_load

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
end
end
end
end
end
end

0 comments on commit 280d5ff

Please sign in to comment.