Sha256: fa3b303b03269dea5517a0490b755a6a07effe880385219cdbb1bf3dac099150
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
module ExpressTemplates module Components module Forms # 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. module OptionSupport # 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. 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 # Provide ActiveRecord code to load the associated collection as # options for display. def related_collection reflection = belongs_to_association if reflection && !reflection.polymorphic? "#{reflection.klass}.all.select(:#{option_value_method}, :#{option_name_method}).order(:#{option_name_method})" end end protected def option_value_method :id end def option_name_method cols = belongs_to_association.klass.columns @option_name_method ||= if cols.detect {|column| column.name.eql?('name') } :name else if string_col = cols.detect {|column| column.type.eql?(:string) } string_col.name.to_sym else :id end end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
express_templates-0.4.2 | lib/express_templates/components/forms/option_support.rb |
express_templates-0.4.1 | lib/express_templates/components/forms/option_support.rb |