spree/spree — pull request #2818 by sohara (2013-04-04 04:19:26+00:00)
Title: Avoid nil object errors when querying a country's state in stock location form

The edit form for stock locations in the backend facet assumes that an editable stock location already has a country assigned. However [this migration](https://github.com/spree/spree/blob/master/core/db/migrate/20130213191427_create_default_stock.rb#L5) creates a default stock location that doesn't have any country or address assigned. This results in your typical `undefined method states for nil` error when attempting to edit the stock location.

There was also some repetitive use of the `f.object.country.states.empty?` expression. My solution involves assigning the value of this expression to a variable while first ensuring that the country relation exists. I don't like the idea of assigning vars in a template, but I wasn't sure how far we should go in refactor this into e.g. a helper class. I'd be happy to take that on if it's considered preferable by the maintainers.