china_region_fu 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,7 +7,7 @@ ChinaRegionFu provide the region data of china.You will got complete chian regio
7
7
 
8
8
  Put 'gem china_region_fu' to your Gemfile:
9
9
 
10
- gem china_region_fu
10
+ gem 'china_region_fu'
11
11
 
12
12
  Run bundler command to install the gem:
13
13
 
@@ -77,6 +77,16 @@ If you want to customize the region modules you can run the generator:
77
77
 
78
78
  </div>
79
79
  <% end %>
80
+
81
+ ##### prompt
82
+
83
+ You need define `province_select_prompt`, `city_select_prompt`, `district_select_prompt` helpers for each select prompt.
84
+ If you have not define these helpers, it will use the default one like:
85
+
86
+ def region_prompt(region_klass)
87
+ human_name = region_klass.model_name.human
88
+ "请选择#{human_name}"
89
+ end
80
90
 
81
91
  Online example: [医院之家](http://www.yihub.com/ "医院").
82
92
 
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "china_region_fu"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = ChinaRegionFu::VERSION
17
+
18
+ gem.add_dependency 'jquery-rails'
17
19
  end
@@ -4,22 +4,17 @@ module ChinaRegionFu
4
4
 
5
5
  module FormHelper
6
6
  def region_select(object, methods, options = {}, html_options = {})
7
- html_options[:class] ? (html_options[:class].prepend('region_select ')) : (html_options[:class] = 'region_select')
8
-
9
7
  output = ''
8
+
9
+ html_options[:class] ? (html_options[:class].prepend('region_select ')) : (html_options[:class] = 'region_select')
10
10
  if Array === methods
11
11
  methods.each_with_index do |method, index|
12
12
  if klass = method.to_s.classify.safe_constantize
13
13
  choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
14
14
  next_method = methods.at(index + 1)
15
- options[:prompt] = region_prompt(klass)
16
- html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
17
- if next_method
18
- html_options[:data].merge!(region_target: "#{object}_#{next_method.to_s}_id", region_target_kalss: next_method.to_s)
19
- else
20
- html_options[:data].delete(:region_target)
21
- html_options[:data].delete(:region_target_kalss)
22
- end
15
+
16
+ set_options(method, options, klass)
17
+ set_html_options(object, method, html_options, next_method)
23
18
 
24
19
  output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
25
20
  else
@@ -34,13 +29,44 @@ module ChinaRegionFu
34
29
  raise "Method '#{method}' is not a vaild attribute of #{object}"
35
30
  end
36
31
  end
37
- output << javascript_tag(%~
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
+ %~
38
65
  $(function(){
39
66
  $('body').on('change', '.region_select', function(event) {
40
- var self, target, targetDom;
67
+ var self, targetDom;
41
68
  self = $(event.currentTarget);
42
- target = self.data('region-target');
43
- targetDom = $('#' + target);
69
+ targetDom = $('#' + self.data('region-target'));
44
70
  if (targetDom.size() > 0) {
45
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) {
46
72
  $('option[value!=""]', targetDom).remove();
@@ -50,17 +76,8 @@ module ChinaRegionFu
50
76
  })
51
77
  }
52
78
  });
53
- })
54
- ~)
55
- output.html_safe
56
- end
57
-
58
-
59
- private
60
-
61
- def region_prompt(klass)
62
- human_name = klass.model_name.human
63
- "请选择#{human_name}"
79
+ });
80
+ ~
64
81
  end
65
82
 
66
83
  end
@@ -71,6 +88,7 @@ module ChinaRegionFu
71
88
  @template.region_select(@object_name, methods, options = options, html_options = html_options)
72
89
  end
73
90
  end
91
+
74
92
  end
75
93
  end
76
94
 
@@ -1,3 +1,3 @@
1
1
  module ChinaRegionFu
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_region_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-13 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jquery-rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: China region Ruby on rails interface
15
31
  email:
16
32
  - xuhao@rubyfans.com