china_region_fu 0.0.4 → 0.0.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.
@@ -0,0 +1,16 @@
1
+ require 'china_region_fu/helpers/utilities'
2
+
3
+ class RegionInput < Formtastic::Inputs::SelectInput
4
+ include ChinaRegionFu::Utilities
5
+
6
+ def collection_from_options
7
+ return [] unless options.key?(:collection)
8
+ super
9
+ end
10
+
11
+ def input_html_options
12
+ the_options = append_region_class(super.dup)
13
+ the_options = set_html_options(object_name, input_name, the_options, input_options.delete(:sub_region).to_s.sub(/_id\Z/, '').foreign_key) if input_options.key?(:sub_region)
14
+ the_options
15
+ end
16
+ end
@@ -0,0 +1,70 @@
1
+ require 'china_region_fu/exceptions'
2
+ require 'china_region_fu/helpers/utilities'
3
+
4
+ module ChinaRegionFu
5
+ module Helpers
6
+ include Utilities
7
+
8
+ def region_select_tag(names, options = {})
9
+ append_region_class(options)
10
+
11
+ if Array === names
12
+ output = ActiveSupport::SafeBuffer.new
13
+ names.each_with_index do |name, index|
14
+ if klass = to_class(name)
15
+ choices = index == 0 ? options_from_collection_for_select(klass.select('id, name'), "id", "name") : ''
16
+ next_name = names.at(index + 1)
17
+ set_html_options(nil, name, options, next_name)
18
+
19
+ output << content_tag(:div, select_tag(name, choices, options.merge(prompt: options.delete("#{name}_prompt".to_sym))), class: "input region #{name.to_s}")
20
+ else
21
+ raise InvalidAttributeError
22
+ end
23
+ end
24
+ output << js_for_region_ajax if names.size > 1
25
+ output
26
+ else
27
+ if klass = to_class(names)
28
+ select_tag(names, options_from_collection_for_select(klass.select('id, name'), "id", "name"), options)
29
+ else
30
+ raise InvalidAttributeError
31
+ end
32
+ end
33
+ end
34
+
35
+ def region_select(object, methods, options = {}, html_options = {})
36
+ options.symbolize_keys!
37
+ html_options.symbolize_keys!
38
+ append_region_class(html_options)
39
+
40
+ if Array === methods
41
+ output = ActiveSupport::SafeBuffer.new
42
+ methods.each_with_index do |method, index|
43
+ if klass = to_class(method)
44
+ choices = index == 0 ? klass.select('id, name').collect {|p| [ p.name, p.id ] } : []
45
+ next_method = methods.at(index + 1)
46
+ set_html_options(object, method, html_options, next_method)
47
+
48
+ output << content_tag(:div, select(object, method.to_s, choices, options.merge(prompt: options.delete("#{method}_prompt".to_sym)), html_options = html_options), class: "input region #{method.to_s}")
49
+ else
50
+ raise InvalidAttributeError
51
+ end
52
+ end
53
+ output << js_for_region_ajax if methods.size > 1
54
+ output
55
+ else
56
+ if klass = to_class(methods)
57
+ content_tag(:div, select(object, methods, klass.select('id, name').collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options), class: "input region #{methods.to_s}")
58
+ else
59
+ raise InvalidAttributeError
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ module FormBuilder
66
+ def region_select(methods, options = {}, html_options = {})
67
+ @template.region_select(@object_name, methods, options = options, html_options = html_options)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,23 @@
1
+ require 'china_region_fu/helpers/utilities'
2
+
3
+ module ChinaRegionFu
4
+ module SimpleForm
5
+ class RegionInput < ::SimpleForm::Inputs::CollectionInput
6
+ include ChinaRegionFu::Utilities
7
+
8
+ def input
9
+ label_method, value_method = detect_collection_methods
10
+ append_region_class(input_html_options)
11
+ set_html_options(object_name, attribute_name, input_html_options, input_options.delete(:sub_region)) if input_options.key?(:sub_region)
12
+ region_collection = collection
13
+ region_collection = [] if region_collection == ::SimpleForm::Inputs::CollectionInput.boolean_collection
14
+ @builder.collection_select(
15
+ attribute_name, region_collection, value_method, label_method,
16
+ input_options, input_html_options
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ ::SimpleForm::FormBuilder.map_type :region, to: ChinaRegionFu::SimpleForm::RegionInput
@@ -0,0 +1,54 @@
1
+ module ChinaRegionFu
2
+ module Utilities
3
+ def js_for_region_ajax
4
+ js = <<-JAVASCRIPT
5
+ <script type="text/javascript">
6
+ //<![CDATA[
7
+ $(function(){
8
+ $('body').on('change', '.region_select', function(event) {
9
+ var self, $targetDom;
10
+ self = $(event.currentTarget);
11
+ $targetDom = $('#' + self.data('region-target'));
12
+ if ($targetDom.size() > 0) {
13
+ $.getJSON('/china_region_fu/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
14
+ var options = [];
15
+ $('option[value!=""]', $targetDom).remove();
16
+ $.each(data, function(index, value) {
17
+ options.push("<option value='" + value.id + "'>" + value.name + "</option>");
18
+ });
19
+ $targetDom.append(options.join(''));
20
+ });
21
+ }
22
+ });
23
+ });
24
+ //]]>
25
+ </script>
26
+ JAVASCRIPT
27
+ js.html_safe
28
+ end
29
+
30
+ def set_html_options(object, method, html_options, next_region)
31
+ region_klass_name = method.to_s#.sub(/_id\Z/, '')
32
+ next_region_klass_name = next_region.to_s#.sub(/_id\Z/, '')
33
+ html_options[:data] ? (html_options[:data][:region_klass] = region_klass_name) : (html_options[:data] = { region_klass: region_klass_name })
34
+ if next_region
35
+ region_target = object ? "#{object}_#{next_region.to_s}" : next_region.to_s
36
+ html_options[:data].merge!(region_target: region_target, region_target_kalss: next_region_klass_name)
37
+ else
38
+ html_options[:data].delete(:region_target)
39
+ html_options[:data].delete(:region_target_kalss)
40
+ end
41
+ html_options
42
+ end
43
+
44
+ def to_class(str_name)
45
+ return nil if str_name.blank?
46
+ str_name.to_s.sub(/_id\Z/, '').classify.safe_constantize
47
+ end
48
+
49
+ def append_region_class(options)
50
+ options[:class] ? (options[:class].prepend('region_select ')) : (options[:class] = 'region_select')
51
+ options
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module ChinaRegionFu
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -6,15 +6,16 @@ module ChinaRegionFu
6
6
  def copy_migration_file
