china_cities 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +60 -0
  4. data/Rakefile +40 -0
  5. data/app/assets/javascripts/jquery.china_cities.js.coffee +17 -0
  6. data/app/controllers/china_cities/application_controller.rb +11 -0
  7. data/app/controllers/china_cities/districts_controller.rb +27 -0
  8. data/app/views/sharp_cities/index.js.erb +1 -0
  9. data/config/routes.rb +6 -0
  10. data/db/cities.json +1 -0
  11. data/db/city_code.rb +27104 -0
  12. data/db/zones.db +0 -0
  13. data/lib/china_cities/engine.rb +6 -0
  14. data/lib/china_cities/version.rb +3 -0
  15. data/lib/china_cities/view_helpers.rb +12 -0
  16. data/lib/china_cities.rb +5 -0
  17. data/lib/generators/china_cities/china_cities_generator.rb +26 -0
  18. data/lib/generators/china_cities/templates/china_city.rb +9 -0
  19. data/lib/generators/china_cities/templates/create_china_cities.rb +18 -0
  20. data/lib/generators/china_cities/templates/import_china_cities.rb +34 -0
  21. data/test/dummy/README.rdoc +261 -0
  22. data/test/dummy/Rakefile +7 -0
  23. data/test/dummy/app/assets/javascripts/application.js +15 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/test/dummy/app/controllers/application_controller.rb +3 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/test/dummy/config/application.rb +59 -0
  29. data/test/dummy/config/boot.rb +10 -0
  30. data/test/dummy/config/database.yml +25 -0
  31. data/test/dummy/config/environment.rb +5 -0
  32. data/test/dummy/config/environments/development.rb +37 -0
  33. data/test/dummy/config/environments/production.rb +67 -0
  34. data/test/dummy/config/environments/test.rb +37 -0
  35. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/dummy/config/initializers/inflections.rb +15 -0
  37. data/test/dummy/config/initializers/mime_types.rb +5 -0
  38. data/test/dummy/config/initializers/secret_token.rb +7 -0
  39. data/test/dummy/config/initializers/session_store.rb +8 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +5 -0
  42. data/test/dummy/config/routes.rb +4 -0
  43. data/test/dummy/config.ru +4 -0
  44. data/test/dummy/public/404.html +26 -0
  45. data/test/dummy/public/422.html +26 -0
  46. data/test/dummy/public/500.html +25 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/fixtures/sharp_zones/cities.yml +11 -0
  50. data/test/fixtures/sharp_zones/provinces.yml +11 -0
  51. data/test/fixtures/sharp_zones/zones.yml +11 -0
  52. data/test/functional/sharp_zones/sharp_cities_controller_test.rb +9 -0
  53. data/test/functional/sharp_zones/sharp_zones_controller_test.rb +9 -0
  54. data/test/integration/navigation_test.rb +10 -0
  55. data/test/sharp_zones_test.rb +7 -0
  56. data/test/test_helper.rb +15 -0
  57. data/test/unit/helpers/sharp_zones/sharp_cities_helper_test.rb +6 -0
  58. data/test/unit/helpers/sharp_zones/sharp_zones_helper_test.rb +6 -0
  59. data/test/unit/sharp_zones/city_test.rb +9 -0
  60. data/test/unit/sharp_zones/province_test.rb +9 -0
  61. data/test/unit/sharp_zones/zone_test.rb +9 -0
  62. metadata +173 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db292cac98962753d12c92cf7238a929961f5412
