Skip to content

Commit

Permalink
Add support for country_select v9. Fixes formtastic#1381
Browse files Browse the repository at this point in the history
The `country_select` v2 syntax was introduced in
[August 2014](https://github.com/countries/country_select/blob/19072737cc2b2a12723b2dd273263b71fd5395ed/UPGRADING.md).

Now, 10 years and 7 major versions later, the project
[dropped support for v1 syntax](countries/country_select#224)
starting with v9.

This change adapts `formtastic` to the new method signature,
removes all claims that it supports v1 syntax,
and updates the URLs to the new home under a new maintainer.
  • Loading branch information
leoarnold committed Feb 22, 2024
1 parent 473f5ce commit 17143b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
formtastic (4.0.0)
formtastic (5.0.0)
actionpack (>= 6.0.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ You can use your new input with `:as => :date_picker`.

There are none other than Rails itself, but...

* If you want to use the `:country` input, you'll need to install the [country-select plugin](https://github.com/stefanpenner/country_select) (or any other country_select plugin with the same API). Both 1.x and 2.x are supported, but they behave differently when storing data, so please see their [upgrade notes](https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md) if switching from 1.x.
* If you want to use the `:country` input, you'll need to install the [country-select plugin](https://github.com/countries/country_select) (or any other country_select plugin with the same API).
* There are a bunch of development dependencies if you plan to contribute to Formtastic


Expand Down
15 changes: 9 additions & 6 deletions lib/formtastic/inputs/country_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ module Inputs
# Rails doesn't come with a `country_select` helper by default any more, so you'll need to do
# one of the following:
#
# * install the [country_select](https://github.com/stefanpenner/country_select) gem
# * install the [country_select](https://github.com/countries/country_select) gem
# * install any other country_select plugin that behaves in a similar way
# * roll your own `country_select` helper with the same args and options as the Rails one
#
# Formtastic supports both 1.x and 2.x of stefanpenner/country_select, but if you're upgrading
# from 1.x, they behave quite differently, so please see their [upgrade instructions](https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md).
#
# By default, Formtastic includes a handful of English-speaking countries as "priority
# countries", which can be set in the `priority_countries` configuration array in the
# formtastic.rb initializer to suit your market and user base (see README for more info on
Expand Down Expand Up @@ -71,12 +68,18 @@ class CountryInput
CountrySelectPluginMissing = Class.new(StandardError)

def to_html
raise CountrySelectPluginMissing, "To use the :country input, please install a country_select plugin, like this one: https://github.com/stefanpenner/country_select" unless builder.respond_to?(:country_select)
raise CountrySelectPluginMissing, "To use the :country input, please install a country_select plugin, like this one: https://github.com/countries/country_select" unless builder.respond_to?(:country_select)
input_wrapping do
label_html <<
builder.country_select(method, priority_countries, input_options, input_html_options)
builder.country_select(method, input_options_including_priorities, input_html_options)
end
end

def input_options_including_priorities
return input_options unless priority_countries

input_options.merge(:priority_countries => priority_countries)
end

def priority_countries
options[:priority_countries] || builder.priority_countries
Expand Down
10 changes: 5 additions & 5 deletions spec/inputs/country_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

describe "when country_select is not available as a helper from a plugin" do

it "should raise an error, sugesting the author installs a plugin" do
it "should raise an error, suggesting the author installs a plugin" do
expect {
semantic_form_for(@new_post) do |builder|
concat(builder.input(:country, :as => :country))
Expand Down Expand Up @@ -57,7 +57,7 @@
priority_countries = ["Foo", "Bah"]
semantic_form_for(@new_post) do |builder|
allow(builder).to receive(:country_select).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, { :priority_countries => priority_countries }, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)

concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
end
Expand All @@ -70,7 +70,7 @@

semantic_form_for(@new_post) do |builder|
allow(builder).to receive(:country_select).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, { :priority_countries => priority_countries }, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)

concat(builder.input(:country, :as => :country))
end
Expand All @@ -86,7 +86,7 @@

concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
allow(builder).to receive(:country_select).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)
expect(builder).to receive(:country_select).with(:country, { :priority_countries => [] }, {:id => "context2_post_country", :required => false, :autofocus => false, :readonly => false}).and_return("<select><option>...</option></select>".html_safe)
concat(builder.input(:country, :priority_countries => []))
end)
end
Expand All @@ -112,7 +112,7 @@
end
end

describe "whent the attribute is 'country_something'" do
describe "when the attribute is 'country_something'" do

before do
concat(semantic_form_for(@new_post) do |builder|
Expand Down

0 comments on commit 17143b9

Please sign in to comment.