Module: Tramway::Core::ApplicationForms::AssociationObjectHelpers
- Included in:
- Tramway::Core::ApplicationForm
- Defined in:
- app/forms/tramway/core/application_forms/association_object_helpers.rb
Instance Method Summary collapse
- #define_association_method(association, class_name) ⇒ Object
- #define_polymorphic_association(association, _class_name) ⇒ Object
Instance Method Details
#define_association_method(association, class_name) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'app/forms/tramway/core/application_forms/association_object_helpers.rb', line 4 def define_association_method(association, class_name) if class_name.is_a? Array define_polymorphic_association association, class_name else self.class.send(:define_method, "#{association}=") do |value| model.send "#{association}_id=", value super class_name.find value end end end |
#define_polymorphic_association(association, _class_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/forms/tramway/core/application_forms/association_object_helpers.rb', line 15 def define_polymorphic_association(association, _class_name) self.class.send(:define_method, "#{association}=") do |value| association_class = value.split('_')[0..-2].join('_').camelize association_class = association_class.constantize if association_class.is_a? String if association_class.nil? Tramway::Error.raise_error :tramway, :core, :application_form, :initialize, :polymorphic_class_is_nil, association_name: association else super association_class.find value.split('_')[-1] send "#{association}_type=", association_class.to_s end end end |