7
7
  migration_template "migration.rb", "db/migrate/create_china_region_tables.rb"
8
8
  end
9
-
10
- def copy_region_config_file
11
- copy_file 'regions.yml', 'config/regions.yml'
9
+
10
+ desc "Download https://github.com/Xuhao/china_region_data/raw/master/regions.yml to config/regions.yml."
11
+ def download_region_config_file
12
+ get 'https://github.com/Xuhao/china_region_data/raw/master/regions.yml', 'config/regions.yml'
12
13
  end
13
-
14
+
14
15
  def execute_migrate
15
16
  rake("db:migrate")
16
17
  end
17
-
18
+
18
19
  def import_region_to_data
19
20
  rake('region:import')
20
21
  end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Copy activerecord models to app/models folder
3
+
4
+ Example:
5
+ rails generate china_region_fu:models
6
+
7
+ This will create:
8
+ app/models/province.rb
9
+ app/models/city.rb
10
+ app/models/district.rb
@@ -1,8 +1,8 @@
1
1
  module ChinaRegionFu
2
- class MvcGenerator < Rails::Generators::NamedBase
2
+ class ModelsGenerator < Rails::Generators::Base
3
3
  source_root File.expand_path('../../../../../app', __FILE__)
4
4
 
5
- def copy_model_file
5
+ def copy_model_files
6
6
  copy_file "models/province.rb", "app/models/province.rb"
7
7
  copy_file "models/city.rb", "app/models/city.rb"
