country_select 1.3.1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,130 +1,12 @@
1
- # CountrySelect
2
- #
3
- # Adds #country_select method to
4
- # ActionView::FormBuilder
5
- #
6
- require 'country_select/version'
7
- require 'country_select/countries'
8
-
9
- module ActionView
10
- module Helpers
11
- module FormOptionsHelper
12
- #
13
- # Return select and option tags
14
- # for the given object and method,
15
- # using country_options_for_select to
16
- # generate the list of option tags.
17
- #
18
- def country_select(object, method, priority_countries = nil,
19
- options = {},
20
- html_options = {})
21
-
22
- tag = if defined?(ActionView::Helpers::InstanceTag) &&
23
- ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
24
-
25
- InstanceTag.new(object, method, self, options.delete(:object))
26
- else
27
- CountrySelect.new(object, method, self, options)
28
- end
29
-
30
- tag.to_country_select_tag(priority_countries, options, html_options)
31
- end
32
-
33
- #
34
- # Returns a string of option tags for
35
- # pretty much any country in the world.
36
- #
37
- # You can also supply an array of countries as
38
- # +priority_countries+ so that they will be
39
- # listed above the rest of the (long) list.
40
- #
41
- # NOTE: Only the option tags are returned, you
42
- # have to wrap this call in a regular HTML
43
- # select tag.
44
- #
45
- def country_options_for_select(selected = nil, priority_countries = nil, use_iso_codes = false, use_locale = nil)
46
- country_options = "".html_safe
47
-
48
- if priority_countries
49
- priority_countries_options = if use_iso_codes || ::CountrySelect.use_iso_codes
50
- # Codes should not be downcased, but they were previous to 1.3
51
- # which means they should continue to be until 2.0 is released in
52
- # order to prevent breakage with existing implementations
53
- #
54
- # In 2.0.0, this map should actually be upcasing the codes
55
- priority_countries.map! { |code| code.to_s.downcase }
1
+ # encoding: utf-8
56
2
 
57
- priority_countries.map do |code|
58
- [
59
- ::CountrySelect::countries(use_locale)[code],
60
- code
61
- ]
62
- end
63
- else
64
- priority_countries
65
- end
3
+ require 'iso3166'
66
4
 
67
- country_options += options_for_select(priority_countries_options, selected)
68
- country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n".html_safe
69
- #
70
- # prevents selected from being included
71
- # twice in the HTML which causes
72
- # some browsers to select the second
73
- # selected option (not priority)
74
- # which makes it harder to select an
75
- # alternative priority country
76
- #
77
- selected = nil if priority_countries.include?(selected)
78
- end
79
-
80
- values = if use_iso_codes || ::CountrySelect.use_iso_codes
81
- ::CountrySelect::countries(use_locale).invert
82
- else
83
- ::CountrySelect::countries(use_locale).values
84
- end
85
-
86
- return country_options + options_for_select(values.sort, selected)
87
- end
88
-
89
- # All the countries included in the country_options output.
90
- end
91
-
92
- module ToCountrySelectTag
93
- def to_country_select_tag(priority_countries, options, html_options)
94
- use_iso_codes = options.delete(:iso_codes)
95
- use_locale = options.delete(:locale)
96
- html_options = html_options.stringify_keys
97
- add_default_name_and_id(html_options)
98
- value = value(object)
99
- content_tag("select",
100
- add_options(
101
- country_options_for_select(value, priority_countries, use_iso_codes, use_locale),
102
- options, value
103
- ), html_options
104
- )
105
- end
106
- end
107
-
108
- if defined?(ActionView::Helpers::InstanceTag) &&
109
- ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
110
- class InstanceTag
111
- include ToCountrySelectTag
112
- end
113
- else
114
- class CountrySelect < Tags::Base
115
- include ToCountrySelectTag
116
- end
117
- end
118
-
119
- class FormBuilder
120
- def country_select(method, priority_countries = nil,
121
- options = {},
122
- html_options = {})
5
+ require 'country_select/version'
6
+ require 'country_select/tag_helper'
123
7
 
