Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
74d3cdde
Commit
74d3cdde
authored
Dec 09, 2019
by
Alex Buijs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add API for states by country
Add an API route for fetching states by country
parent
fac7279f
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
1 deletion
+80
-1
config/routes.rb
config/routes.rb
+1
-0
ee/app/controllers/countries_controller.rb
ee/app/controllers/countries_controller.rb
+4
-1
ee/app/controllers/country_states_controller.rb
ee/app/controllers/country_states_controller.rb
+16
-0
ee/changelogs/unreleased/growth-87-states-api.yml
ee/changelogs/unreleased/growth-87-states-api.yml
+5
-0
ee/config/routes/country_state.rb
ee/config/routes/country_state.rb
+3
-0
ee/lib/world.rb
ee/lib/world.rb
+12
-0
ee/spec/controllers/country_states_controller_spec.rb
ee/spec/controllers/country_states_controller_spec.rb
+25
-0
ee/spec/lib/world_spec.rb
ee/spec/lib/world_spec.rb
+14
-0
No files found.
config/routes.rb
View file @
74d3cdde
...
...
@@ -118,6 +118,7 @@ Rails.application.routes.draw do
draw
:trial
draw
:trial_registration
draw
:country
draw
:country_state
end
Gitlab
.
ee
do
...
...
ee/app/controllers/countries_controller.rb
View file @
74d3cdde
...
...
@@ -2,7 +2,10 @@
class
CountriesController
<
ActionController
::
Metal
include
AbstractController
::
Rendering
include
ActionController
::
Renderers
::
All
include
ActionController
::
ApiRendering
include
ActionController
::
Renderers
use_renderers
:json
def
index
countries
=
World
.
countries_for_select
...
...
ee/app/controllers/country_states_controller.rb
0 → 100644
View file @
74d3cdde
# frozen_string_literal: true
class
CountryStatesController
<
ActionController
::
Metal
include
AbstractController
::
Rendering
include
ActionController
::
ApiRendering
include
ActionController
::
Renderers
use_renderers
:json
def
index
country
=
params
[
:country
]
states
=
World
.
states_for_country
(
country
)
render
json:
states
,
status:
(
states
?
:ok
:
:not_found
)
end
end
ee/changelogs/unreleased/growth-87-states-api.yml
0 → 100644
View file @
74d3cdde
---
title
:
Add API for states by country
merge_request
:
21417
author
:
type
:
added
ee/config/routes/country_state.rb
0 → 100644
View file @
74d3cdde
# frozen_string_literal: true
resources
:country_states
,
only:
[
:index
]
ee/lib/world.rb
View file @
74d3cdde
...
...
@@ -10,6 +10,18 @@ module World
strong_memoize
(
:countries_for_select
)
{
all_countries
.
sort_by
(
&
:name
).
map
{
|
c
|
[
c
.
name
,
c
.
alpha2
]
}
}
end
def
states_for_country
(
country_code
)
strong_memoize
(
"states_for_country_
#{
country_code
}
"
)
do
country
=
ISO3166
::
Country
.
find_country_by_alpha2
(
country_code
)
next
unless
country
country
.
states
&
.
reject
{
|
_
,
state
|
state
.
name
.
nil?
}
&
.
sort_by
{
|
_
,
state
|
state
.
name
}
&
.
map
{
|
code
,
state
|
[
state
.
name
,
code
]
}.
to_h
end
end
def
all_countries
strong_memoize
(
:all_countries
)
{
ISO3166
::
Country
.
all
.
reject
{
|
item
|
DENYLIST
.
include?
(
item
.
name
)
}
}
end
...
...
ee/spec/controllers/country_states_controller_spec.rb
0 → 100644
View file @
74d3cdde
# frozen_string_literal: true
require
'spec_helper'
describe
CountryStatesController
do
describe
'GET #index'
do
it
'returns a list of states as json'
do
country
=
'NL'
get
:index
,
params:
{
country:
country
}
expected_json
=
World
.
states_for_country
(
country
).
to_json
expect
(
response
.
status
).
to
eq
(
200
)
expect
(
response
.
body
).
to
eq
(
expected_json
)
end
it
'returns "null" when the provided country is not found'
do
country
=
'NLX'
get
:index
,
params:
{
country:
country
}
expect
(
response
.
status
).
to
eq
(
404
)
expect
(
response
.
body
).
to
eq
(
"null"
)
end
end
end
ee/spec/lib/world_spec.rb
View file @
74d3cdde
...
...
@@ -18,4 +18,18 @@ describe World do
expect
(
result
.
first
).
to
eq
(
%w[Afghanistan AF]
)
end
end
describe
'.states_for_country'
do
it
'returns a list of state names for a country in alphabetical order'
do
result
=
described_class
.
states_for_country
(
'NL'
)
expect
(
result
.
first
).
to
eq
(
%w[Drenthe DR]
)
end
it
'returns nil when given country cannot be found'
do
result
=
described_class
.
states_for_country
(
'NLX'
)
expect
(
result
).
to
be_nil
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment