Module: ExpressTemplates::Components::Forms::OptionSupport
Overview
Provides a form component with knowledge of any association on the field and an means of loading the collection for supplying options to the user.
Instance Method Summary collapse
-
#belongs_to_association ⇒ Object
Reflect on any association and return it if the association type is :belongs_to.
-
#related_collection ⇒ Object
Provide ActiveRecord code to load the associated collection as options for display.
Instance Method Details
#belongs_to_association ⇒ Object
Reflect on any association and return it if the association type is :belongs_to. Returns false if the association is not :belongs_to. Returns nil if there was a problem reflecting.
11 12 13 14 15 16 17 |
# File 'lib/express_templates/components/forms/option_support.rb', line 11 def belongs_to_association # assumes the belongs_to association uses <name>_id reflection = resource_class.constantize.reflect_on_association(field_name.gsub(/_id$/, '').to_sym) if reflection && reflection.macro.eql?(:belongs_to) return reflection end end |
#related_collection ⇒ Object
Provide ActiveRecord code to load the associated collection as options for display.
21 22 23 24 25 26 |
# File 'lib/express_templates/components/forms/option_support.rb', line 21 def reflection = belongs_to_association if reflection && !reflection.polymorphic? "#{reflection.klass}.all.select(:#{option_value_method}, :#{option_name_method}).order(:#{option_name_method})" end end |