china_regions 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/app/models/province.rb +9 -0
- data/lib/china_regions/action_view/extension.rb +101 -0
- data/lib/china_regions/engine.rb +9 -1
- data/lib/china_regions/version.rb +1 -1
- data/lib/china_regions.rb +1 -2
- data/lib/tasks/{china_region.rake → china_regions.rake} +0 -0
- metadata +3 -3
- data/lib/china_regions/helper.rb +0 -101
data/app/models/province.rb
CHANGED
@@ -0,0 +1,101 @@
|
|
1
|
+
module ChinaRegions
|
2
|
+
module ActionView
|
3
|
+
module Extension
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module FormHelper
|
7
|
+
def region_select(object, methods, options = {}, html_options = {})
|
8
|
+
output = ''
|
9
|
+
|
10
|
+
html_options[:class] ?
|
11
|
+
(html_options[:class].prepend('region_select ')) :
|
12
|
+
(html_options[:class] = 'region_select')
|
13
|
+
|
14
|
+
if Array === methods
|
15
|
+
methods.each_with_index do |method, index|
|
16
|
+
if klass = method.to_s.classify.safe_constantize
|
17
|
+
choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
|
18
|
+
next_method = methods.at(index + 1)
|
19
|
+
|
20
|
+
set_options(method, options, klass)
|
21
|
+
set_html_options(object, method, html_options, next_method)
|
22
|
+
|
23
|
+
output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
|
24
|
+
else
|
25
|
+
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
if klass = methods.to_s.classify.safe_constantize
|
30
|
+
options[:prompt] = region_prompt(klass)
|
31
|
+
output << select(object, methods, klass.all.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
|
32
|
+
else
|
33
|
+
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
output << javascript_tag(js_output)
|
38
|
+
output.html_safe
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def set_options(method, options, region_klass)
|
45
|
+
if respond_to?("#{method}_select_prompt")
|
46
|
+
options[:prompt] = __send__("#{method}_select_prompt")
|
47
|
+
else
|
48
|
+
options[:prompt] = region_prompt(region_klass)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def set_html_options(object, method, html_options, next_region)
|
53
|
+
html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
|
54
|
+
if next_region
|
55
|
+
html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id", region_target_kalss: next_region.to_s)
|
56
|
+
else
|
57
|
+
html_options[:data].delete(:region_target)
|
58
|
+
html_options[:data].delete(:region_target_kalss)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def region_prompt(region_klass)
|
63
|
+
human_name = region_klass.model_name.human
|
64
|
+
"请选择#{human_name}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def js_output
|
68
|
+
%~
|
69
|
+
$(function(){
|
70
|
+
$('body').on('change', '.region_select', function(event) {
|
71
|
+
var self, targetDom;
|
72
|
+
self = $(event.currentTarget);
|
73
|
+
targetDom = $('#' + self.data('region-target'));
|
74
|
+
if (targetDom.size() > 0) {
|
75
|
+
$.getJSON('/china_regions/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
|
76
|
+
$('option[value!=""]', targetDom).remove();
|
77
|
+
$.each(data, function(index, value) {
|
78
|
+
targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
|
79
|
+
});
|
80
|
+
})
|
81
|
+
}
|
82
|
+
});
|
83
|
+
});
|
84
|
+
~
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
module FormBuilder
|
91
|
+
def region_select(methods, options = {}, html_options = {})
|
92
|
+
@template.region_select(@object_name, methods, options = options, html_options = html_options)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
ActionView::Base.send :include, ChinaRegions::ActionView::Extension::FormHelper
|
101
|
+
ActionView::Helpers::FormBuilder.send :include, ChinaRegions::ActionView::Extension::FormBuilder
|
data/lib/china_regions/engine.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
require "rails"
|
2
2
|
|
3
3
|
module ChinaRegions
|
4
|
-
|
4
|
+
if ::Rails.version > "3.1"
|
5
|
+
class Engine < ::Rails::Engine; end
|
6
|
+
else
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
ActiveSupport.on_load(:action_view) do
|
9
|
+
::ActionView::Base.send :include, Penmanship::ActionView::Extension
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
5
13
|
end
|
data/lib/china_regions.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: china_regions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -46,14 +46,14 @@ files:
|
|
46
46
|
- china_regions.gemspec
|
47
47
|
- config/routes.rb
|
48
48
|
- lib/china_regions.rb
|
49
|
+
- lib/china_regions/action_view/extension.rb
|
49
50
|
- lib/china_regions/engine.rb
|
50
|
-
- lib/china_regions/helper.rb
|
51
51
|
- lib/china_regions/version.rb
|
52
52
|
- lib/generators/china_regions/install/install_generator.rb
|
53
53
|
- lib/generators/china_regions/install/templates/cities.yml
|
54
54
|
- lib/generators/china_regions/install/templates/migration.rb
|
55
55
|
- lib/generators/china_regions/regions/regions_generator.rb
|
56
|
-
- lib/tasks/
|
56
|
+
- lib/tasks/china_regions.rake
|
57
57
|
homepage: http://github.com/encoreshao
|
58
58
|
licenses: []
|
59
59
|
post_install_message:
|
data/lib/china_regions/helper.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module ChinaRegions
|
4
|
-
module Helper
|
5
|
-
|
6
|
-
module FormHelper
|
7
|
-
def region_select(object, methods, options = {}, html_options = {})
|
8
|
-
output = ''
|
9
|
-
|
10
|
-
html_options[:class] ?
|
11
|
-
(html_options[:class].prepend('region_select ')) :
|
12
|
-
(html_options[:class] = 'region_select')
|
13
|
-
|
14
|
-
if Array === methods
|
15
|
-
methods.each_with_index do |method, index|
|
16
|
-
if klass = method.to_s.classify.safe_constantize
|
17
|
-
choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
|
18
|
-
next_method = methods.at(index + 1)
|
19
|
-
|
20
|
-
set_options(method, options, klass)
|
21
|
-
set_html_options(object, method, html_options, next_method)
|
22
|
-
|
23
|
-
output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
|
24
|
-
else
|
25
|
-
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
else
|
29
|
-
if klass = methods.to_s.classify.safe_constantize
|
30
|
-
options[:prompt] = region_prompt(klass)
|
31
|
-
output << select(object, methods, klass.all.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
|
32
|
-
else
|
33
|
-
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
output << javascript_tag(js_output)
|
38
|
-
output.html_safe
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def set_options(method, options, region_klass)
|
45
|
-
if respond_to?("#{method}_select_prompt")
|
46
|
-
options[:prompt] = __send__("#{method}_select_prompt")
|
47
|
-
else
|
48
|
-
options[:prompt] = region_prompt(region_klass)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def set_html_options(object, method, html_options, next_region)
|
53
|
-
html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
|
54
|
-
if next_region
|
55
|
-
html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id", region_target_kalss: next_region.to_s)
|
56
|
-
else
|
57
|
-
html_options[:data].delete(:region_target)
|
58
|
-
html_options[:data].delete(:region_target_kalss)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def region_prompt(region_klass)
|
63
|
-
human_name = region_klass.model_name.human
|
64
|
-
"请选择#{human_name}"
|
65
|
-
end
|
66
|
-
|
67
|
-
def js_output
|
68
|
-
%~
|
69
|
-
$(function(){
|
70
|
-
$('body').on('change', '.region_select', function(event) {
|
71
|
-
var self, targetDom;
|
72
|
-
self = $(event.currentTarget);
|
73
|
-
targetDom = $('#' + self.data('region-target'));
|
74
|
-
if (targetDom.size() > 0) {
|
75
|
-
$.getJSON('/china_regions/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
|
76
|
-
$('option[value!=""]', targetDom).remove();
|
77
|
-
$.each(data, function(index, value) {
|
78
|
-
targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
|
79
|
-
});
|
80
|
-
})
|
81
|
-
}
|
82
|
-
});
|
83
|
-
});
|
84
|
-
~
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
module FormBuilder
|
91
|
-
def region_select(methods, options = {}, html_options = {})
|
92
|
-
@template.region_select(@object_name, methods, options = options, html_options = html_options)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
|
100
|
-
ActionView::Base.send :include, ChinaRegions::Helper::FormHelper
|
101
|
-
ActionView::Helpers::FormBuilder.send :include, ChinaRegions::Helper::FormBuilder
|