country-select-iso 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/country-select-iso.rb +32 -3
  2. metadata +1 -1
@@ -8,6 +8,10 @@ module ActionView
8
8
  def country_select(object, method, options = {}, html_options = {})
9
9
  InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(options, html_options)
10
10
  end
11
+
12
+ def state_select(object, method, options = {}, html_options = {})
13
+ InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(options, html_options)
14
+ end
11
15
  # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
12
16
  # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
13
17
  # that they will be listed above the rest of the (long) list.
@@ -24,7 +28,7 @@ module ActionView
24
28
 
25
29
  if priority_countries
26
30
  country_options += options_for_select(countries
27
- .select{|c| !priority_countries.index{|pc| matches(c, pc)}.nil?}
31
+ .select{|c| !priority_countries.index{|pc| country_matches(c, pc)}.nil?}
28
32
  .map{|c| [c[:name], c[value]]}, selected)
29
33
  country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
30
34
  end
@@ -32,7 +36,16 @@ module ActionView
32
36
  country_options + options_for_select(countries.map{|c| [c[:name], c[value]]}, selected)
33
37
  end
34
38
 
35
- def matches country, value
39
+ def state_options_for_select(selected = nil, options = nil)
40
+ options = options || {}
41
+ country = options[:country] || "US"
42
+ value = options[:value] || :full_code
43
+ name = options[:name] || :localized_name
44
+ states = ::CountrySelectIso::states.select{|s| s[:country_iso2] == country}.sort{|s1, s2| s1[name] <=> s2[name]}
45
+ options_for_select(states.map{|s| [s[name], s[value]]})
46
+ end
47
+
48
+ def country_matches country, value
36
49
  country[:name] == value || country[:iso2] == value || country[:iso3] == value
37
50
  end
38
51
  end
@@ -49,11 +62,27 @@ module ActionView
49
62
  ), html_options
50
63
  )
51
64
  end
65
+
66
+ def to_state_select_tag(options, html_options)
67
+ html_options = html_options.stringify_keys
68
+ add_default_name_and_id(html_options)
69
+ value = value(object)
70
+ content_tag("select",
71
+ add_options(
72
+ state_options_for_select(value, options).html_safe,
73
+ options, value
74
+ ), html_options
75
+ )
76
+ end
52
77
  end
53
78
 
54
79
  class FormBuilder
55
80
  def country_select(method, options = {}, html_options = {})
56
- @template.country_select(@object_name, method, options.merge(:object => @object), html_options)
81
+ @template.country_select(@object_name, method, options.merge(object: @object), html_options)
82
+ end
83
+
84
+ def state_select(method, options = {}, html_options = {})
85
+ @template.state_select(@object_name, method, options.merge(object: @object), html_options)
57
86
  end
58
87
  end
59
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: country-select-iso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: