Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the default country by ISO code #810

Merged
merged 10 commits into from Feb 17, 2016
Merged

Find the default country by ISO code #810

merged 10 commits into from Feb 17, 2016

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Feb 7, 2016

...so that we don't have to know database IDs on boot-up time.

# @return [Integer,nil] id of {Country} to be selected by default in dropdowns (default: nil)
preference :default_country_id, :integer

# @!attribute [rw] default_country_iso
# @deprecated
# @return [Integer,nil] id of {Country} to be selected by default in dropdowns (default: nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says default nil, but the code 'us'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right. The comment also says deprecated, which I don't intend
it to be. I'll force push once the build is through.
Am 07.02.2016 19:28 schrieb "Thomas von Deyen" [email protected]:

In core/app/models/spree/app_configuration.rb
#810 (comment):

 #   @return [Integer,nil] id of {Country} to be selected by default in dropdowns (default: nil)
 preference :default_country_id, :integer
  • @!attribute [rw] default_country_iso

  • @deprecated

  • @return [Integer,nil] id of {Country} to be selected by default in dropdowns (default: nil)

The comment says default nil, but the code 'us'


Reply to this email directly or view it on GitHub
https://github.com/solidusio/solidus/pull/810/files#r52121680.

@tvdeyen
Copy link
Member

tvdeyen commented Feb 7, 2016

👍

@mamhoff
Copy link
Contributor Author

mamhoff commented Feb 8, 2016

This ties in with the taxation stuff, as using the default country I can bypass the whole idea of a default tax zone. Upcoming: A default state that behaves just like the default country.

@jhawthorn
Copy link
Contributor

This breaks the existing default_country_id preference, it would be great if that could remain functional.

@jhawthorn
Copy link
Contributor

Thanks for re-adding that functionality Martin. This is 👍 from me now.

end || first
if Spree::Config.default_country_id
ActiveSupport::Deprecation.warn("Setting your default country via its ID is deprecated. Please set your default country via the `default_country_iso` setting.", caller)
find_by(id: Spree::Config.default_country_id) || find_by!(iso: Spree::Config.default_country_iso)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you want to load the country by iso if the find by id returns nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the scenario of find by ID returning nil is essentially a misconfigured shop. Previously, in that case it would return first, i.e. any country. I find it more fitting to actually return something sensible, and that would be the configured country by iso.

It's supposed to be nudging people into configuring their shops correctly. Apart from the address form and (newly) taxes, the default country is also used for stock locations, and should make sense there, too.

@tvdeyen
Copy link
Member

tvdeyen commented Feb 11, 2016

I think default_country_code would be a more understandable name. Although iso is the correct term, code is more commonly known.

Don't know what do you think?

@mamhoff
Copy link
Contributor Author

mamhoff commented Feb 11, 2016

Hm, isn't the country code for Germany +49? Joking aside, this fits in well with the country factory (iso) , the Adress class (country_iso, country_iso=) and lots of other places in Solidus. I'm for keeping that name.

Right now, the default country for address drop downs is
defined using that addresses `id`, which is error-prone
and makes Solidus' startup depend on the database.

This commit adds a preference for the country's ISO code,
which is more understandable and does not depend on the database.
Also, raise an ActiveRecord::RecordNotFound if the default
country is not present. In this case, a number of core functionalities
(especially Customer returns) will not work as expected.
The only configuration value actually emitted via the API
is the default country id. This commit adds another value to be
emitted, the default country ISO, and feeds that from the
actual default country found at runtime.
Before, this would rely on the default country id. Now it
uses the default country ISO.
By default, preferences are not stored anyway, and with the default
country ISO code present, we do not need to first find the US and then
store the ID.
Instead of the first country found, please raise an error if
the default country ISO code does not have an associated country.

The issue referenced in the spec, spree/spree#1142,
is people complaining about no states found for Nil, which only ever
happened because Spree::Country.default would silently fail before.
Also, make sure the countries created actually have correct
data by providing the two-letter ISO code to the factories.
Previously, this PR broke existing shops with a default country ID with no
Warning. With this commit, we get a warning, and still load the default country
by ID. The fallback will do exactly what the default code is doing.
@mamhoff
Copy link
Contributor Author

mamhoff commented Feb 12, 2016

Rebased on current master to remove conflicts.

@cbrunsdon
Copy link
Contributor

I think this is awesome. I've always been embarrassed by the default_country_id and think this is an awesome change excellently executed.
👍

@jhawthorn
Copy link
Contributor

I also think iso is the best choice

jhawthorn added a commit that referenced this pull request Feb 17, 2016
Find the default country by ISO code
@jhawthorn jhawthorn merged commit 306b869 into solidusio:master Feb 17, 2016
@jhawthorn
Copy link
Contributor

Thanks again!

@mamhoff mamhoff deleted the default-country-by-iso branch March 1, 2016 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants