country_state_select 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -0
  3. data/README.md +126 -8
  4. data/app/controllers/country_state_select/cscs_controller.rb +9 -0
  5. data/config/routes.rb +4 -4
  6. data/country_state_select.gemspec +16 -10
  7. data/lib/country_state_select.rb +33 -22
  8. data/lib/country_state_select/version.rb +1 -1
  9. data/spec/country_state_select_spec.rb +172 -0
  10. data/spec/spec_helper.rb +8 -0
  11. data/test/dummy/Gemfile +40 -43
  12. data/test/dummy/app/assets/javascripts/application.js +4 -3
  13. data/test/dummy/app/assets/javascripts/locations.coffee +2 -0
  14. data/test/dummy/app/assets/stylesheets/application.scss +16 -0
  15. data/test/dummy/app/assets/stylesheets/{users.css.scss → locations.css.scss} +1 -1
  16. data/test/dummy/app/controllers/locations_controller.rb +55 -0
  17. data/test/dummy/app/form/location_form.rb +11 -0
  18. data/test/dummy/app/models/location.rb +2 -0
  19. data/test/dummy/app/views/layouts/application.html.erb +7 -5
  20. data/test/dummy/app/views/locations/_form.html.erb +15 -0
  21. data/test/dummy/app/views/locations/index.html.erb +29 -0
  22. data/test/dummy/app/views/locations/new.html.erb +13 -0
  23. data/test/dummy/app/views/locations/show.html.erb +13 -0
  24. data/test/dummy/bin/bundle +0 -0
  25. data/test/dummy/bin/rails +0 -0
  26. data/test/dummy/bin/rake +0 -0
  27. data/test/dummy/config/environments/production.rb +1 -1
  28. data/test/dummy/config/initializers/simple_form.rb +165 -0
  29. data/test/dummy/config/locales/simple_form.en.yml +31 -0
  30. data/test/dummy/config/routes.rb +2 -56
  31. data/test/dummy/db/migrate/20140724080030_create_locations.rb +10 -0
  32. data/test/dummy/db/migrate/20151024160724_add_test_city_to_locations.rb +5 -0
  33. data/test/dummy/db/schema.rb +5 -8
  34. data/vendor/assets/javascript/country_state_select.js.erb +159 -36
  35. metadata +172 -39
  36. data/lib/country_state_select/cst_data.rb +0 -17
  37. data/test/dummy/app/assets/javascripts/users.js.coffee +0 -9
  38. data/test/dummy/app/assets/stylesheets/application.css +0 -14
  39. data/test/dummy/app/assets/stylesheets/scaffolds.css.scss +0 -69
  40. data/test/dummy/app/controllers/users_controller.rb +0 -74
  41. data/test/dummy/app/helpers/users_helper.rb +0 -2
  42. data/test/dummy/app/models/user.rb +0 -2
  43. data/test/dummy/app/views/users/_form.html.erb +0 -35
  44. data/test/dummy/app/views/users/edit.html.erb +0 -6
  45. data/test/dummy/app/views/users/index.html.erb +0 -35
  46. data/test/dummy/app/views/users/index.json.jbuilder +0 -4
  47. data/test/dummy/app/views/users/new.html.erb +0 -5
  48. data/test/dummy/app/views/users/show.html.erb +0 -27
  49. data/test/dummy/app/views/users/show.json.jbuilder +0 -1
  50. data/test/dummy/db/migrate/20140724080030_create_users.rb +0 -12
  51. data/test/dummy/db/migrate/20140728194622_add_state_name_to_users.rb +0 -5
  52. data/test/dummy/db/migrate/20140728203606_add_country_name_to_user.rb +0 -5
  53. data/test/dummy/test/controllers/users_controller_test.rb +0 -49
  54. data/test/dummy/test/fixtures/users.yml +0 -13
  55. data/test/dummy/test/helpers/users_helper_test.rb +0 -4
  56. data/test/dummy/test/models/user_test.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02bf73bd8a9196b44064226453776133072796c9
