Class: Protoform::Rails::OptionMapper
- Inherits:
-
Object
- Object
- Protoform::Rails::OptionMapper
- Includes:
- Enumerable
- Defined in:
- lib/protoform/rails/option_mapper.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.
13 14 15 |
# File 'lib/protoform/rails/option_mapper.rb', line 13 def initialize(collection) @collection = collection end |
Instance Method Details
#active_record_relation_options_enumerable(relation) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/protoform/rails/option_mapper.rb', line 30 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
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/protoform/rails/option_mapper.rb', line 17 def each(&) @collection.each do |object| case object in ActiveRecord::Relation => relation (relation).each(&) in id, value yield id, value in value yield value, value.to_s end end end |