Module: Custom::SelectHelper

Included in:
CurrencyHelper, Lib::Boats::Dimensions
Defined in:
app/helpers/custom/select_helper.rb

Instance Method Summary collapse

Instance Method Details

#collection_for_select(collection, name, id, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/custom/select_helper.rb', line 10

def collection_for_select(collection, name, id, options = {})
  return [] unless collection.is_a?(Array)

  collection = if options[:collection_is_not_hash]
                 collection.map do |h|
                   field = h.try(name)
                   label = options[:i18n].present? ? options[:i18n][field] : field.capitalize
                   [ label, h.try(id) ]
                 end
               else
                 collection.map { |h| [ h[name], h[id] ] }
               end
  collection.unshift [options[:blank_value_label],''] if options[:blank_value_label]
  collection = options_for_select(collection, options[:value]) if options[:options_for_select].present?
  collection
end

#collection_name_by_id(collection, id) ⇒ Object



3
4
5
6
7
8
# File 'app/helpers/custom/select_helper.rb', line 3

def collection_name_by_id(collection, id)
  return false if collection.empty?

  element = collection.find { |hash| hash[:id] == id }
  element.try(:fetch, :name)
end