-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9c9641
commit 280d5ff
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
lib/plutonium/core/fields/inputs/polymorphic_belongs_to_association_input.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |