Module: Tramway::Inputs::PolymorphicAssociationsHelper

Included in:
Tramway::InputsHelper
Defined in:
app/helpers/tramway/inputs/polymorphic_associations_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_collection_for_polymorphic_association(form_object, property) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/helpers/tramway/inputs/polymorphic_associations_helper.rb', line 4

def build_collection_for_polymorphic_association(form_object, property)
  object_names = full_class_names(form_object, property).map do |class_name|
    class_name.send("#{current_user.role}_scope", current_user.id).map do |obj|
      decorator_class(class_name).decorate obj
    end
  end.flatten
  object_names.sort_by { |obj| obj.name.to_s }
end

#build_value_for_polymorphic_association(form_object, property, value) ⇒ Object



13
14
15
16
17
18
19
# File 'app/helpers/tramway/inputs/polymorphic_associations_helper.rb', line 13

def build_value_for_polymorphic_association(form_object, property, value)
  if form_object.send(property).present?
    "#{form_object.send(property).class.to_s.underscore}_#{form_object.send(property).id}"
  elsif value[:type].present? && value[:id].present?
    "#{value[:type]&.underscore}_#{value[:id]}"
  end
end

#full_class_names(form_object, property) ⇒ Object



21
22
23
# File 'app/helpers/tramway/inputs/polymorphic_associations_helper.rb', line 21

def full_class_names(form_object, property)
  form_object.model.class.send("#{property}_type").values.map(&:constantize)
end