china_regions 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,6 +38,27 @@ bundle install
38
38
  create app/models/province.rb
39
39
  create app/models/city.rb
40
40
  create app/models/district.rb
41
+
42
+ ## How to view
43
+
44
+ 范例:
45
+
46
+ = form_for @article do |f|
47
+
48
+ = f.region_select [:province, :city, :district]
49
+
50
+ # form_tag
51
+ = region_select :article, :province_id
52
+ = region_select :article, :city_id
53
+ = region_select :article, :district_id
54
+
55
+ OR
56
+
57
+ = region_select :article, :province
58
+ = region_select :article, :city
59
+ = region_select :article, :district
60
+
61
+ = f.submit class: 'btn'
41
62
 
42
63
 
43
64
  ## Contributing
@@ -1,4 +1,6 @@
1
1
  en:
2
+ views:
3
+ select: Choose A %{model}
2
4
  activerecord:
3
5
  models:
4
6
  province: Province
@@ -11,7 +13,7 @@ en:
11
13
  name_en: English Name
12
14
  name_abbr: Short Name
13
15
  city:
14
- name: Name
16
+ name: Nam
15
17
  province_id: Province
16
18
  province: Province
17
19
  name_en: English Name
@@ -1,4 +1,6 @@
1
1
  zh:
2
+ views:
3
+ select: 请选择%{model}
2
4
  activerecord:
3
5
  models:
4
6
  province: 省份
@@ -6,7 +6,7 @@ module ChinaRegions
6
6
  else
7
7
  class Railtie < ::Rails::Railtie
8
8
  ActiveSupport.on_load(:action_view) do