4
- data.tar.gz: df815b26922b16949f4c47ed724b20a042a73ba9
3
+ metadata.gz: 2c7cc9e30bebe46054458ff4145604012db5eeba
4
+ data.tar.gz: f378530aece9b4763fdfec46e6963c29ae3a038e
5
5
  SHA512:
6
- metadata.gz: 2fadfdc4f7209ad09cdd7a172b23789518cc0f33717ad4e6ce153562d2c51006c9988d48965ee22e63a54a795b19023ca72faacdea50f3326ff0942513bedcbc
7
- data.tar.gz: f1b5ce31078cf6740469c7f79dd9250ec8013dd7fcc0b618f6f0d1a93bd6bde12be41d038503f0592a8d291f9a20e4e6bd080e09328d065a75f1003a3717d5db
6
+ metadata.gz: 49807b5fe4c87a700b8f45eac6fbafbcdf7bab75beec9c94b70afaf7750074d5a1cd41e8aede34b20b119ec79bbbaabee7aa878763b3c789370e5df3690ad831
7
+ data.tar.gz: ccd794e1caf34f5ab5ac01910cd055e6f88c2ea348dd297bfdb177dd10fde1466282e032e4277c2959439fc814286b3ae27f775d12d68ba5cd80f6e2538e31fb
@@ -6,4 +6,6 @@ rvm:
6
6
  - 2.1.2
7
7
  script:
8
8
  # ---if you written test cases then write commands here to run
9
+
9
10
  - bundle install
11
+ - rspec
data/README.md CHANGED
@@ -1,22 +1,140 @@
1
1
  ##Country State Select [![Build Status](https://travis-ci.org/arvindvyas/Country-State-Select.svg?branch=master)](https://travis-ci.org/arvindvyas/Country-State-Select) [![Code Climate](https://codeclimate.com/github/arvindvyas/Country-State-Select/badges/gpa.svg)](https://codeclimate.com/github/arvindvyas/Country-State-Select)
2
2
 
