district_cn_selector 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/.gitignore +27 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +37 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +138 -0
  7. data/Rakefile +21 -0
  8. data/config/routes.rb +7 -0
  9. data/district_cn_selector.gemspec +27 -0
  10. data/lib/district_cn_selector.rb +22 -0
  11. data/lib/district_cn_selector/district_controller.rb +26 -0
  12. data/lib/district_cn_selector/district_select_helper.rb +46 -0
  13. data/lib/district_cn_selector/district_select_ul_helper.rb +48 -0
  14. data/lib/district_cn_selector/engine.rb +22 -0
  15. data/lib/district_cn_selector/helpers/form_builder.rb +13 -0
  16. data/lib/district_cn_selector/helpers/form_builder_extension.rb +49 -0
  17. data/lib/district_cn_selector/hooks/simple_form.rb +72 -0
  18. data/lib/district_cn_selector/selector.rb +202 -0
  19. data/lib/district_cn_selector/theme.rb +19 -0
  20. data/lib/district_cn_selector/version.rb +3 -0
  21. data/spec/controllers/district_cn_selector/district_controller_spec.rb +13 -0
  22. data/spec/dummy/README.rdoc +261 -0
  23. data/spec/dummy/Rakefile +7 -0
  24. data/spec/dummy/app/assets/javascripts/application.js +5 -0
  25. data/spec/dummy/app/assets/stylesheets/application.css.scss +31 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  27. data/spec/dummy/app/controllers/home_controller.rb +7 -0
  28. data/spec/dummy/app/controllers/tests_controller.rb +10 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/mailers/.gitkeep +0 -0
  31. data/spec/dummy/app/models/company.rb +8 -0
  32. data/spec/dummy/app/views/home/index.html.erb +31 -0
  33. data/spec/dummy/app/views/home/select2_backup.html.erb +49 -0
  34. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  35. data/spec/dummy/app/views/tests/1.html.erb +8 -0
  36. data/spec/dummy/app/views/tests/2.html.erb +9 -0
  37. data/spec/dummy/app/views/tests/3.html.erb +7 -0
  38. data/spec/dummy/app/views/tests/4.html.erb +7 -0
  39. data/spec/dummy/app/views/tests/5.html.erb +7 -0
  40. data/spec/dummy/app/views/tests/6.html.erb +7 -0
  41. data/spec/dummy/config.ru +4 -0
  42. data/spec/dummy/config/application.rb +75 -0
  43. data/spec/dummy/config/boot.rb +10 -0
  44. data/spec/dummy/config/database.yml +16 -0
  45. data/spec/dummy/config/environment.rb +5 -0
  46. data/spec/dummy/config/environments/development.rb +37 -0
  47. data/spec/dummy/config/environments/production.rb +67 -0
  48. data/spec/dummy/config/environments/test.rb +37 -0
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/spec/dummy/config/initializers/inflections.rb +15 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  52. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  53. data/spec/dummy/config/initializers/session_store.rb +8 -0
  54. data/spec/dummy/config/initializers/simple_form.rb +142 -0
  55. data/spec/dummy/config/initializers/simple_form_bootstrap.rb +45 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/dummy/config/locales/active_record.zh-CN.yml +11 -0
  58. data/spec/dummy/config/locales/en.yml +5 -0
  59. data/spec/dummy/config/locales/simple_form.en.yml +26 -0
  60. data/spec/dummy/config/routes.rb +5 -0
  61. data/spec/dummy/db/migrate/20130717055253_create_companies.rb +10 -0
  62. data/spec/dummy/db/schema.rb +24 -0
  63. data/spec/dummy/lib/assets/.gitkeep +0 -0
  64. data/spec/dummy/log/.gitkeep +0 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +25 -0
  68. data/spec/dummy/public/area_select_cn/area-bg1.jpg +0 -0
  69. data/spec/dummy/public/area_select_cn/area-bg2.jpg +0 -0
  70. data/spec/dummy/public/favicon.ico +0 -0
  71. data/spec/dummy/script/rails +6 -0
  72. data/spec/factories/.gitkeep +0 -0
  73. data/spec/features/visit_testpage_spec.rb +93 -0
  74. data/spec/helpers/form_spec.rb +106 -0
  75. data/spec/helpers/simple_form_spec.rb +101 -0
  76. data/spec/spec_helper.rb +48 -0
  77. data/spec/support/mock_controller.rb +22 -0
  78. data/spec/support/view_test_helper.rb +29 -0
  79. data/vendor/assets/images/district_cn_selector/area-bg1.jpg +0 -0
  80. data/vendor/assets/images/district_cn_selector/area-bg2.jpg +0 -0
  81. data/vendor/assets/images/district_cn_selector/bootstrap-theme.png +0 -0
  82. data/vendor/assets/images/district_cn_selector/default-theme.png +0 -0
  83. data/vendor/assets/javascripts/district_cn_selector/jquery.district-ul.js +224 -0
  84. data/vendor/assets/javascripts/district_cn_selector/jquery.district.js +119 -0
  85. data/vendor/assets/stylesheets/district_cn_selector/district-ul.css +93 -0
  86. metadata +269 -0
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,5 @@
1
+ //= require jquery
2
+ //= require jquery_ujs
3
+ //= require district_cn_selector/jquery.district-ul
4
+ //= require district_cn_selector/jquery.district
5
+ //= require_self
@@ -0,0 +1,31 @@
1
+ /*
2
+ *= require_self
3
+ *= require district_cn_selector/district-ul
4
+ */
5
+
6
+ @import "bootstrap";
7
+ @import "font-awesome";
8
+
9
+ $baseLineHeight: 20px;
10
+
11
+ body {
12
+ padding-top: 45px;
13
+ }
14
+
15
+ strong {
16
+ font-weight: bold;
17
+ line-height: $baseLineHeight - 1;
18
+ }
19
+
20
+ .field_with_errors{
21
+ .controls{ border: 1px solid red; }
22
+ color: red;
23
+ }
24
+ .province_select,
25
+ .city_select,
26
+ .district_select{
27
+ select{width:130px;margin-right:10px}
28
+ }
29
+
30
+
31
+
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ class HomeController < ApplicationController
3
+ def index
4
+ @company = Company.new
5
+ @company.region_code = 331002
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ class TestsController < ApplicationController
3
+ def show
4
+ @company = Company.new
5
+ @company.region_code = 341002
6
+ @company.loc_code = 341002
7
+
8
+ render params[:id]
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
@@ -0,0 +1,8 @@
1
+ class Company < ActiveRecord::Base
2
+ attr_accessor :region_code
3
+ attr_accessible :region_code, :loc_code
4
+
5
+ act_as_area_field :region_code
6
+ validates :region_code, presence: true
7
+ validates :loc_code, presence: true
8
+ end
@@ -0,0 +1,31 @@
1
+ <div class="span8 alert">
2
+ <%= form_for @company, :url => '#', :builder => DistrictCnSelector::Helpers::FormBuilder, :html => {:class => 'form-horizontal'} do |f| %>
3
+ <legend>form_for</legend>
4
+ <%= f.district_select_ul :region_code %>
5
+ <%= f.district_select :loc_code %>
6
+ <div class="form-actions">
7
+ <%= f.submit "提交", :class => "btn btn-success" %>
8
+ </div>
9
+ <% end %>
10
+
11
+ <%= simple_form_for @company, :url => '#', :html => {:class => 'form-horizontal'} do |f| %>
12
+ <legend>simple_form_for</legend>
13
+ <%= f.input :region_code, :as => :district_select_ul, :prompt_class => "btn btn-success" %>
14
+ <%= f.input :loc_code, :as => :district_select %>
15
+ <div class="form-actions">
16
+ <%= f.submit "提交", :class => "btn btn-success" %>
17
+ </div>
18
+ <% end %>
19
+
20
+ <%= form_tag "#", :class => 'form-horizontal' do %>
21
+ <legend>form_tag</legend>
22
+ <%= district_select_ul(:company, :region_code, "331000", :theme => :bootstrap, :prompt_class => "btn btn-danger") %>
23
+ <%= district_select(:company, :loc_code, "331000") %>
24
+
25
+ <div class="form-actions">
26
+ <%= submit_tag "提交", :class => "btn btn-success" %>
27
+ </div>
28
+ <% end %>
29
+
30
+ </div>
31
+
@@ -0,0 +1,49 @@
1
+ <div class="span4">
2
+ <input type="hidden" id="e1" tabindex="-1" value="331002"/>
3
+
4
+ <%= content_for :js do %>
5
+ <script>
6
+ $(document).ready(function () {
7
+ function format(item) { return item.text; }
8
+
9
+ $("#e1").select2({
10
+ minimumInputLength: 1,
11
+ placeholder: "请选择地区",
12
+ width: "100%",
13
+ matcher: function (term, text, opt) {
14
+ return text.toUpperCase().indexOf(term.toUpperCase()) >= 0
15
+ || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0;
16
+ },
17
+ ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
18
+ url: "/area_select_cn/district/search",
19
+ dataType: 'json',
20
+ data: function (term, page) {
21
+ return {
22
+ region_name: term // search term
23
+ //page_limit: 10,
24
+ //apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
25
+ };
26
+ },
27
+ results: function (data, page) { // parse the results into the format expected by Select2.
28
+ // since we are using custom formatting functions we do not need to alter remote JSON data
29
+ return {results: data};
30
+ }
31
+ },
32
+ initSelection: function (element, callback) {
33
+ var region_code = $(element).attr('value');
34
+ if (region_code !== "") {
35
+ $.ajax("/area_select_cn/district?region_code=" + region_code, {
36
+ data: {},
37
+ dataType: "json"
38
+ }).done(function (data) {
39
+ callback(data);
40
+ });
41
+ }
42
+ },
43
+ //formatResult: format, // omitted for brevity, see the source of this page
44
+ formatSelection: format // omitted for brevity, see the source of this page
45
+ });
46
+ });
47
+ </script>
48
+ <% end %>
49
+ </div>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= yield :title %> District Cn Selector</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= csrf_meta_tags %>
7
+ </head>
8
+ <body>
9
+ <div id="content" class="container">
10
+ <%= yield %>
11
+ </div>
12
+ <%= javascript_include_tag "application" %>
13
+ <%= yield :js %>
14
+ </body>
15
+ </html>
@@ -0,0 +1,8 @@
1
+ <%= form_for @company,
2
+ :url => '#', :builder => DistrictCnSelector::Helpers::FormBuilder, :html => {:class => 'form-horizontal'} do |f| %>
3
+ <legend>form_for</legend>
4
+ <%= f.district_select_ul :region_code %>
5
+ <div class="form-actions">
6
+ <%= f.submit "提交", :class => "btn btn-success" %>
7
+ </div>
8
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <%= form_for @company,
2
+ :url => '#', :builder => DistrictCnSelector::Helpers::FormBuilder,
3
+ :html => {:class => 'form-horizontal'} do |f| %>
4
+ <legend>form_for</legend>
5
+ <%= f.district_select_ul :loc_code %>
6
+ <div class="form-actions">
7
+ <%= f.submit "提交", :class => "btn btn-success" %>
8
+ </div>
9
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <%= simple_form_for @company, :url => '#', :html => {:class => 'form-horizontal'} do |f| %>
2
+ <legend>simple_form_for</legend>
3
+ <%= f.input :region_code, :as => :district_select_ul, :prompt_class => "btn btn-success" %>
4
+ <div class="form-actions">
5
+ <%= f.submit "提交", :class => "btn btn-success" %>
6
+ </div>
7
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <%= simple_form_for @company, :url => '#', :html => {:class => 'form-horizontal'} do |f| %>
2
+ <legend>simple_form_for</legend>
3
+ <%= f.input :loc_code, :as => :district_select_ul, :prompt_class => "btn btn-success" %>
4
+ <div class="form-actions">
5
+ <%= f.submit "提交", :class => "btn btn-success" %>
6
+ </div>
7
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <%= form_tag '#', :class => 'form-horizontal' do %>
2
+ <legend>form_tag</legend>
3
+ <%= district_select_ul(:company, :region_code, "341002", :theme => :bootstrap, :prompt_class => "btn btn-danger") %>
4
+ <div class="form-actions">
5
+ <%= submit_tag "提交", :class => "btn btn-success" %>
6
+ </div>
7
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <%= form_tag '#', :class => 'form-horizontal' do %>
2
+ <legend>form_tag</legend>
3
+ <%= district_select_ul(:company, :loc_code, "341002", :theme => :bootstrap, :prompt_class => "btn btn-danger") %>
4
+ <div class="form-actions">
5
+ <%= submit_tag "提交", :class => "btn btn-success" %>
6
+ </div>
7
+ <% end %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,75 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "active_resource/railtie"
8
+ require "sprockets/railtie"
9
+ # require "rails/test_unit/railtie"
10
+ require "pp"
11
+
12
+ Bundler.require(*Rails.groups)
13
+ require "district_cn_selector"
14
+
15
+ if defined?(Bundler)
16
+ # If you precompile assets before deploying to production, use this line
17
+ Bundler.require *Rails.groups(:assets => %w(development test))
18
+ # If you want your assets lazily compiled in production, use this line
19
+ # Bundler.require(:default, :assets, Rails.env)
20
+ end
21
+
22
+
23
+ module Dummy
24
+ class Application < Rails::Application
25
+ # Settings in config/environments/* take precedence over those specified here.
26
+ # Application configuration should go into files in config/initializers
27
+ # -- all .rb files in that directory are automatically loaded.
28
+
29
+ # Custom directories with classes and modules you want to be autoloadable.
30
+ # config.autoload_paths += %W(#{config.root}/extras)
31
+
32
+ # Only load the plugins named here, in the order given (default is alphabetical).
33
+ # :all can be used as a placeholder for all plugins not explicitly named.
34
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
35
+
36
+ # Activate observers that should always be running.
37
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
38
+
39
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
40
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
41
+ # config.time_zone = 'Central Time (US & Canada)'
42
+
43
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
44
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
45
+ # config.i18n.default_locale = :de
46
+ config.i18n.default_locale = :'zh-CN'
47
+
48
+ # Configure the default encoding used in templates for Ruby 1.9.
49
+ config.encoding = "utf-8"
50
+
51
+ # Configure sensitive parameters which will be filtered from the log file.
52
+ config.filter_parameters += [:password]
53
+
54
+ # Enable escaping HTML in JSON.
55
+ config.active_support.escape_html_entities_in_json = true
56
+
57
+ # Use SQL instead of Active Record's schema dumper when creating the database.
58
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
59
+ # like if you have constraints or database-specific column types
60
+ # config.active_record.schema_format = :sql
61
+
62
+ # Enforce whitelist mode for mass assignment.
63
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
64
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
65
+ # parameters by using an attr_accessible or attr_protected declaration.
66
+ config.active_record.whitelist_attributes = true
67
+
68
+ # Enable the asset pipeline
69
+ config.assets.enabled = true
70
+
71
+ # Version of your assets, change this if you want to expire all your assets
72
+ config.assets.version = '1.0'
73
+ end
74
+ end
75
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,16 @@
1
+ defaults: &defaults
2
+ adapter: sqlite3
3
+ pool: 5
4
+ timeout: 5000
5
+
6
+ development:
7
+ <<: *defaults
8
+ database: db/development.sqlite3
9
+
10
+ test:
11
+ <<: *defaults
12
+ database: db/test.sqlite3
13
+
14
+ production:
15
+ <<: *defaults
16
+ database: db/production.sqlite3
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,37 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger
20
+ config.active_support.deprecation = :log
21
+
22
+ # Only use best-standards-support built into browsers
23
+ config.action_dispatch.best_standards_support = :builtin
24
+
25
+ # Raise exception on mass assignment protection for Active Record models
26
+ config.active_record.mass_assignment_sanitizer = :strict
27
+
28
+ # Log the query plan for queries taking more than this (works
29
+ # with SQLite, MySQL, and PostgreSQL)
30
+ config.active_record.auto_explain_threshold_in_seconds = 0.5
31
+
32
+ # Do not compress assets
33
+ config.assets.compress = false
34
+
35
+ # Expands the lines which load the assets
36
+ config.assets.debug = true
37
+ end
@@ -0,0 +1,67 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_assets = true
13
+
14
+ # Compress JavaScripts and CSS
15
+ config.assets.compress = true
16
+
17
+ # Don't fallback to assets pipeline if a precompiled asset is missed
18
+ config.assets.compile = false
19
+
20
+ # Generate digests for assets URLs
21
+ config.assets.digest = true
22
+
23
+ # Defaults to nil and saved in location specified by config.assets.prefix
24
+ # config.assets.manifest = YOUR_PATH
25
+
26
+ # Specifies the header that your server uses for sending files
27
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29
+
30
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31
+ # config.force_ssl = true
32
+
33
+ # See everything in the log (default is :info)
34
+ # config.log_level = :debug
35
+
36
+ # Prepend all log lines with the following tags
37
+ # config.log_tags = [ :subdomain, :uuid ]
38
+
39
+ # Use a different logger for distributed setups
40
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
41
+
42
+ # Use a different cache store in production
43
+ # config.cache_store = :mem_cache_store
44
+
45
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
46
+ # config.action_controller.asset_host = "http://assets.example.com"
47
+
48
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
49
+ # config.assets.precompile += %w( search.js )
50
+
51
+ # Disable delivery errors, bad email addresses will be ignored
52
+ # config.action_mailer.raise_delivery_errors = false
53
+
54
+ # Enable threaded mode
55
+ # config.threadsafe!
56
+
57
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
58
+ # the I18n.default_locale when a translation can not be found)
59
+ config.i18n.fallbacks = true
60
+
61
+ # Send deprecation notices to registered listeners
62
+ config.active_support.deprecation = :notify
63
+
64
+ # Log the query plan for queries taking more than this (works
65
+ # with SQLite, MySQL, and PostgreSQL)
66
+ # config.active_record.auto_explain_threshold_in_seconds = 0.5
67
+ end