8
8
  copy_file "models/district.rb", "app/models/district.rb"
@@ -10,7 +10,7 @@ namespace :region do
10
10
  load_to_db(regions)
11
11
  puts "Regions import done!"
12
12
  end
13
-
13
+
14
14
  def cleanup_regins
15
15
  Province.delete_all
16
16
  City.delete_all
@@ -19,13 +19,13 @@ namespace :region do
19
19
 
20
20
  def load_to_db(regions)
21
21
  regions.each do |province_name, province_hash|
22
- current_province = Province.create(:name => province_name, :pinyin => province_hash['pinyin'], :pinyin_abbr => province_hash['pinyin_abbr'])
22
+ current_province = Province.where(name: province_name, pinyin: province_hash['pinyin'], pinyin_abbr: province_hash['pinyin_abbr']).first_or_create!
23
23
  cities_hash = province_hash['cities']
24
24
  cities_hash.each do |city_name, city_hash|
25
- current_city = current_province.cities.create(:name => city_name, :pinyin => city_hash['pinyin'], :pinyin_abbr => city_hash['pinyin_abbr'], :zip_code => city_hash['zip_code'], 'level' => city_hash['level'] || 4)
25
+ current_city = current_province.cities.where(name: city_name, pinyin: city_hash['pinyin'], pinyin_abbr: city_hash['pinyin_abbr'], zip_code: city_hash['zip_code'], level: city_hash['level'] || 4).first_or_create!
26
26
  districts_hash = city_hash['districts']
27
27
  districts_hash.each do |district_name, district_hash|
28
- current_city.districts.create(:name => district_name, :pinyin => district_hash['pinyin'], :pinyin_abbr => district_hash['pinyin_abbr'])
28
+ current_city.districts.where(name: district_name, pinyin: district_hash['pinyin'], pinyin_abbr: district_hash['pinyin_abbr']).first_or_create!
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaRegionFu do
4
+
5
+ it 'should have a version number' do
6
+ ChinaRegionFu::VERSION.should_not be_nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'china_region_fu'
metadata CHANGED
@@ -1,30 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_region_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Xuhao
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-06-25 00:00:00.000000000 Z
11
+ date: 2013-07-20 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: jquery-rails
14
+ name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: actionpack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
20
74
  - !ruby/object:Gem::Version
21
75
  version: '0'
22
76
  type: :runtime
23
77
  prerelease: false
24
78
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
79
  requirements:
27
- - - ! '>='
80
+ - - '>='
28
81
  - !ruby/object:Gem::Version
29
82
  version: '0'
30
83
  description: China region Ruby on rails interface
@@ -35,6 +88,8 @@ extensions: []
35
88
  extra_rdoc_files: []
36
89
  files:
37
90
  - .gitignore
91
+ - .rspec
92
+ - .travis.yml
38
93
  - Gemfile
39
94
  - LICENSE
40
95
  - README.md
@@ -47,37 +102,44 @@ files:
47
102
  - config/routes.rb
48
103
  - lib/china_region_fu.rb
49
104
  - lib/china_region_fu/engine.rb
50
- - lib/china_region_fu/helper.rb
105
+ - lib/china_region_fu/exceptions.rb
106
+ - lib/china_region_fu/helpers/formtastic.rb
107
+ - lib/china_region_fu/helpers/helpers.rb
108
+ - lib/china_region_fu/helpers/simple_form.rb
109
+ - lib/china_region_fu/helpers/utilities.rb
51
110
  - lib/china_region_fu/version.rb
52
111
  - lib/generators/china_region_fu/install/USAGE
53
112
  - lib/generators/china_region_fu/install/install_generator.rb
54
113
  - lib/generators/china_region_fu/install/templates/migration.rb
55
- - lib/generators/china_region_fu/install/templates/regions.yml
56
- - lib/generators/china_region_fu/mvc/USAGE
57
- - lib/generators/china_region_fu/mvc/mvc_generator.rb
114
+ - lib/generators/china_region_fu/models/USAGE
115
+ - lib/generators/china_region_fu/models/models_generator.rb
58
116
  - lib/tasks/region.rake
