spree/spree — pull request #2072 by msevestre (2012-10-11 03:25:42+00:00)
Title: PR for Issue #1977: States always required

@radar: Here is a first try to solve the issue in 1977.
I had to change many files in order to get that to work. Since I am still not really familiar with the spree code base, I would really appreciate your input on that PR:

Here is what I have done in a nutshell:

1. Add boolean `states_required` to country, default `true` so that it is compatible with the previous behavior. It would be great to find some information on which countries actually do require states and which not and add that to the migration

2. Extended the admin section to manage countries. No `Delete` or `Add`, just edit so that the user can set the `states_required` to true or false.

3. Update the `_address` partial and `checkout.js` to render the new logic.
* if states are defined for the country then it is the same as before
* if not, then if states_required, the field is enabled and mandatory.
* otherwise the field is disabled

I followed the existing logic to retrieve the value of the boolean `states_required` for each country (similar to what's going on with the state_mapper which is used to get the list of states for all countries). It seems to me to be a bit of a hack however. I wanted first to add an attribute `data-state-required` to the option field but did not know how to do that using the `collection_select` helper.

4. I also refactored the _address partial a bit. The code for shipping and billing was exactly the same and clearly not DRY. I moved the code in another partial

5. Last but not least I have added a few tests but not enough to my liking. Are they in the source code some tests that I could look at which would help me to understand how to write UI tests (goal is to test the address partial and checkout,js)?

Thanks for your feedback. I'll add some tests tomorrow after getting your inputs

Cheers