Permalink
Browse files

added :except and :include options to control which languages are dis…

…played
  • Loading branch information...
1 parent ddc2408 commit 7bbc4f430a9932fbdd826ebb37c8086f585414b6 @kristianmandrup kristianmandrup committed Jan 14, 2011
Showing with 43 additions and 7 deletions.
  1. +21 −5 lib/localized_language_select.rb
  2. +22 −2 test/localized_language_select_test.rb
@@ -22,9 +22,25 @@ module LocalizedLanguageSelect
class << self
# Returns array with codes and localized language names (according to <tt>I18n.locale</tt>)
# for <tt><option></tt> tags
- def localized_languages_array
- I18n.translate(:languages).map { |key, value| [value, key.to_s] }.sort
+ def localized_languages_array options = {}
+ res = []
+ list = I18n.translate(:languages).each do |key, value|
+ res << [value, key.to_s.upcase] if include_language?(key.to_s, options)
+ end
+ res.sort_by { |country| country.first.parameterize }
end
+
+ def include_language?(key, options)
+ if options[:only]
+ return options[:only].include?(key)
+ end
+ if options[:except]
+ return !options[:except].include?(key)
+ end
+ true
+ end
+
+
# Return array with codes and localized language names for array of language codes passed as argument
# == Example
# priority_languages_array([:de, :fr, :en])
@@ -63,13 +79,13 @@ def localized_language_select_tag(name, selected_value = nil, priority_languages
# Returns a string of option tags for languages according to locale. Supply the language code in lower-case ('en', 'de')
# as +selected+ to have it marked as the selected option tag.
# Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
- def localized_language_options_for_select(selected = nil, priority_languages = nil)
+ def localized_language_options_for_select(selected = nil, priority_languages = nil, options = {})
language_options = ""
if priority_languages
language_options += options_for_select(LocalizedLanguageSelect::priority_languages_array(priority_languages), selected)
language_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
end
- return language_options + options_for_select(LocalizedLanguageSelect::localized_languages_array, selected)
+ return language_options + options_for_select(LocalizedLanguageSelect::localized_languages_array(options), selected)
end
end
@@ -81,7 +97,7 @@ def to_localized_language_select_tag(priority_languages, options, html_options)
value = value(object)
content_tag("select",
add_options(
- localized_language_options_for_select(value, priority_languages),
+ localized_language_options_for_select(value, priority_languages, options),
options, value
), html_options
)
@@ -4,7 +4,7 @@
require 'rubygems'
require 'active_support'
require 'action_controller'
-require 'action_controller/test_process'
+# require 'action_controller/test_process'
require 'action_view'
require 'action_view/helpers'
require 'action_view/helpers/tag_helper'
@@ -80,7 +80,27 @@ def test_priority_languages_returns_correctly_and_in_correct_order
assert_equal [ ['Dutch', 'nl'], ['French', 'fr'] ], LocalizedLanguageSelect::priority_languages_array([:nl, :fr])
end
- private
+ def test_include_country_rejects_on_except_option
+ res = !LocalizedLanguageSelect::include_language?('af', except: ['af'])
+ puts "should not include_language? '' except af : #{res}"
+ end
+
+ def test_include_country_accept_on_only_option
+ res = LocalizedLanguageSelect::include_language?('af', only: ['af'])
+ puts "should include_language? 'af' : #{res}"
+
+ res = !LocalizedLanguageSelect::include_language?('af', only: ['ad'])
+ puts "should not include_language? 'af' : #{res}"
+ end
+
+ def test_localized_countries_array_rejects_on_except_option
+ list = LocalizedLanguageSelect::localized_languages_array(:except => ['af'])
+ res = !list.include?(['Afrikaans', 'af'])
+ puts "rejected 'af' from array? #{res}"
+ end
+
+
+ # private
def setup
['fr', 'en'].each do |locale|

0 comments on commit 7bbc4f4

Please sign in to comment.