59
- homepage: http://rubyfans.com
60
- licenses: []
117
+ - spec/china_region_fu_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: https://github.com/Xuhao/china_region_fu
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
61
123
  post_install_message:
62
124
  rdoc_options: []
63
125
  require_paths:
64
126
  - lib
65
127
  required_ruby_version: !ruby/object:Gem::Requirement
66
- none: false
67
128
  requirements:
68
- - - ! '>='
129
+ - - '>='
69
130
  - !ruby/object:Gem::Version
70
131
  version: '0'
71
132
  required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
133
  requirements:
74
- - - ! '>='
134
+ - - '>='
75
135
  - !ruby/object:Gem::Version
76
136
  version: '0'
77
137
  requirements: []
78
138
  rubyforge_project:
79
- rubygems_version: 1.8.24
139
+ rubygems_version: 2.0.3
80
140
  signing_key:
81
- specification_version: 3
141
+ specification_version: 4
82
142
  summary: China region Ruby on rails interface
83
- test_files: []
143
+ test_files:
144
+ - spec/china_region_fu_spec.rb
145
+ - spec/spec_helper.rb
@@ -1,97 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- module ChinaRegionFu
3
- module Helper
4
-
5
- module FormHelper
6
- def region_select(object, methods, options = {}, html_options = {})
7
- output = ''
8
-
9
- html_options[:class] ? (html_options[:class].prepend('region_select ')) : (html_options[:class] = 'region_select')
10
- if Array === methods
11
- methods.each_with_index do |method, index|
12
- if klass = method.to_s.classify.safe_constantize
13
- choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
14
- next_method = methods.at(index + 1)
15
-
16
- set_options(method, options, klass)
17
- set_html_options(object, method, html_options, next_method)
18
-
19
- output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
20
- else
21
- raise "Method '#{method}' is not a vaild attribute of #{object}"
22
- end
23
- end
24
- else
25
- if klass = methods.to_s.classify.safe_constantize
26
- options[:prompt] = region_prompt(klass)
27
- output << select(object, methods, klass.all.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
28
- else
29
- raise "Method '#{method}' is not a vaild attribute of #{object}"
30
- end
31
- end
32
-
33
- output << javascript_tag(js_output)
34
- output.html_safe
35
- end
36
-
37
-
38
- private
39
-
40
- def set_options(method, options, region_klass)
41
- if respond_to?("#{method}_select_prompt")
42
- options[:prompt] = __send__("#{method}_select_prompt")
43
- else
44
- options[:prompt] = region_prompt(region_klass)
45
- end
46
- end
47
-
48
- def set_html_options(object, method, html_options, next_region)
49
- html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
50
- if next_region
51
- html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id", region_target_kalss: next_region.to_s)
52
- else
53
- html_options[:data].delete(:region_target)
54
- html_options[:data].delete(:region_target_kalss)
55
- end
56
- end
57
-
58
- def region_prompt(region_klass)
59
- human_name = region_klass.model_name.human
60
- "请选择#{human_name}"
61
- end
62
-
63
- def js_output
64
- %~
65
- $(function(){
66
- $('body').on('change', '.region_select', function(event) {
67
- var self, targetDom;
68
- self = $(event.currentTarget);
69
- targetDom = $('#' + self.data('region-target'));
70
- if (targetDom.size() > 0) {
71
- $.getJSON('/china_region_fu/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
72
- $('option[value!=""]', targetDom).remove();
73
- $.each(data, function(index, value) {
74
- targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
75
- });
76
- })
77
- }
78
- });
79
- });
80
- ~
81
- end
82
-
83
- end
84
-
85
-
86
- module FormBuilder
87
- def region_select(methods, options = {}, html_options = {})
88
- @template.region_select(@object_name, methods, options = options, html_options = html_options)
89
- end
90
- end
91
-
92
- end
93
- end
94
-
95
-
96
- ActionView::Base.send :include, ChinaRegionFu::Helper::FormHelper
97
- ActionView::Helpers::FormBuilder.send :include, ChinaRegionFu::Helper::FormBuilder