country-select-iso 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/country-select-iso.rb +60 -0
  2. metadata +47 -0
@@ -0,0 +1,60 @@
1
+ require_relative 'countries-list.rb'
2
+
3
+ # CountrySelect
4
+ module ActionView
5
+ module Helpers
6
+ module FormOptionsHelper
7
+ # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
8
+ def country_select(object, method, options = {}, html_options = {})
9
+ InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(options, html_options)
10
+ end
11
+ # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
12
+ # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
13
+ # that they will be listed above the rest of the (long) list.
14
+ #
15
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
16
+ def country_options_for_select(selected = nil, options = nil)
17
+ options = options || {}
18
+ value = options[:value] || :name
19
+ priority_countries = options[:priority_countries] || ["US", "GB", "CA"]
20
+
21
+ countries = ::CountrySelectIso::countries.sort{|c1, c2| c1[:name] <=> c2[:name]}
22
+
23
+ country_options = ""
24
+
25
+ if priority_countries
26
+ country_options += options_for_select(countries
27
+ .select{|c| !priority_countries.index{|pc| matches(c, pc)}.nil?}
28
+ .map{|c| [c[:name], c[value]]}, selected)
29
+ country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
30
+ end
31
+
32
+ country_options + options_for_select(countries.map{|c| [c[:name], c[value]]}, selected)
33
+ end
34
+
35
+ def matches country, value
36
+ country[:name] == value || country[:iso2] == value || country[:iso3] == value
37
+ end
38
+ end
39
+
40
+ class InstanceTag
41
+ def to_country_select_tag(options, html_options)
42
+ html_options = html_options.stringify_keys
43
+ add_default_name_and_id(html_options)
44
+ value = value(object)
45
+ content_tag("select",
46
+ add_options(
47
+ country_options_for_select(value, options).html_safe,
48
+ options, value
49
+ ), html_options
50
+ )
51
+ end
52
+ end
53
+
54
+ class FormBuilder
55
+ def country_select(method, options = {}, html_options = {})
56
+ @template.country_select(@object_name, method, options.merge(:object => @object), html_options)
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: country-select-iso
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Koziarski
9
+ - James Dean Shepherd
10
+ - Igor Shapiro
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-01-08 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: Provides country & state utilities with country & state selection boxes
17
+ email: shapigor@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/country-select-iso.rb
23
+ homepage: https://github.com/igorshapiro/country-select
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Country select box
47
+ test_files: []