4
+ data.tar.gz: 841a2a0d9a1306b2b6ba28baa8e2638b172261e3
5
+ SHA512:
6
+ metadata.gz: 0d109dd3c46ae1a44d5663a56ca0877847b8588b7e2dc27f36ee1f407f4e8050032fe1431854119be01cd50335dc43c104bfa312f75575606febb4d2bcba3b35
7
+ data.tar.gz: d32949af7fc314a678213964ccb7012b3594e5ecc259166a980035f91cb9053d400735aacca4d66c119bcf7a21054a7c94c42495a23ea6ae2a789829872b11e1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ gem 'china_cities'
6
+
7
+
8
+ And then execute:
9
+
10
+ bundle
11
+
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Run generator:
17
+
18
+ ```
19
+ rails g china install
20
+
21
+ rake db:migrate
22
+
23
+ ```
24
+
25
+ Add this line app/assets/javascripts/application.js
26
+
27
+ ```
28
+ //= require 'jquery.china_cities'
29
+ ```
30
+
31
+
32
+ Update config/routes.rb
33
+
34
+ ```
35
+ mount ChinaCities::Engine => '/china_cities'
36
+
37
+ ```
38
+
39
+
40
+ In your view add:
41
+
42
+ ```
43
+ .city-by-name
44
+ .select.city-select
45
+ = options_for_select(ChinaCity.provinces.map{|city|{city.name, city.name})
46
+ .select.city-select
47
+ .select.city-select
48
+
49
+ :coffee
50
+ $('.city-by-name').china_city('by_name')
51
+
52
+ ```
53
+
54
+ Notice:`.city-by-name` means it will generate the options with the city name value, and you can choose `.city-by-id` and `.city-by-code`.
55
+
56
+
57
+ LICENSE.
58
+ -------------------------
59
+
60
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'SharpZones'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ (($) ->
2
+ $.fn.china_city = (type, locale='zh-CN') ->
3
+ @each ->
4
+ console.log(type)
5
+ selects = $(@).find('.city-select')
6
+ selects.change ->
7
+ $this = $(@)
8
+ next_selects = selects.slice(selects.index(@) + 1) # empty all children city
9
+ $("option:gt(0)", next_selects).remove()
10
+ if next_selects.first()[0] and $this.val() and !$this.val().match(/--.*--/) # init next child
11
+ url = "/china_cities/#{type}/#{$(@).val()}?locale=#{locale}"
12
+
13
+ $.get url, (data) ->
14
+ data = data.data if data.data?
15
+ next_selects.first()[0].options.add(new Option(option[0], option[1])) for option in data
16
+ next_selects.trigger('china_city:load_data_completed');
17
+ )(jQuery)
@@ -0,0 +1,11 @@
1
+ module ChinaCities
2
+ class ApplicationController < ActionController::Base
3
+ before_filter :set_locale
4
+
5
+ def set_locale
6
+ if params[:locale]
7
+ I18n.locale = params[:locale]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ module ChinaCities
2
+ class DistrictsController < ApplicationController
3
+ respond_to :html, :json
4
+
5
+ def json_file
6
+ send_file "#{ChinaCities::Engine.root.to_s}/db/cities.json"
7
+ end
8
+
9
+ def by_name
10
+ city = ChinaCity.where(name: params[:name]).first
11
+ cities = ChinaCity.where(parent_id: city.id).map{|city|[city.name, city.name]}
12
+ render json: cities.to_json, layout: nil
13
+ end
14
+
15
+ def by_id
16
+ city = ChinaCity.find params[:id]
17
+ cities = ChinaCity.where(parent_id: city.id).map{|city|[city.name, city.id]}
18
+ render json: cities.to_json, layout: nil
19
+ end
20
+
21
+ def by_code
22
+ city = ChinaCity.where(code: params[:code]).first
23
+ cities = ChinaCity.where(parent_id: city.id).map{|city|[city.name, city.code]}
24
+ render json: cities.to_json, layout: nil
25
+ end
26
+ end
27
+ end
@@ -0,0 +1 @@
1
+ $("#city_code").empty().append("<%= escape_javascript(options_for_select(@cities.map{|i|[i.name, i.code]})) %>");
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ ChinaCities::Engine.routes.draw do
2
+ get "by_name/:name" => "districts#by_name"
3
+ get "by_id/:id" => "districts#by_id"
4
+ get "by_code/:code" => "districts#by_code"
5
+ get "json_file" => "districts#json_file"
6
+ end