Module: Madmin::Fields::PolymorphicHelper

Defined in:
app/helpers/madmin/fields/polymorphic_helper.rb

Instance Method Summary collapse

Instance Method Details

#polymorphic_models(type) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'app/helpers/madmin/fields/polymorphic_helper.rb', line 4

def polymorphic_models(type)
  all_resources = Madmin::Resources.all.map { |r| Madmin::ResourceDecorator.new(r) }

  polymorphic_resources = all_resources.select { |resource|
    associations = resource.model.reflect_on_all_associations
    associations.select { |a| a.options.dig(:as) === type }.any?
  }

  polymorphic_resources.map(&:model)
end

#polymorphic_options_for_selected_type(form:, field:) ⇒ Object



15
16
17
18
19
20
21
22
# File 'app/helpers/madmin/fields/polymorphic_helper.rb', line 15

def polymorphic_options_for_selected_type(form:, field:)
  options_from_collection_for_select(
    form.object.send(field.polymorphic_type_param).constantize.send(field.polymorphic_scope),
    :id,
    field.polymorphic_display_value,
    form.object.send(field.polymorphic_id_param)
  )
end