Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb

Instance Method Summary collapse

Instance Method Details

#collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



11
12
13
# File 'lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb', line 11

def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
  InstanceTag.new(object, method, self, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options)
end

#option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil, disabled = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb', line 52

def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil, disabled = nil)
  collection.inject("") do |options_for_select, group|
    group_label_string = eval("group.#{group_label_method}")
    options_for_select += "<optgroup label=\"#{html_escape(group_label_string)}\">"
    options_for_select += options_from_collection_for_select(eval("group.#{group_method}"), option_key_method, option_value_method, selected_key, disabled)
    options_for_select += '</optgroup>'
  end
end

#options_for_select(container, selected = nil, disabled = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb', line 15

def options_for_select(container, selected = nil, disabled = nil)
  container = container.to_a if Hash === container

  options_for_select = container.inject([]) do |options, element|
    text, value = option_text_and_value(element)
    selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
    disabled_attribute = ' disabled="disabled"' if option_value_selected?(value, disabled) && disabled != nil
    options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}>#{html_escape(text.to_s)}</option>)
  end

  options_for_select.join("\n")
end

#options_from_collection_for_select(collection, value_method, text_method, selected = nil, disabled = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb', line 28

def options_from_collection_for_select(collection, value_method, text_method, selected = nil, disabled = nil)
  options = collection.map do |element|
    [element.send(text_method), element.send(value_method)]
  end

  if selected.is_a?(Proc)
    selected_values = collection.map do |element|
      element.send(value_method) if selected.call(element) 
    end.compact
  else
    selected_values = selected
  end

  if disabled.is_a?(Proc)
    disabled_values = collection.map do |element|
      element.send(value_method) if disabled.call(element)
    end.compact
  else
    disabled_values = disabled
  end
  
  options_for_select(options, selected_values, disabled_values)
end

#select(object, method, choices, options = {}, html_options = {}) ⇒ Object



7
8
9
# File 'lib/plugins/option_tags_with_disable/lib/option_tags_will_disable.rb', line 7

def select(object, method, choices, options = {}, html_options = {})
  InstanceTag.new(object, method, self, options.delete(:object)).to_select_tag(choices, options, html_options)
end