Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/language_select.rb

Instance Method Summary collapse

Instance Method Details

#language_options_for_select(selected = nil, priority_languages = nil) ⇒ Object

Returns a string of option tags for most languages throughout the World.

You can also supply an array of languages as priority_languages. They will be listed at the top of the (long) list.

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/language_select.rb', line 45

def language_options_for_select(selected = nil, priority_languages = nil)
  language_options = "".html_safe

  if priority_languages then
    priority_languages_options = priority_languages.map do |code|
                                     [
                                       ::LanguageSelect::LANGUAGES[code],
                                       code
                                     ]
                                   end

    language_options += options_for_select(priority_languages_options, selected)
    language_options += '<option value="" disabled="disabled">-------------</option>\n'.html_safe
    #
    # prevents selected from being included
    # twice in the HTML which causes
    # some browsers to select the second
    # selected option (not priority)
    # which makes it harder to select an
    # alternative priority language
    #
    selected = nil if priority_languages.include?(selected)
  end

  return language_options + options_for_select(::LanguageSelect::LANGUAGES_FOR_SELECT, selected)
end

#language_select(object, method, priority_languages = nil, options = {}, html_options = {}) ⇒ Object

Return select and option tags for the given object and method, using language_options_for_select to generate the list of option tags.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/language_select.rb', line 21

def language_select(object, method, priority_languages = nil,
                                   options = {},
                                   html_options = {})

  tag = if defined?(ActionView::Helpers::InstanceTag) &&
          ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0

          InstanceTag.new(object, method, self, options.delete(:object))
        else
          LanguageSelect.new(object, method, self, options)
        end

  tag.to_language_select_tag(priority_languages, options, html_options)
end