3
- Country State Select is library which gives you all the country names and when you select any country then it also create the other select box with all the states name of that country
3
+ Country State Select is a library that provides an easy API to generate Country , State / Province and City dropdowns for use in forms.
4
+
5
+ When implemented correctly, a State / Province dropdown is filled with appropriate regions based upon what Country a user has selected .
6
+
7
+ For instance, if a user chooses "United States of America" for a Country dropdown, the State dropdown will be filled with the 50 appropriate states plus the District of Columbia also then user can list city according to state selection but currently cities are limited.
8
+
9
+ The data for Countries and States is populated by the excellent [city-state](https://github.com/loureirorg/city-state) gem.
4
10
 
5
11
  #Getting Started
6
12
 
7
- Country State Select is released as a Ruby Gem. The gem is to be installed within a Ruby
8
- or Rails application. To install, simply add the following to your Gemfile.
13
+ Country State Select is released as a Ruby Gem and requires the Rails environment as well.
14
+
15
+ To install, simply add the following to your Gemfile.
9
16
 
10
17
  gem 'country_state_select'
11
18
 
12
- Run bundle install and don't forget to restart your server after you install a new gem.
19
+ Then run
20
+
21
+ bundle install
22
+
23
+
24
+ Don't forget to restart your server after you install it.
25
+
26
+
27
+ ## chosen JavaScript assets
28
+ The application makes use of the chosen-rails library for a better user experience. This can be disabled if desired.
29
+
30
+ If you're using chosen-rails, please add the following to app/assets/javascripts/application.js:
13
31
 
32
+ //= require chosen-jquery
14
33
 
15
34
 
16
- ##Contributing to Country State Select
35
+ ## chosen CSS assets
36
+
37
+ You'll also need to add the chosen-rails stylesheet.
17
38
 
18
- Fork, fix, then send me a pull request.
39
+ If you're using chosen-rails, please add the following to app/assets/stylesheets/application.css:
40
+
41
+ *= require chosen
42
+
43
+ or
44
+
45
+ @import "chosen";
46
+
47
+
48
+ # Form Implementation in SimpleForm
49
+
50
+ Implementation in a view is simple.
51
+
52
+ Just create a simple_form object like so:
53
+
54
+ <%= simple_form_for @form_object do |f|
55
+ f.button :submit
56
+ end %>
57
+
58
+ Once your form object is created, add in fields for your user to select a Country and State / Province.
59
+
60
+ ## Country Field
61
+
62
+ To add the Country field, add an input field like the following:
63
+
64
+ <%= f.input :country_field, collection: CountryStateSelect.countries_collection %>
19
65
 
20
- ##License
66
+ The above code is simply a normal simple_form field object with a collection of countries passed to it using the CountryStateSelect library.
67
+
68
+ You can pass any of the normal options that a field object accepts - :label, :input_html, etc.
69
+
70
+ ## States / Provinces Field
71
+
72
+ To list the States / Provinces, add an input field like the following:
73
+
74
+ <%= options = { form: f, field_names: { :country => :country_field, :state => :state_field } }
75
+
76
+ f.input :state_field, CountryStateSelect.state_options(options) %>
77
+
78
+ It's important to note that the :country and :state keys in the :field_names object must contain the correct values in order for the correct States / Provinces to load into the window.
79
+
80
+ Other than that, it's yet another standard simple_form field object.
81
+
82
+ Any options that are valid in simple_form field can be passed into the options hash.
83
+
84
+ # Required JavaScript
85
+
86
+ The magic of the library is the JavaScript that is performed on the fields. This JavaScript needs to be included in order for the proper State / Provinces to be selected based upon your Country selection.
87
+
88
+ Add this JavaScript to the page that is loading your form:
89
+
90
+ $(document).on('ready page:load', function() {
91
+ return CountryStateSelect({
92
+ country_id: "country_field_id",
93
+ state_id: "state_field_id"
94
+ });
95
+ });
96
+
97
+ If you are using CoffeeScript, use the following:
98
+
99
+ $(document).on 'ready page:load', ->
100
+ CountryStateSelect({ country_id: "country_field_id", state_id: "state_field_id" })
101
+
102
+ The country_id accepts the form ID for the country field. The state_id accepts the form ID for the state field.
103
+
104
+ The easiest way to determine the ID is to view the source in the web browser. (Or, you could set it in the options hash.)
105
+
106
+ The ID being referred to is like the following in a select menu:
107
+
108
+ <select id="country_field_id"></select>
109
+
110
+ # chosen UI improvements (optional)
111
+
112
+ The CountryStateSelect interface offers the option to turn the Chosen library on or off.
113
+
114
+ To turn it on, set it to true:
115
+
116
+ $(document).on('ready page:load', function() {
117
+ return CountryStateSelect({
118
+ chosen_ui: true,
119
+ ...
120
+ ...
121
+ ...
122
+ });
123
+ });
124
+
125
+ To turn it off, set it to false:
126
+
127
+ $(document).on('ready page:load', function() {
128
+ return CountryStateSelect({
129
+ chosen_ui: false,
130
+ ...
131
+ ...
132
+ ...
133
+ });
134
+ });
135
+
136
+
137
+ ##Contributing to Country State Select
21
138
 
22
- :include:license
139
+ Fork, fix, then send a pull request.
140
+
@@ -8,5 +8,14 @@ module CountryStateSelect
8
8
  format.json { render :json => csc.to_a}
9
9
  end
10
10
  end
11
+
12
+ # Sent it to state_id and country id it will return cities of that states
13
+ def find_cities
14
+ cities = CS.cities(params[:state_id].to_sym, params[:country_id].to_sym)
15
+
16
+ respond_to do |format|
17
+ format.json { render :json => cities.to_a}
18
+ end
19
+ end
11
20
  end
12
21
  end
@@ -1,6 +1,6 @@
1
1
  Rails.application.routes.draw do
2
- scope :module => "country_state_select" do
3
- post 'find_states' => 'cscs#find_states'
4
- get 'find_states' => 'cscs#find_states'
5
- end
2
+ scope :module => "country_state_select" do
3
+ post 'find_states' => 'cscs#find_states'
4
+ post 'find_cities' => 'cscs#find_cities'
5
+ end
6
6
  end
@@ -8,18 +8,24 @@ Gem::Specification.new do |spec|
8
8
  spec.version = CountryStateSelect::VERSION
9
9
  spec.authors = ["Arvind Vyas"]
10
10
  spec.email = ["arvindvyas07@gmail.com"]
11
- spec.summary = %q{This is to list country and state.}
12
- spec.description = %q{Can list out country and according to that can list state dynamically.}
13
- spec.homepage = "https://github.com/arvindvyas/country_state_select.git"
11
+ spec.summary = %q{Dynamically select Country and State.}
12
+ spec.description = %q{Country State Select is a Ruby Gem that aims to make Country and State/Province selection a cinch in Ruby on Rails environments.}
13
+ spec.homepage = "https://github.com/aldefouw/country_state_select.git"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
- spec.files = `git ls-files`.split("\n")
18
- spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- spec.require_paths = ['lib']
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency 'rails', '>= 3.0'
22
- spec.add_runtime_dependency 'city-state'
23
21
  spec.add_development_dependency "bundler", "~> 1.6"
24
- spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency 'pry'
25
+
26
+ spec.add_runtime_dependency 'rails'
27
+ spec.add_runtime_dependency 'simple_form', "~> 3.2"
28
+ spec.add_runtime_dependency 'chosen-rails', "~> 1.4"
29
+ spec.add_runtime_dependency 'compass-rails', '~> 2.0.4'
30
+ spec.add_runtime_dependency 'city-state'
25
31
  end
@@ -1,37 +1,48 @@
1
1
  require "country_state_select/version"
2
- require 'country_state_select/cst_data'
2
+ require "city-state"
3
+ require "rails"
4
+ require "sass-rails"
5
+ require "compass-rails"
6
+ require "chosen-rails"
7
+ require "simple_form"
3
8
 
4
9
  module CountryStateSelect
5
- include CountryStateSelect::CstData
6
-
7
- def self.countries
8
- CountryStateSelect::CstData.countries
10
+
11
+ #Collect the Countries
12
+ def self.countries_collection
13
+ CS.countries.except!(:COUNTRY_ISO_CODE).collect {|p| [ p[ 1], p[0] ] }.compact
9
14
  end
10
15
 
11
- def self.india
12
- CountryStateSelect::CstData.states("IN")
16
+ #Pass array of unwanted countries to get back all except those in the array
17
+ def self.countries_except(*except)
18
+ countries_collection.collect { |c| c unless except.include?(c.second) }.compact
13
19
  end
14
20
 
15
- def self.us_states
16
- CountryStateSelect::CstData.states("US")
21
+ #Return either the State (String) or States (Array)
22
+ def self.states_collection(f, options)
23
+ states = collect_states(f.object.send(options[:country]))
24
+ return f.object.send(options[:state]) if states.size == 0
25
+ states
17
26
  end
18
-
19
- def self.canadian_states
20
- CountryStateSelect::CstData.states("CA")
27
+
28
+ #Return the collected States for a given Country
29
+ def self.collect_states(country)
30
+ CS.states(country).collect {|p| [ p[1], p[0] ] }.compact
21
31
  end
22
-
23
- def self.uk_states
24
- CountryStateSelect::CstData.states("GB")
32
+
33
+ #Return the cities of given state and country
34
+ def self.collect_cities(state_id = '', country_id = '')
35
+ CS.cities(state_id.to_sym, country_id.to_sym)
25
36
  end
26
37
 
27
- #this method will provide the user to opetion to skip any countries in drop down list
28
- def self.countries_except(*except)
29
- countries = []
30
- self.countries.each do |country|
31
- countries<< country unless country.in?(except)
32
- end
33
- return countries
38
+ #Return a hash for use in the simple_form
39
+ def self.state_options(options)
40
+ states = states_collection(options[:form], options[:field_names])
41
+ options = options.merge(collection: states)
42
+ options = options.merge(:as => :string) if states.class == String
43
+ options
34
44
  end
45
+
35
46
  end
36
47
 
37
48
  case ::Rails.version.to_s
@@ -1,3 +1,3 @@
1
1
  module CountryStateSelect
2
- VERSION = "2.0.0"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -0,0 +1,172 @@
1
+ require 'spec_helper'
2
+
3
+ describe CountryStateSelect do
4
+
5
+ describe '#countries_collection' do
6
+ it 'does not include :COUNTRY_ISO_CODE' do
7
+ expect(CountryStateSelect.countries_collection).not_to include([:COUNTRY_ISO_CODE=>"country_name"])
8
+ end
9
+
10
+ it "returns an array" do
11
+ method_call = CountryStateSelect.countries_collection
12
+ expect(method_call.class).to eq(Array)
13
+ end
14
+
15
+ it "returns the value first and the key second" do
16
+ method_call = CountryStateSelect.countries_collection
17
+ expect(method_call.first.first).to eq("Andorra")
18
+ expect(method_call.first.second).to eq(:AD)
19
+ end
20
+ end
21
+
22
+ describe "#countries_except" do
23
+ let(:us) { ["United States", :US] }
24
+ let(:sm) { ["Saint Martin", :MF] }
25
+ let(:exceptions) { [us, sm] }
26
+
27
+ it 'does include the United States ordinarily' do
28
+ expect(CountryStateSelect.countries_except([])).to include(us)
29
+ end
30
+
31
+ it 'does include Saint Martin ordinarily' do
32
+ expect(CountryStateSelect.countries_except([])).to include(sm)
33
+ end
34
+
35
+ it 'does not include the United States or Saint Martin if we exclude them explicitly' do
36
+ expect(CountryStateSelect.countries_except(exceptions)).not_to include(exceptions)
37
+ end
38
+
39
+ it "returns the same result as #countries_collection if no exceptions are passed" do
40
+ expect(CountryStateSelect.countries_except()).to eq(CountryStateSelect.countries_collection)
41
+ end
42
+ end
43
+
44
+ describe '#states_collection' do
45
+ let(:f) { double( object: { } ) }
46
+ let(:options) { { country: 'country_field_name', state: 'state_field_name' } }
47
+
48
+ it "should return 51 elements when we query for the United States" do
49
+ expect(f.object).to receive(:country_field_name).and_return('US') #Set the form Country Field to US
50
+
51
+ method_call = CountryStateSelect.states_collection(f, options)
52
+
53
+ expect(method_call.class).to eq(Array)
54
+ expect(method_call.size).to eq(51)
55
+ end
56
+
57
+ it "should return a String when we query for Saint Martin" do
58
+ expect(f.object).to receive(:country_field_name).and_return('MF') #Set the form Country Field to MF for Saint Martin
59
+ expect(f.object).to receive(:state_field_name).and_return('')
60
+
61
+ method_call = CountryStateSelect.states_collection(f, options)
62
+
63
+ expect(method_call.class).to eq(String)
64
+ expect(method_call.size).to eq(0)
65
+ end
66
+
67
+ it "should return the 'State from Form' if the Country requested returns no States" do
68
+ expect(f.object).to receive(:country_field_name).and_return('MF') #Set the form Country Field to MF for Saint Martin
69
+ expect(f.object).to receive(:state_field_name).and_return('State from Form')
70
+
71
+ method_call = CountryStateSelect.states_collection(f, options)
72
+
73
+ expect(method_call).to eq("State from Form")
74
+ end
75
+ end
76
+
77
+ describe '#states_options' do
78
+ let(:f) { double( object: {} ) }
79
+ let(:options) { { form: f, label: "State / Province", field_names: { :country => :country_field_name, :state => :state_field_name } } }
80
+
81
+ it "should return a Hash" do
82
+ expect(f.object).to receive(:country_field_name).and_return('US')
83
+
84
+ method_call = CountryStateSelect.state_options(options)
85
+ expect(method_call.class).to eq(Hash)
86
+ end
87
+
88
+ it "should contain a key-value pair of as: => :string if no states are returned for the country" do
89
+ expect(f.object).to receive(:country_field_name).and_return('MF')
90
+ expect(f.object).to receive(:state_field_name).and_return('')
91
+
92
+ method_call = CountryStateSelect.state_options(options)
93
+ expect(method_call).to have_key(:as)
94
+ expect(method_call[:as]).to eq(:string)
95
+ end
96
+
97
+ it "should not return a key called 'as' if we want a dropdown" do
98
+ expect(f.object).to receive(:country_field_name).and_return('US')
99
+
100
+ method_call = CountryStateSelect.state_options(options)
101
+ expect(method_call).not_to have_key(:as)
102
+ end
103
+
104
+ it "should have a label if one has been set" do
105
+ expect(f.object).to receive(:country_field_name).and_return('US')
106
+
107
+ method_call = CountryStateSelect.state_options(options)
108
+ expect(method_call).to have_key(:label)
109
+ expect(method_call[:label]).to eq(options[:label])
110
+ end
111
+
112
+ it "should contain a collection of type Array if a country has states" do
113
+ expect(f.object).to receive(:country_field_name).and_return('US')
114
+
115
+ method_call = CountryStateSelect.state_options(options)
116
+ expect(method_call).to have_key(:collection)
117
+ expect(method_call[:collection].class).to eq(Array)
118
+ end
119
+
120
+ it "should contain a collection of type String if a country has no states" do
121
+ expect(f.object).to receive(:country_field_name).and_return('MF')
122
+ expect(f.object).to receive(:state_field_name).and_return('')
123
+
124
+ method_call = CountryStateSelect.state_options(options)
125
+ expect(method_call).to have_key(:collection)
126
+ expect(method_call[:collection].class).to eq(String)
127
+ end
128
+ end
129
+
130
+ describe '#collect_states' do
131
+ it "returns an array" do
132
+ method_call = CountryStateSelect.collect_states('US')
133
+ expect(method_call.class).to eq(Array)
134
+ end
135
+
136
+ it "returns the diffrent value for the key and value" do
137
+ method_call = CountryStateSelect.collect_states('US')
138
+ expect(method_call.first.first).not_to eq(method_call.first.second)
139
+ end
140
+
141
+ it "returns the value part of the key-value pair" do
142
+ method_call = CountryStateSelect.collect_states('US')
143
+ expect(method_call.first.first).to eq("Alaska")
144
+ end
145
+
146
+ it "returns an empty array if there are no states in that Country" do
147
+ method_call = CountryStateSelect.collect_states('MF')
148
+ expect(method_call).to eq([])
149
+ end
150
+ end
151
+
152
+ describe '#collect_cities' do
153
+
154
+ it "returns an array" do
155
+ method_call = CountryStateSelect.collect_cities(:mp, :in)
156
+ expect(method_call.class).to eq(Array)
157
+ end
158
+
159
+ it "it should return same value if city is not duplicate with state with out sending country" do
160
+ city_without_country = CountryStateSelect.collect_cities(:mp, '')
161
+ city_with_country = CountryStateSelect.collect_cities(:mp, :in)
162
+ expect(city_without_country).to eq(city_with_country)
163
+ end
164
+
165
+
166
+ it "returns an empty array if there are no states in that Country" do
167
+ method_call = CountryStateSelect.collect_cities('', '')
168
+ expect(method_call).to eq([])
169
+ end
170
+ end
171
+
172
+ end