Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions app/assets/javascripts/subdivision_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};
Expand All @@ -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() }
Expand Down
1 change: 1 addition & 0 deletions lib/subdivision_select/subdivision_select_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions spec/dummy/app/controllers/addresses_demo_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions spec/dummy/app/views/addresses_demo/custom_field.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<h1>Demo, using custom country field name (country_selector option)</h1>

<% @addresses.each do |address| %>
<%= form_tag(update_address_path(address, is_custom: true), method: :patch) do %>
<div class="field">
<%= label_tag(:title) %><br />
<%= text_field_tag("address[title]", address.title) %>
</div>

<div class="field">
<%= label_tag("address[custom_field]", "Country") %><br />
<%=
country_select(
:address,
:custom_field,
selected: address.country,
include_blank: true
)
%>
</div>

<div class="field">
<%= label_tag("address[subdivision]", "State / Province") %><br />
<%=
subdivision_select_tag(
:address,
:subdivision,
country: address.country,
selected: address.subdivision,
include_blank: true,
country_selector: "#address_custom_field"
)
%>
</div>

<div class="field">
<%= submit_tag("Update Address") %>
</div>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/features/select_boxes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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
visit url
expect(page).to have_content "Demo"

expect(page).to have_select(
"address_country",
"Country",
with_options: ["United States", "India"]
)

Expand Down
8 changes: 5 additions & 3 deletions subdivision_select.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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