124
- @template.country_select(@object_name, method, priority_countries,
125
- options.merge(:object => @object),
126
- html_options)
127
- end
128
- end
129
- end
8
+ if defined?(ActionView::Helpers::Tags::Base)
9
+ require 'country_select/country_select_helper'
10
+ else
11
+ require 'country_select/rails3/country_select_helper'
130
12
  end
@@ -0,0 +1,38 @@
1
+ module ActionView
2
+ module Helpers
3
+ class FormBuilder
4
+ def country_select(method, priority_or_options = {}, options = {}, html_options = {})
5
+ if Hash === priority_or_options
6
+ html_options = options
7
+ options = priority_or_options
8
+ else
9
+ options[:priority_countries] = priority_or_options
10
+ end
11
+
12
+ @template.country_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
13
+ end
14
+ end
15
+
16
+ module FormOptionsHelper
17
+ def country_select(object, method, options = {}, html_options = {})
18
+ Tags::CountrySelect.new(object, method, self, options, html_options).render
19
+ end
20
+ end
21
+
22
+ module Tags
23
+ class CountrySelect < Base
24
+ include ::CountrySelect::TagHelper
25
+
26
+ def initialize(object_name, method_name, template_object, options, html_options)
27
+ @html_options = html_options
28
+
29
+ super(object_name, method_name, template_object, options)
30
+ end
31
+
32
+ def render
33
+ select_content_tag(country_option_tags, @options, @html_options)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ module ActionView
2
+ module Helpers
3
+ class FormBuilder
4
+ def country_select(method, priority_or_options = {}, options = {}, html_options = {})
5
+ if Hash === priority_or_options
6
+ html_options = options
7
+ options = priority_or_options
8
+ else
9
+ options[:priority_countries] = priority_or_options
10
+ end
11
+
12
+ @template.country_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
13
+ end
14
+ end
15
+
16
+ module FormOptionsHelper
17
+ def country_select(object, method, options = {}, html_options = {})
18
+ CountrySelect.new(object, method, self, options.delete(:object)).render(options, html_options)
19
+ end
20
+ end
21
+
22
+ class CountrySelect < InstanceTag
23
+ include ::CountrySelect::TagHelper
24
+
25
+ def render(options, html_options)
26
+ @options = options
27
+ @html_options = html_options
28
+
29
+ if self.respond_to?(:select_content_tag)
30
+ select_content_tag(country_option_tags, @options, @html_options)
31
+ else
32
+ html_options = @html_options.stringify_keys
33
+ add_default_name_and_id(html_options)
34
+ content_tag(:select, country_option_tags, html_options)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,88 @@
1
+ module CountrySelect
2
+ module TagHelper
3
+ def country_option_tags
4
+ option_tags_options = {
5
+ :selected => @options.fetch(:selected) { value(@object) },
6
+ :disabled => @options[:disabled]
7
+ }
8
+
9
+ if priority_countries.present?
10
+ priority_countries_options = country_options_for(priority_countries, false)
11
+
12
+ option_tags = options_for_select(priority_countries_options, option_tags_options)
13
+ option_tags += html_safe_newline + options_for_select([priority_countries_divider], disabled: priority_countries_divider)
14
+
15
+ if priority_countries.include?(option_tags_options[:selected])
16
+ option_tags_options[:selected] = nil
17
+ end
18
+
19
+ option_tags += html_safe_newline + options_for_select(country_options, option_tags_options)
20
+ else
21
+ option_tags = options_for_select(country_options, option_tags_options)
22
+ end
23
+ end
24
+
25
+ private
26
+ def locale
27
+ @options[:locale]
28
+ end
29
+
30
+ def priority_countries
31
+ @options[:priority_countries]
32
+ end
33
+
34
+ def priority_countries_divider
35
+ @options[:priority_countries_divider] || "-"*15
36
+ end
37
+
38
+ def only_country_codes
39
+ @options[:only]
40
+ end
41
+
42
+ def except_country_codes
43
+ @options[:except]
44
+ end
45
+
46
+ def country_options
47
+ country_options_for(all_country_codes, true)
48
+ end
49
+
50
+ def all_country_codes
51
+ codes = ISO3166::Country.all.map(&:last)
52
+
53
+ if only_country_codes.present?
54
+ codes & only_country_codes
55
+ elsif except_country_codes.present?
56
+ codes - except_country_codes
57
+ else
58
+ codes
59
+ end
60
+ end
61
+
62
+ def country_options_for(country_codes, sorted=true)
63
+ I18n.with_locale(locale) do
64
+ country_list = country_codes.map do |code|
65
+ code = code.to_s.upcase
66
+
67
+ unless country = ISO3166::Country.new(code)
68
+ code = ISO3166::Country.find_by_name(code).first
69
+ end
70
+
71
+ country ||= ISO3166::Country.new(code)
72
+ default_name = country.name
73
+ localized_name = country.translations[I18n.locale.to_s]
74
+
75
+ name = localized_name || default_name
76
+
77
+ [name,code]
78
+ end
79
+ sorted ? country_list.sort : country_list
80
+ end
81
+ end
82
+
83
+ def html_safe_newline
84
+ "\n".html_safe
85
+ end
86
+ end
87
+ end
88
+
@@ -1,3 +1,3 @@
1
1
  module CountrySelect