9
- ::ActionView::Base.send :include, Penmanship::ActionView::Extension
9
+ ::ActionView::Base.send :include, ChinaRegions::Helpers::FormHelper
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+
3
+ module ChinaRegions
4
+ module Helpers
5
+ module FormHelper
6
+
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 region_klass = method.to_s.classify.safe_constantize
17
+ choices = (index == 0 ? region_klass.scoped.collect {|p| [ p.name, p.id ] } : [])
18
+ next_method = methods.at(index + 1)
19
+
20
+ set_options(method, options, region_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
+
30
+ _methods = unless methods.to_s.include?('_id')
31
+ (methods.to_s + ('_id')).to_sym
32
+ else
33
+ _methods = methods
34
+ methods = methods.to_s.gsub(/(_id)$/, '')
35
+ _methods
36
+ end
37
+
38
+ if region_klass = methods.to_s.classify.safe_constantize
39
+ options[:prompt] = region_prompt(region_klass)
40
+
41
+ output << select(object, _methods, region_klass.scoped.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
42
+ else
43
+ raise "Method '#{method}' is not a vaild attribute of #{object}"
44
+ end
45
+ end
46
+
47
+ output << javascript_tag(js_output)
48
+ output.html_safe
49
+ end
50
+
51
+
52
+ private
53
+
54
+ def set_options(method, options, region_klass)
55
+ if respond_to?("#{method}_select_prompt")
56
+ options[:prompt] = __send__("#{method}_select_prompt")
57
+ else
58
+ options[:prompt] = region_prompt(region_klass)
59
+ end
60
+ end
61
+
62
+ def set_html_options(object, method, html_options, next_region)
63
+ html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
64
+ if next_region
65
+ html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id", region_target_kalss: next_region.to_s)
66
+ else
67
+ html_options[:data].delete(:region_target)
68
+ html_options[:data].delete(:region_target_kalss)
69
+ end
70
+ end
71
+
72
+ def region_prompt(region_klass)
73
+ t('views.select', model: region_klass.model_name.human)
74
+ end
75
+
76
+ def js_output
77
+ %~
78
+ $(function(){
79
+ $('body').on('change', '.region_select', function(event) {
80
+ var self, targetDom;
81
+ self = $(event.currentTarget);
82
+ targetDom = $('#' + self.data('region-target'));
83
+ if (targetDom.size() > 0) {
84
+ $.getJSON('/china_regions/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
85
+ $('option[value!=""]', targetDom).remove();
86
+ $.each(data, function(index, value) {
87
+ targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
88
+ });
89
+ })
90
+ }
91
+ });
92
+ });
93
+ ~
94
+ end
95
+
96
+ end
97
+
98
+
99
+ module FormBuilder
100
+ def region_select(methods, options = {}, html_options = {})
101
+ @template.region_select(@object_name, methods, options = options, html_options = html_options)
102
+ end
103
+ end
104
+
105
+ end
106
+ end
107
+
108
+ ActionView::Base.send :include, ChinaRegions::Helpers::FormHelper
109
+ ActionView::Helpers::FormBuilder.send :include, ChinaRegions::Helpers::FormBuilder
@@ -1,3 +1,3 @@
1
1
  module ChinaRegions
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/china_regions.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "china_regions/version"
2
2
  require 'china_regions/engine' if defined? Rails
3
- require "china_regions/action_view/extension"
3
+ require "china_regions/helpers/form_helper"
4
4
 
5
5
  module ChinaRegions;end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-12 00:00:00.000000000 Z
12
+ date: 2013-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
@@ -48,8 +48,8 @@ files:
48
48
  - config/locales/zh.yml
49
49
  - config/routes.rb
50
50
  - lib/china_regions.rb
51
- - lib/china_regions/action_view/extension.rb
52
51
  - lib/china_regions/engine.rb
52
+ - lib/china_regions/helpers/form_helper.rb
53
53
  - lib/china_regions/version.rb
54
54
  - lib/generators/china_regions/install_generator.rb
55
55
  - lib/generators/china_regions/regions_generator.rb
@@ -1,103 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module ChinaRegions
4
- module ActionView
5
- module Extension
6
- extend ActiveSupport::Concern
7
-
8
- module FormHelper
9
- def region_select(object, methods, options = {}, html_options = {})
10
- output = ''
11
-
12
- html_options[:class] ?
13
- (html_options[:class].prepend('region_select ')) :
14
- (html_options[:class] = 'region_select')
15
-
16
- if Array === methods
17
- methods.each_with_index do |method, index|
18
- if klass = method.to_s.classify.safe_constantize
19
- choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
20
- next_method = methods.at(index + 1)
21
-
22
- set_options(method, options, klass)
23
- set_html_options(object, method, html_options, next_method)
24
-
25
- output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
26
- else
27
- raise "Method '#{method}' is not a vaild attribute of #{object}"
28
- end
29
- end
30
- else
31
- if klass = methods.to_s.classify.safe_constantize
32
- options[:prompt] = region_prompt(klass)
33
- output << select(object, methods, klass.all.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
34
- else
35
- raise "Method '#{method}' is not a vaild attribute of #{object}"
36
- end
37
- end
38
-
39
- output << javascript_tag(js_output)
40
- output.html_safe
41
- end
42
-
43
-
44
- private
45
-
46
- def set_options(method, options, region_klass)
47
- if respond_to?("#{method}_select_prompt")
48
- options[:prompt] = __send__("#{method}_select_prompt")
49
- else
50
- options[:prompt] = region_prompt(region_klass)
51
- end
52
- end
53
-
54
- def set_html_options(object, method, html_options, next_region)
55
- html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
56
- if next_region
57
- html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id", region_target_kalss: next_region.to_s)
58
- else
59
- html_options[:data].delete(:region_target)
60
- html_options[:data].delete(:region_target_kalss)
61
- end
62
- end
63
-
64
- def region_prompt(region_klass)
65
- human_name = region_klass.model_name.human
66
- "请选择#{human_name}"
67
- end
68
-
69
- def js_output
70
- %~
71
- $(function(){
72
- $('body').on('change', '.region_select', function(event) {
73
- var self, targetDom;
74
- self = $(event.currentTarget);
75
- targetDom = $('#' + self.data('region-target'));
76
- if (targetDom.size() > 0) {
77
- $.getJSON('/china_regions/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
78
- $('option[value!=""]', targetDom).remove();
79
- $.each(data, function(index, value) {
80
- targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
81
- });
82
- })
83
- }
84
- });
85
- });
86
- ~
87
- end
88
-
89
- end
90
-
91
-
92
- module FormBuilder
93
- def region_select(methods, options = {}, html_options = {})
94
- @template.region_select(@object_name, methods, options = options, html_options = html_options)
95
- end
96
- end
97
-
98
- end
99
- end
100
- end
101
-
102
- ActionView::Base.send :include, ChinaRegions::ActionView::Extension::FormHelper
103
- ActionView::Helpers::FormBuilder.send :include, ChinaRegions::ActionView::Extension::FormBuilder