Class: Superform::Rails::OptionMapper
- Inherits:
-
Object
- Object
- Superform::Rails::OptionMapper
- Includes:
- Enumerable
- Defined in:
- lib/superform/rails.rb
Overview
Accept a collection of objects and map them to options suitable for form controls, like ‘select > options`
Instance Method Summary collapse
- #active_record_relation_options_enumerable(relation) ⇒ Object
- #each(&options) ⇒ Object
-
#initialize(collection) ⇒ OptionMapper
constructor
A new instance of OptionMapper.
Constructor Details
#initialize(collection) ⇒ OptionMapper
Returns a new instance of OptionMapper.
171 172 173 |
# File 'lib/superform/rails.rb', line 171 def initialize(collection) @collection = collection end |
Instance Method Details
#active_record_relation_options_enumerable(relation) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/superform/rails.rb', line 188 def (relation) Enumerator.new do |collection| relation.each do |object| attributes = object.attributes id = attributes.delete(relation.primary_key) value = attributes.values.join(" ") collection << [ id, value ] end end end |
#each(&options) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/superform/rails.rb', line 175 def each(&) @collection.each do |object| case object in ActiveRecord::Relation => relation (relation).each(&) in id, value .call id, value in value .call value, value.to_s end end end |