2
- VERSION = "1.3.1"
2
+ VERSION = "2.0.0.rc1"
3
3
  end
@@ -1,166 +1,139 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  require 'action_view'
4
6
  require 'country_select'
5
7
 
6
- module ActionView
7
- module Helpers
8
-
9
- describe CountrySelect do
10
- include TagHelper
11
-
12
- class Walrus
13
- attr_accessor :country_name
14
- end
15
-
16
- let(:walrus) { Walrus.new }
17
-
18
- let!(:template) { ActionView::Base.new }
19
-
20
- let(:select_tag) do
21
- "<select id=\"walrus_country_name\" name=\"walrus[country_name]\">"
22
- end
23
-
24
- let(:selected_us_option) do
25
- if defined?(Tags::Base)
26
- content_tag(:option, 'United States of America', :selected => :selected, :value => "United States of America")
27
- else
28
- "<option value=\"United States of America\" selected=\"selected\">United States of America</option>"
29
- end
30
- end
31
-
32
- let(:selected_iso_us_option) do
33
- if defined?(Tags::Base)
34
- content_tag(:option, 'United States of America', :selected => :selected, :value => 'us')
35
- else
36
- "<option value=\"us\" selected=\"selected\">United States of America</option>"
37
- end
38
- end
39
-
40
- let(:builder) do
41
- if defined?(Tags::Base)
42
- FormBuilder.new(:walrus, walrus, template, {})
43
- else
44
- FormBuilder.new(:walrus, walrus, template, {}, Proc.new { })
45
- end
46
- end
47
-
48
- context "iso codes disabled" do
49
- describe "#country_select" do
50
- let(:tag) { builder.country_select(:country_name) }
51
-
52
- it "creates a select tag" do
53
- tag.should include(select_tag)
54
- end
55
-
56
- it "creates option tags of the countries" do
57
- ::CountrySelect::countries("en").each do |code,name|
58
- tag.should include(content_tag(:option, name, :value => name))
59
- end
60
- end
61
-
62
- it "selects the value of country_name" do
63
- walrus.country_name = 'United States of America'
64
- t = builder.country_select(:country_name)
65
- t.should include(selected_us_option)
66
- end
67
- end
68
-
69
- describe "#priority_countries" do
70
- let(:tag) { builder.country_select(:country_name, ['United States of America']) }
71
-
72
- it "puts the countries at the top" do
73
- tag.should include("#{select_tag}<option value=\"United States of America")
74
- end
75
-
76
- it "inserts a divider" do
77
- tag.should include(">United States of America</option><option value=\"\" disabled=\"disabled\">-------------</option>")
78
- end
79
-
80
- it "does not mark two countries as selected" do
81
- walrus.country_name = "United States of America"
82
- str = <<-EOS.strip
83
- </option>\n<option value="United States of America" selected="selected">United States of America</option>
84
- EOS
85
- tag.should_not include(str)
86
- end
87
- end
88
- end
89
-
90
- context "iso codes enabled" do
91
- describe "#country_select" do
92
- let(:tag) { builder.country_select(:country_name, nil, :iso_codes => true) }
93
-
94
- it "creates a select tag" do
95
- tag.should include(select_tag)
96
- end
97
-
98
- it "creates option tags of the countries" do
99
- ::CountrySelect::countries("en").each do |code,name|
100
- tag.should include(content_tag(:option, name, :value => code))
101
- end
102
- end
103
-
104
- it "selects the value of country_name" do
105
- walrus.country_name = 'us'
106
- t = builder.country_select(:country_name, nil, :iso_codes => true)
107
- t.should include(selected_iso_us_option)
108
- end
109
- end
110
-
111
- describe "#country_select with global option" do
112
- before do
113
- ::CountrySelect.use_iso_codes = true
114
- end
115
-
116
- after do
117
- ::CountrySelect.use_iso_codes = false
118
- end
119
-
120
- let(:tag) { builder.country_select(:country_name, nil) }
121
-
122
- it "creates option tags of the countries" do
123
- ::CountrySelect::countries("en").each do |code,name|
124
- tag.should include(content_tag(:option, name, :value => code))
125
- end
126
- end
127
-
128
- it "selects the value of country_name" do
129
- walrus.country_name = 'us'
130
- t = builder.country_select(:country_name)
131
- t.should include(selected_iso_us_option)
132
- end
133
- end
134
-
135
- describe "#priority_countries" do
136
- let(:tag) { builder.country_select(:country_name, ['us'], :iso_codes => true) }
137
-
138
- it "puts the countries at the top" do
139
- tag.should include("#{select_tag}<option value=\"us")
140
- end
141
-
142
- it "inserts a divider" do
143
- tag.should include(">United States of America</option><option value=\"\" disabled=\"disabled\">-------------</option>")
144
- end
145
-
146
- it "does not mark two countries as selected" do
147
- walrus.country_name = "us"
148
- str = <<-EOS.strip
149
- </option>\n<option value="us" selected="selected">United States of America</option>
150
- EOS
151
- tag.should_not include(str)
152
- end
153
- end
154
- end
155
- context "different language selected" do
156
- describe "'es' selected as the instance language" do
157
- let(:tag) { builder.country_select(:country_name, ['us'], {:iso_codes => true, :locale => 'es'}) }
158
-
159
- it "displays spanish names" do
160
- tag.should include(">Estados Unidos</option><option value=\"\" disabled=\"disabled\">-------------</option>")
161
- end
162
- end
163
- end
8
+ describe "CountrySelect" do
9
+ include ActionView::Helpers::TagHelper
10
+ include ActionView::Helpers::FormOptionsHelper
11
+
12
+ class Walrus
13
+ attr_accessor :country_code
14
+ end
15
+
16
+ let(:walrus) { Walrus.new }
17
+ let!(:template) { ActionView::Base.new }
18
+
19
+ let(:builder) do
20
+ if defined?(ActionView::Helpers::Tags::Base)
21
+ ActionView::Helpers::FormBuilder.new(:walrus, walrus, template, {})
22
+ else
23
+ ActionView::Helpers::FormBuilder.new(:walrus, walrus, template, {}, Proc.new { })
24
+ end
25
+ end
26
+
27
+ let(:select_tag) do
28
+ <<-EOS.chomp.strip
29
+ <select id="walrus_country_code" name="walrus[country_code]">
30
+ EOS
31
+ end
32
+
33
+ it "selects the value of country_code" do
34
+ tag = options_for_select([['United States of America', 'US']], 'US')
35
+
36
+ walrus.country_code = 'US'
37
+ t = builder.country_select(:country_code)
38
+ expect(t).to include(tag)
39
+ end
40
+
41
+ it "uses the locale specified by I18n.locale" do
42
+ tag = options_for_select([['Estados Unidos', 'US']], 'US')
43
+
44
+ walrus.country_code = 'US'
45
+ original_locale = I18n.locale
46
+ begin
47
+ I18n.locale = :es
48
+ t = builder.country_select(:country_code)
49
+ expect(t).to include(tag)
50
+ ensure
51
+ I18n.locale = original_locale
52
+ end
53
+ end
54
+
55
+ it "accepts a locale option" do
56
+ tag = options_for_select([['États-Unis', 'US']], 'US')
57
+
58
+ walrus.country_code = 'US'
59
+ t = builder.country_select(:country_code, locale: :fr)
60
+ expect(t).to include(tag)
61
+ end
62
+
63
+ it "accepts priority countries" do
64
+ tag = options_for_select(
65
+ [
66
+ ['Latvia','LV'],
67
+ ['United States of America','US'],
68
+ ['Denmark', 'DK'],
69
+ ['-'*15,'-'*15]
70
+ ],
71
+ selected: 'US',
72
+ disabled: '-'*15
73
+ )
74
+
75
+ walrus.country_code = 'US'
76
+ t = builder.country_select(:country_code, priority_countries: ['LV','US','DK'])
77
+ expect(t).to include(tag)
78
+ end
79
+
80
+ it "selects only the first matching option" do
81
+ tag = options_for_select([["United States of America", "US"],["Uruguay", "UY"]], "US")
82
+ walrus.country_code = 'US'
83
+ t = builder.country_select(:country_code, priority_countries: ['LV','US'])
84
+ expect(t).to_not include(tag)
85
+ end
86
+
87
+ it "displays only the chosen countries" do
88
+ options = [["Denmark", "DK"],["Germany", "DE"]]
89
+ tag = builder.select(:country_code, options)
90
+ walrus.country_code = 'US'
91
+ t = builder.country_select(:country_code, only: ['DK','DE'])
92
+ expect(t).to eql(tag)
93
+ end
94
+
95
+ it "discards some countries" do
96
+ tag = options_for_select([["United States of America", "US"]])
97
+ walrus.country_code = 'DE'
98
+ t = builder.country_select(:country_code, except: ['US'])
99
+ expect(t).to_not include(tag)
100
+ end
101
+
102
+ context "using old 1.x syntax" do
103
+ it "accepts priority countries" do
104
+ tag = options_for_select(
105
+ [
106
+ ['Latvia','LV'],
107
+ ['United States of America','US'],
108
+ ['Denmark', 'DK'],
109
+ ['-'*15,'-'*15]
110
+ ],
111
+ selected: 'US',
112
+ disabled: '-'*15
113
+ )
114
+
115
+ walrus.country_code = 'US'
116
+ t = builder.country_select(:country_code, ['LV','US','DK'])
117
+ expect(t).to include(tag)
118
+ end
119
+
120
+ it "selects only the first matching option" do
121
+ tag = options_for_select([["United States of America", "US"],["Uruguay", "UY"]], "US")
122
+ walrus.country_code = 'US'
123
+ t = builder.country_select(:country_code, ['LV','US'])
124
+ expect(t).to_not include(tag)
125
+ end
126
+
127
+ it "supports the country names as provided by default in Formtastic" do
128
+ tag = options_for_select([
129
+ ["Australia", "AU"],
130
+ ["Canada", "CA"],
131
+ ["United Kingdom", "GB"],
132
+ ["United States of America", "US"]
133
+ ])
134
+ country_names = ["Australia", "Canada", "United Kingdom", "United States"]
135
+ t = builder.country_select(:country_code, country_names)
136
+ expect(t).to include(tag)
164
137
  end
165
138
  end
166
139
  end