diff --git a/app/assets/javascripts/subdivision_select.js b/app/assets/javascripts/subdivision_select.js index 501408f..bcfad40 100644 --- a/app/assets/javascripts/subdivision_select.js +++ b/app/assets/javascripts/subdivision_select.js @@ -3,16 +3,22 @@ var SubdivisionSelect = (function() { SubdivisionSelect.countrySelector = "select[id$=country]"; function SubdivisionSelect(element) { - this._countrySelect = element; - this._subdivisionSelect = $(element). - closest("form"). - find(SubdivisionSelect.subdivisionSelector); - }; + this._subdivisionSelect = $(element); + var countrySelector = this._subdivisionSelect.data("country-selector"); + if (countrySelector) { + this._countrySelect = $(countrySelector); + } + else { + this._countrySelect = this._subdivisionSelect + .closest("form") + .find(SubdivisionSelect.countrySelector); + } + } SubdivisionSelect.init = function () { var klass = this; - return $(klass.countrySelector).each(function() { + return $(klass.subdivisionSelector).each(function() { return new klass(this).init(); }); }; @@ -21,7 +27,7 @@ var SubdivisionSelect = (function() { var self = this; self._enabledInputsBeforeSubmit(); - $(this._countrySelect).change(function() { + this._countrySelect.change(function() { $.ajax( { url: "/subdivisions", data: { country_code: $(this).val() } diff --git a/lib/subdivision_select/subdivision_select_helper.rb b/lib/subdivision_select/subdivision_select_helper.rb index 6d6734b..690155c 100644 --- a/lib/subdivision_select/subdivision_select_helper.rb +++ b/lib/subdivision_select/subdivision_select_helper.rb @@ -43,6 +43,7 @@ def initialize(object_name, method_name, template_object, options, html_options) @html_options = html_options # Add data attribue, for selecting via JS @html_options["data-subdivision-selector"] = "1" + @html_options["data-country-selector"] = options.delete(:country_selector) if options.has_key?(:country_selector) super(object_name, method_name, template_object, options) end diff --git a/spec/controllers/subdivision_select/subdivisions_controller_spec.rb b/spec/controllers/subdivision_select/subdivisions_controller_spec.rb index f37c8e2..fb30563 100644 --- a/spec/controllers/subdivision_select/subdivisions_controller_spec.rb +++ b/spec/controllers/subdivision_select/subdivisions_controller_spec.rb @@ -6,7 +6,7 @@ module SubdivisionSelect describe "GET #get" do it "returns http success in JSON, when correct param supplied" do - get :get, country_code: "IE" + get :get, params: {country_code: "IE"} expect(response).to have_http_status(:success) expect(response.content_type).to eq("application/json") end diff --git a/spec/dummy/app/controllers/addresses_demo_controller.rb b/spec/dummy/app/controllers/addresses_demo_controller.rb index 1310c37..3429735 100644 --- a/spec/dummy/app/controllers/addresses_demo_controller.rb +++ b/spec/dummy/app/controllers/addresses_demo_controller.rb @@ -10,17 +10,23 @@ def tags @addresses = Address.all end + def custom_field + @addresses = Address.all + end + def update @address = Address.find(params[:id]) - if @address.update_attributes(address_params) + if @address.update_attributes(address_params(params[:is_custom])) redirect_to root_path, notice: "Updated address!" end end private - def address_params - params.require(:address).permit(:title, :subdivision, :country) + def address_params(is_custom) + p = params.require(:address).permit(:title, :subdivision, is_custom ? :custom_field : :country) + p[:country] = p.delete(:custom_field) if is_custom + p end end diff --git a/spec/dummy/app/views/addresses_demo/custom_field.erb b/spec/dummy/app/views/addresses_demo/custom_field.erb new file mode 100644 index 0000000..2c1dc7b --- /dev/null +++ b/spec/dummy/app/views/addresses_demo/custom_field.erb @@ -0,0 +1,40 @@ +

Demo, using custom country field name (country_selector option)

+ +<% @addresses.each do |address| %> + <%= form_tag(update_address_path(address, is_custom: true), method: :patch) do %> +
+ <%= label_tag(:title) %>
+ <%= text_field_tag("address[title]", address.title) %> +
+ +
+ <%= label_tag("address[custom_field]", "Country") %>
+ <%= + country_select( + :address, + :custom_field, + selected: address.country, + include_blank: true + ) + %> +
+ +
+ <%= label_tag("address[subdivision]", "State / Province") %>
+ <%= + subdivision_select_tag( + :address, + :subdivision, + country: address.country, + selected: address.subdivision, + include_blank: true, + country_selector: "#address_custom_field" + ) + %> +
+ +
+ <%= submit_tag("Update Address") %> +
+ <% end %> +<% end %> diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 699d2e7..2e62d90 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -3,6 +3,7 @@ root "addresses_demo#index" get "tags" => "addresses_demo#tags" + get "custom_field" => "addresses_demo#custom_field" mount SubdivisionSelect::Engine => "/subdivisions" end diff --git a/spec/dummy/spec/controllers/addresses_demo_controller_spec.rb b/spec/dummy/spec/controllers/addresses_demo_controller_spec.rb index a672b42..f655a57 100644 --- a/spec/dummy/spec/controllers/addresses_demo_controller_spec.rb +++ b/spec/dummy/spec/controllers/addresses_demo_controller_spec.rb @@ -1,4 +1,6 @@ require "rails_helper" +require 'rails-controller-testing' +Rails::Controller::Testing.install RSpec.describe AddressesDemoController, type: :controller do describe "GET #index" do @@ -18,7 +20,7 @@ describe "PATCH #update" do it "returns http success" do @address = create(:address) - patch :update, id: @address.id, address: { subdivision: "D", country: "IE" } + patch :update, params: {id: @address.id, address: { subdivision: "D", country: "IE" }} expect(assigns(:address).country).to eq("IE") expect(assigns(:address).subdivision).to eq("D") end diff --git a/spec/dummy/spec/features/select_boxes_spec.rb b/spec/dummy/spec/features/select_boxes_spec.rb index a272264..2549afb 100644 --- a/spec/dummy/spec/features/select_boxes_spec.rb +++ b/spec/dummy/spec/features/select_boxes_spec.rb @@ -5,7 +5,7 @@ end # The root URL is FormBuilder, and /tags is the FormTagHelper - URLS = "/", "/tags" + URLS = "/", "/tags", "/custom_field" URLS.each do |url| scenario "load page", js: true do @@ -13,7 +13,7 @@ expect(page).to have_content "Demo" expect(page).to have_select( - "address_country", + "Country", with_options: ["United States", "India"] ) diff --git a/subdivision_select.gemspec b/subdivision_select.gemspec index 713d6a5..30a18d5 100644 --- a/subdivision_select.gemspec +++ b/subdivision_select.gemspec @@ -22,16 +22,18 @@ Gem::Specification.new do |s| s.test_files = Dir["spec/**/*"] s.add_dependency "rails", ">= 4.1" - s.add_dependency "country_select", "~> 2.0" - s.add_dependency "countries", "~> 1.1" + s.add_dependency "country_select", "~> 3.0" + s.add_dependency "countries", "~> 2.0" s.add_dependency "jquery-rails", ">= 3.0" s.add_development_dependency "sqlite3", "~> 1.3" # Not used but we need an AR adapter - s.add_development_dependency "rspec-rails", "~> 3.2" + s.add_development_dependency "rspec-rails", "~> 3.5" s.add_development_dependency "factory_girl_rails", "~> 4.5" s.add_development_dependency "database_cleaner" s.add_development_dependency "capybara", "~> 2.4" s.add_development_dependency "poltergeist", "~> 1.8" + + s.add_development_dependency "rails-controller-testing", "~> 1.0" end