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 @@ +