country_state_select 3.0.0 → 3.1.5
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.
- checksums.yaml +5 -5
- data/.circleci/config.yml +15 -0
- data/.travis.yml +5 -2
- data/Gemfile +2 -0
- data/README.md +57 -42
- data/Rakefile +2 -1
- data/app/controllers/country_state_select/cscs_controller.rb +8 -6
- data/config/routes.rb +4 -2
- data/country_state_select.gemspec +15 -18
- data/lib/country_state_select.rb +44 -29
- data/lib/country_state_select/engine.rb +2 -0
- data/lib/country_state_select/engine3.rb +2 -0
- data/lib/country_state_select/railtie.rb +2 -0
- data/lib/country_state_select/version.rb +3 -1
- data/spec/country_state_select_spec.rb +40 -40
- data/spec/spec_helper.rb +4 -3
- data/vendor/assets/javascript/country_state_select.js.erb +23 -13
- metadata +16 -204
- data/test/dummy/.gitignore +0 -16
- data/test/dummy/Gemfile +0 -44
- data/test/dummy/README.rdoc +0 -3
- data/test/dummy/Rakefile +0 -6
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +0 -19
- data/test/dummy/app/assets/javascripts/locations.coffee +0 -2
- data/test/dummy/app/assets/stylesheets/application.scss +0 -16
- data/test/dummy/app/assets/stylesheets/locations.css.scss +0 -3
- data/test/dummy/app/controllers/application_controller.rb +0 -5
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/controllers/locations_controller.rb +0 -55
- data/test/dummy/app/form/location_form.rb +0 -11
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/models/location.rb +0 -2
- data/test/dummy/app/views/layouts/application.html.erb +0 -16
- data/test/dummy/app/views/locations/_form.html.erb +0 -15
- data/test/dummy/app/views/locations/index.html.erb +0 -29
- data/test/dummy/app/views/locations/new.html.erb +0 -13
- data/test/dummy/app/views/locations/show.html.erb +0 -13
- data/test/dummy/bin/bundle +0 -3
- data/test/dummy/bin/rails +0 -4
- data/test/dummy/bin/rake +0 -4
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -23
- data/test/dummy/config/boot.rb +0 -4
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -29
- data/test/dummy/config/environments/production.rb +0 -80
- data/test/dummy/config/environments/test.rb +0 -36
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/test/dummy/config/initializers/inflections.rb +0 -16
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -12
- data/test/dummy/config/initializers/session_store.rb +0 -3
- data/test/dummy/config/initializers/simple_form.rb +0 -165
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -23
- data/test/dummy/config/locales/simple_form.en.yml +0 -31
- data/test/dummy/config/routes.rb +0 -4
- data/test/dummy/db/migrate/20140724080030_create_locations.rb +0 -10
- data/test/dummy/db/migrate/20151024160724_add_test_city_to_locations.rb +0 -5
- data/test/dummy/db/schema.rb +0 -24
- data/test/dummy/db/seeds.rb +0 -7
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/lib/tasks/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +0 -58
- data/test/dummy/public/422.html +0 -58
- data/test/dummy/public/500.html +0 -57
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/robots.txt +0 -5
- data/test/dummy/test/controllers/.keep +0 -0
- data/test/dummy/test/fixtures/.keep +0 -0
- data/test/dummy/test/helpers/.keep +0 -0
- data/test/dummy/test/integration/.keep +0 -0
- data/test/dummy/test/mailers/.keep +0 -0
- data/test/dummy/test/models/.keep +0 -0
- data/test/dummy/test/test_helper.rb +0 -15
- data/test/dummy/vendor/assets/javascripts/.keep +0 -0
- data/test/dummy/vendor/assets/stylesheets/.keep +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e19a1973258042a873d741682f8b4fa4eadfe1191cc988bc4bb8efa1785fd108
|
4
|
+
data.tar.gz: e68fdf8aa8c862cc844337862e00646bc71727c607ec38fcc27d947246a9ffc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4807a8138b62e598471aa6edc0f32e78a8c84fa47b9f79fbcc551af5a755fad6347e1cead2a0d201f60c766c54b16a7c827f4f0c5fe118d08f52204052697a0a
|
7
|
+
data.tar.gz: 1ea10b63f82e6f4f94de47fffceaed21934a6ca01c78efbdc110a43e4c36d45a34f2dc2dfefcf828dbe9be19b2b8ccd7355ba557f80b73555e09bf2d9d7d6dca
|
@@ -0,0 +1,15 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
ruby: circleci/ruby@0.1.2
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.6.3-stretch-node
|
9
|
+
executor: ruby/default
|
10
|
+
steps:
|
11
|
+
- checkout
|
12
|
+
- run:
|
13
|
+
name: Which bundler?
|
14
|
+
command: bundle -v
|
15
|
+
- ruby/bundle-install
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
##Country State Select
|
1
|
+
## Country State Select
|
2
|
+
[](https://travis-ci.org/arvindvyas/Country-State-Select)
|
3
|
+
[](https://codeclimate.com/github/arvindvyas/Country-State-Select)
|
4
|
+
|
5
|
+
|
2
6
|
|
3
7
|
Country State Select is a library that provides an easy API to generate Country , State / Province and City dropdowns for use in forms.
|
4
8
|
|
@@ -8,7 +12,10 @@ For instance, if a user chooses "United States of America" for a Country dropdow
|
|
8
12
|
|
9
13
|
The data for Countries and States is populated by the excellent [city-state](https://github.com/loureirorg/city-state) gem.
|
10
14
|
|
11
|
-
#
|
15
|
+
# Demo
|
16
|
+
https://country-state-select.herokuapp.com
|
17
|
+
|
18
|
+
# Getting Started
|
12
19
|
|
13
20
|
Country State Select is released as a Ruby Gem and requires the Rails environment as well.
|
14
21
|
|
@@ -20,9 +27,13 @@ Then run
|
|
20
27
|
|
21
28
|
bundle install
|
22
29
|
|
30
|
+
# NOTE
|
31
|
+
In the latest version we have removed all the dependencies of js and you are feel free to use any js you can use predefine method to populate data
|
23
32
|
|
24
33
|
Don't forget to restart your server after you install it.
|
25
|
-
|
34
|
+
## Setup Gem
|
35
|
+
if not autoloaded than please add in your application.js
|
36
|
+
//= require country_state_select
|
26
37
|
|
27
38
|
## chosen JavaScript assets
|
28
39
|
The application makes use of the chosen-rails library for a better user experience. This can be disabled if desired.
|
@@ -50,11 +61,11 @@ Don't forget to restart your server after you install it.
|
|
50
61
|
Implementation in a view is simple.
|
51
62
|
|
52
63
|
Just create a simple_form object like so:
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
64
|
+
```ruby
|
65
|
+
<%= simple_form_for @form_object do |f|
|
66
|
+
f.button :submit
|
67
|
+
end %>
|
68
|
+
```
|
58
69
|
Once your form object is created, add in fields for your user to select a Country and State / Province.
|
59
70
|
|
60
71
|
## Country Field
|
@@ -86,18 +97,19 @@ Any options that are valid in simple_form field can be passed into the options h
|
|
86
97
|
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
98
|
|
88
99
|
Add this JavaScript to the page that is loading your form:
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
100
|
+
```javascript
|
101
|
+
$(document).on('ready page:load', function() {
|
102
|
+
return CountryStateSelect({
|
103
|
+
country_id: "country_field_id",
|
104
|
+
state_id: "state_field_id"
|
105
|
+
});
|
106
|
+
});
|
107
|
+
```
|
97
108
|
If you are using CoffeeScript, use the following:
|
98
|
-
|
99
|
-
|
100
|
-
|
109
|
+
```coffescript
|
110
|
+
$(document).on 'ready page:load', ->
|
111
|
+
CountryStateSelect({ country_id: "country_field_id", state_id: "state_field_id" })
|
112
|
+
```
|
101
113
|
|
102
114
|
The country_id accepts the form ID for the country field. The state_id accepts the form ID for the state field.
|
103
115
|
|
@@ -112,29 +124,32 @@ The ID being referred to is like the following in a select menu:
|
|
112
124
|
The CountryStateSelect interface offers the option to turn the Chosen library on or off.
|
113
125
|
|
114
126
|
To turn it on, set it to true:
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
127
|
+
```javascript
|
128
|
+
$(document).on('ready page:load', function() {
|
129
|
+
return CountryStateSelect({
|
130
|
+
chosen_ui: true,
|
131
|
+
chosen_options: {
|
132
|
+
disable_search_threshold: 10,
|
133
|
+
width: '95%'
|
134
|
+
},
|
135
|
+
...
|
136
|
+
...
|
137
|
+
...
|
138
|
+
});
|
139
|
+
});
|
140
|
+
```
|
125
141
|
To turn it off, set it to false:
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
##Contributing
|
138
|
-
|
142
|
+
```javascript
|
143
|
+
$(document).on('ready page:load', function() {
|
144
|
+
return CountryStateSelect({
|
145
|
+
chosen_ui: false,
|
146
|
+
...
|
147
|
+
...
|
148
|
+
...
|
149
|
+
});
|
150
|
+
});
|
151
|
+
```
|
152
|
+
|
153
|
+
## Contributing
|
139
154
|
Fork, fix, then send a pull request.
|
140
155
|
|
data/Rakefile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @author : Arvind Vyas
|
2
4
|
module CountryStateSelect
|
3
5
|
class CscsController < ApplicationController
|
4
6
|
def find_states
|
5
|
-
csc =
|
7
|
+
csc = CS.states(params[:country_id])
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
+
respond_to do |format|
|
10
|
+
format.json { render json: csc.to_a }
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
@@ -14,8 +16,8 @@ module CountryStateSelect
|
|
14
16
|
cities = CS.cities(params[:state_id].to_sym, params[:country_id].to_sym)
|
15
17
|
|
16
18
|
respond_to do |format|
|
17
|
-
|
19
|
+
format.json { render json: cities.to_a }
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
21
|
-
end
|
23
|
+
end
|
data/config/routes.rb
CHANGED
@@ -1,31 +1,28 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'country_state_select/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'country_state_select'
|
8
9
|
spec.version = CountryStateSelect::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
10
|
+
spec.authors = ['Arvind Vyas']
|
11
|
+
spec.email = ['arvindvyas07@gmail.com']
|
12
|
+
spec.summary = 'Dynamically select Country and State.'
|
13
|
+
spec.description = 'Country State Select is a Ruby Gem that aims to make Country and State/Province selection a cinch in Ruby on Rails environments.'
|
14
|
+
spec.homepage = 'https://github.com/arvindvyas/Country-State-Select'
|
15
|
+
spec.license = 'MIT'
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0")
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency 'pry'
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'rake', '~> 12.3.3'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
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
26
|
spec.add_runtime_dependency 'city-state'
|
27
|
+
spec.add_runtime_dependency 'rails'
|
31
28
|
end
|
data/lib/country_state_select.rb
CHANGED
@@ -1,57 +1,72 @@
|
|
1
|
-
|
2
|
-
require "city-state"
|
3
|
-
require "rails"
|
4
|
-
require "sass-rails"
|
5
|
-
require "compass-rails"
|
6
|
-
require "chosen-rails"
|
7
|
-
require "simple_form"
|
1
|
+
# frozen_string_literal: true
|
8
2
|
|
9
|
-
|
3
|
+
require 'country_state_select/version'
|
4
|
+
require 'city-state'
|
5
|
+
require 'rails'
|
10
6
|
|
11
|
-
|
7
|
+
module CountryStateSelect
|
8
|
+
# Collect the Countries
|
12
9
|
def self.countries_collection
|
13
|
-
CS.countries.except!(:COUNTRY_ISO_CODE).collect {|p| [
|
10
|
+
CS.countries.except!(:COUNTRY_ISO_CODE).collect { |p| [p[1], p[0]] }.compact
|
14
11
|
end
|
15
12
|
|
16
|
-
#Pass array of unwanted countries to get back all except those in the array
|
13
|
+
# Pass array of unwanted countries to get back all except those in the array
|
17
14
|
def self.countries_except(*except)
|
18
|
-
countries_collection.collect { |c| c unless except.include?(c
|
15
|
+
countries_collection.collect { |c| c unless except.include?(c[1]) }.compact
|
19
16
|
end
|
20
17
|
|
21
|
-
#Return either the State (String) or States (Array)
|
18
|
+
# Return either the State (String) or States (Array)
|
22
19
|
def self.states_collection(f, options)
|
23
20
|
states = collect_states(f.object.send(options[:country]))
|
24
|
-
return f.object.send(options[:state]) if states.
|
21
|
+
return f.object.send(options[:state]) if states.empty?
|
22
|
+
|
25
23
|
states
|
26
24
|
end
|
27
25
|
|
28
|
-
#Return the
|
26
|
+
# Return either the City (String) or Cities (Array)
|
27
|
+
def self.cities_collection(f, options)
|
28
|
+
cities = collect_cities(f.object.send(options[:state]))
|
29
|
+
end
|
30
|
+
|
31
|
+
# Return the collected States for a given Country
|
29
32
|
def self.collect_states(country)
|
30
|
-
CS.states(country).collect {|p| [
|
33
|
+
CS.states(country).collect { |p| [p[1], p[0]] }.compact
|
31
34
|
end
|
32
35
|
|
33
|
-
#Return the cities of given state and country
|
36
|
+
# Return the cities of given state and country
|
34
37
|
def self.collect_cities(state_id = '', country_id = '')
|
38
|
+
return [] if state_id.nil? || country_id.nil?
|
39
|
+
|
35
40
|
CS.cities(state_id.to_sym, country_id.to_sym)
|
36
41
|
end
|
37
42
|
|
38
|
-
#Return a hash for use in the simple_form
|
43
|
+
# Return a hash for use in the simple_form
|
39
44
|
def self.state_options(options)
|
40
45
|
states = states_collection(options[:form], options[:field_names])
|
41
|
-
options = options
|
42
|
-
|
43
|
-
|
46
|
+
options = merge_hash(options, states)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Return a hash for use in the simple_form
|
50
|
+
def self.city_options(options)
|
51
|
+
cities = cities_collection(options[:form], options[:field_names])
|
52
|
+
options = merge_hash(options, cities)
|
44
53
|
end
|
45
54
|
|
55
|
+
# Create hash to use in the simple_form
|
56
|
+
def self.merge_hash(options, collections)
|
57
|
+
options = options.merge(collection: collections)
|
58
|
+
options = options.merge(as: :string) if collections.instance_of?(String)
|
59
|
+
options
|
60
|
+
end
|
46
61
|
end
|
47
62
|
|
48
63
|
case ::Rails.version.to_s
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
64
|
+
when /[4-6]/
|
65
|
+
require 'country_state_select/engine'
|
66
|
+
when /^3\.[12]/
|
67
|
+
require 'country_state_select/engine3'
|
68
|
+
when /^3\.0/
|
69
|
+
require 'country_state_select/railtie'
|
70
|
+
else
|
71
|
+
raise 'Unsupported rails version'
|
57
72
|
end
|
@@ -1,28 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe CountryStateSelect do
|
4
|
-
|
5
6
|
describe '#countries_collection' do
|
6
7
|
it 'does not include :COUNTRY_ISO_CODE' do
|
7
|
-
expect(CountryStateSelect.countries_collection).not_to include([:
|
8
|
+
expect(CountryStateSelect.countries_collection).not_to include([COUNTRY_ISO_CODE: 'country_name'])
|
8
9
|
end
|
9
10
|
|
10
|
-
it
|
11
|
+
it 'returns an array' do
|
11
12
|
method_call = CountryStateSelect.countries_collection
|
12
13
|
expect(method_call.class).to eq(Array)
|
13
14
|
end
|
14
15
|
|
15
|
-
it
|
16
|
+
it 'returns the value first and the key second' do
|
16
17
|
method_call = CountryStateSelect.countries_collection
|
17
|
-
expect(method_call
|
18
|
-
expect(method_call
|
18
|
+
expect(method_call[0][0]).to eq('Andorra')
|
19
|
+
expect(method_call[0][1]).to eq(:AD)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
describe
|
23
|
-
let(:us) { [
|
24
|
-
let(:sm) { [
|
25
|
-
let(:exceptions) {
|
23
|
+
describe '#countries_except' do
|
24
|
+
let(:us) { ['United States', :US] }
|
25
|
+
let(:sm) { ['Saint Martin', :MF] }
|
26
|
+
let(:exceptions) { [us, sm] }
|
26
27
|
|
27
28
|
it 'does include the United States ordinarily' do
|
28
29
|
expect(CountryStateSelect.countries_except([])).to include(us)
|
@@ -36,17 +37,17 @@ describe CountryStateSelect do
|
|
36
37
|
expect(CountryStateSelect.countries_except(exceptions)).not_to include(exceptions)
|
37
38
|
end
|
38
39
|
|
39
|
-
it
|
40
|
-
expect(CountryStateSelect.countries_except
|
40
|
+
it 'returns the same result as #countries_collection if no exceptions are passed' do
|
41
|
+
expect(CountryStateSelect.countries_except).to eq(CountryStateSelect.countries_collection)
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
45
|
describe '#states_collection' do
|
45
|
-
let(:f) { double(
|
46
|
+
let(:f) { double(object: {}) }
|
46
47
|
let(:options) { { country: 'country_field_name', state: 'state_field_name' } }
|
47
48
|
|
48
|
-
it
|
49
|
-
expect(f.object).to receive(:country_field_name).and_return('US') #Set the form Country Field to US
|
49
|
+
it 'should return 51 elements when we query for the United States' do
|
50
|
+
expect(f.object).to receive(:country_field_name).and_return('US') # Set the form Country Field to US
|
50
51
|
|
51
52
|
method_call = CountryStateSelect.states_collection(f, options)
|
52
53
|
|
@@ -54,8 +55,8 @@ describe CountryStateSelect do
|
|
54
55
|
expect(method_call.size).to eq(51)
|
55
56
|
end
|
56
57
|
|
57
|
-
it
|
58
|
-
expect(f.object).to receive(:country_field_name).and_return('MF') #Set the form Country Field to MF for Saint Martin
|
58
|
+
it 'should return a String when we query for Saint Martin' do
|
59
|
+
expect(f.object).to receive(:country_field_name).and_return('MF') # Set the form Country Field to MF for Saint Martin
|
59
60
|
expect(f.object).to receive(:state_field_name).and_return('')
|
60
61
|
|
61
62
|
method_call = CountryStateSelect.states_collection(f, options)
|
@@ -65,27 +66,29 @@ describe CountryStateSelect do
|
|
65
66
|
end
|
66
67
|
|
67
68
|
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(:country_field_name).and_return('MF') # Set the form Country Field to MF for Saint Martin
|
69
70
|
expect(f.object).to receive(:state_field_name).and_return('State from Form')
|
70
71
|
|
71
72
|
method_call = CountryStateSelect.states_collection(f, options)
|
72
73
|
|
73
|
-
expect(method_call).to eq(
|
74
|
+
expect(method_call).to eq('State from Form')
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
77
78
|
describe '#states_options' do
|
78
|
-
let(:f) { double(
|
79
|
-
let(:options)
|
79
|
+
let(:f) { double(object: {}) }
|
80
|
+
let(:options) do
|
81
|
+
{ form: f, label: 'State / Province', field_names: { country: :country_field_name, state: :state_field_name } }
|
82
|
+
end
|
80
83
|
|
81
|
-
it
|
84
|
+
it 'should return a Hash' do
|
82
85
|
expect(f.object).to receive(:country_field_name).and_return('US')
|
83
86
|
|
84
87
|
method_call = CountryStateSelect.state_options(options)
|
85
88
|
expect(method_call.class).to eq(Hash)
|
86
89
|
end
|
87
90
|
|
88
|
-
it
|
91
|
+
it 'should contain a key-value pair of as: => :string if no states are returned for the country' do
|
89
92
|
expect(f.object).to receive(:country_field_name).and_return('MF')
|
90
93
|
expect(f.object).to receive(:state_field_name).and_return('')
|
91
94
|
|
@@ -101,7 +104,7 @@ describe CountryStateSelect do
|
|
101
104
|
expect(method_call).not_to have_key(:as)
|
102
105
|
end
|
103
106
|
|
104
|
-
it
|
107
|
+
it 'should have a label if one has been set' do
|
105
108
|
expect(f.object).to receive(:country_field_name).and_return('US')
|
106
109
|
|
107
110
|
method_call = CountryStateSelect.state_options(options)
|
@@ -109,7 +112,7 @@ describe CountryStateSelect do
|
|
109
112
|
expect(method_call[:label]).to eq(options[:label])
|
110
113
|
end
|
111
114
|
|
112
|
-
it
|
115
|
+
it 'should contain a collection of type Array if a country has states' do
|
113
116
|
expect(f.object).to receive(:country_field_name).and_return('US')
|
114
117
|
|
115
118
|
method_call = CountryStateSelect.state_options(options)
|
@@ -117,7 +120,7 @@ describe CountryStateSelect do
|
|
117
120
|
expect(method_call[:collection].class).to eq(Array)
|
118
121
|
end
|
119
122
|
|
120
|
-
it
|
123
|
+
it 'should contain a collection of type String if a country has no states' do
|
121
124
|
expect(f.object).to receive(:country_field_name).and_return('MF')
|
122
125
|
expect(f.object).to receive(:state_field_name).and_return('')
|
123
126
|
|
@@ -128,45 +131,42 @@ describe CountryStateSelect do
|
|
128
131
|
end
|
129
132
|
|
130
133
|
describe '#collect_states' do
|
131
|
-
it
|
134
|
+
it 'returns an array' do
|
132
135
|
method_call = CountryStateSelect.collect_states('US')
|
133
136
|
expect(method_call.class).to eq(Array)
|
134
137
|
end
|
135
138
|
|
136
|
-
it
|
139
|
+
it 'returns the diffrent value for the key and value' do
|
137
140
|
method_call = CountryStateSelect.collect_states('US')
|
138
|
-
expect(method_call
|
141
|
+
expect(method_call[0][0]).not_to eq(method_call[0][1])
|
139
142
|
end
|
140
143
|
|
141
|
-
it
|
144
|
+
it 'returns the value part of the key-value pair' do
|
142
145
|
method_call = CountryStateSelect.collect_states('US')
|
143
|
-
expect(method_call.first.first).to eq(
|
146
|
+
expect(method_call.first.first).to eq('Alaska')
|
144
147
|
end
|
145
148
|
|
146
|
-
it
|
149
|
+
it 'returns an empty array if there are no states in that Country' do
|
147
150
|
method_call = CountryStateSelect.collect_states('MF')
|
148
151
|
expect(method_call).to eq([])
|
149
152
|
end
|
150
153
|
end
|
151
154
|
|
152
155
|
describe '#collect_cities' do
|
153
|
-
|
154
|
-
it "returns an array" do
|
156
|
+
it 'returns an array' do
|
155
157
|
method_call = CountryStateSelect.collect_cities(:mp, :in)
|
156
158
|
expect(method_call.class).to eq(Array)
|
157
159
|
end
|
158
160
|
|
159
|
-
it
|
161
|
+
it 'it should return same value if city is not duplicate with state with out sending country' do
|
160
162
|
city_without_country = CountryStateSelect.collect_cities(:mp, '')
|
161
163
|
city_with_country = CountryStateSelect.collect_cities(:mp, :in)
|
162
164
|
expect(city_without_country).to eq(city_with_country)
|
163
165
|
end
|
164
166
|
|
165
|
-
|
166
|
-
it "returns an empty array if there are no states in that Country" do
|
167
|
+
it 'returns an empty array if there are no states in that Country' do
|
167
168
|
method_call = CountryStateSelect.collect_cities('', '')
|
168
|
-
expect(method_call).to eq(
|
169
|
+
expect(method_call).to eq(nil)
|
169
170
|
end
|
170
171
|
end
|
171
|
-
|
172
|
-
end
|
172
|
+
end
|