arvindvyas / Country-State-Select

It will fetch the countries and according to that fetch the state of that countries, currently it is for countries listing
MIT License
69 stars 78 forks source link

Compatability without simple_form #33

Closed variousred closed 8 years ago

variousred commented 8 years ago

I want to use this gem without having to use simple_form. I already have an established app with many forms that i need to add this country/state functionality to.

arvindvyas commented 8 years ago

You can use older version of it , I am trying to manage it separately.

aldefouw commented 8 years ago

@variousred

When I wrote this gem for Version 3.0, I completely changed the public interface that @arvindvyas had in Version 2.0. I wrote it with simple_form in mind because the application that I was working on used simple_form.

I improved it in many ways, but I also added dependencies, which is bad - I apologize. (This is the first gem I have written.)

I have simple_form listed as a dependency in the gemspec, but I should really change that.

The good news is, according to my testing, simple_form is unnecessary.

This gem should work with plain old "form_for" form objects as well.

I just ran a test on code like this.

<%= form_for @form_object do |f| f.select :work_country, CountryStateSelect.countries_collection f.select :work_state, CountryStateSelect.state_options(label: "State / Province", form: f, field_names: {:country => :work_country, :state => :work_state}) end %>

It worked for me.

I need to do some more testing and make sure this is the case and there aren't any hidden problems I neglected to observe - but if I were you, I would give it a try and see if it works for you.

If I can prove that this works without simple_form, I will issue a new version that removes the simple_form dependency and update the documentation accordingly.

Give it a try without simple_form, but stay tuned ...

variousred commented 8 years ago

Thanks for that update, @aldefouw , sounds good

aldefouw commented 8 years ago

UPDATE

I am working on a version that should support form_for, but as I work on it, I am realizing that there are some serious bugs related to cities implementation. I'll haven't reviewed that code that was added because I have been swamped with other projects (not public projects - projects at work) - so I'll have to talk with Arvind about that.

What that means for you is that whatever I deliver to you will have broken cities implementation. Is that a problem?

In any case, the form_for implementation will end up being like this: '<%= form_for @location do |f| %>

<p>Country</p>
<%= f.select :test_country, CountryStateSelect.countries_collection, selected: f.object.test_country %>

<p>State / Province</p>
<%= f.select :test_state, CountryStateSelect.collect_states(f.object.test_country), selected: f.object.test_state %>

<p>City</p>
<%= f.select :test_city, CountryStateSelect.collect_cities(f.object.test_country, f.object.test_state), selected: f.object.test_city %>

<br /><br />

<%= f.button "Submit", class: "btn btn-success" %>

<%= link_to "Cancel", locations_path, class: "btn btn-danger" %>

<% end %>'

Part of the goal of requiring simple form is that things are passed with hashes, so you don't need to manually specify that you have a particular item specified in a list. It just works.

Using simple_form, the gem protects users from having the headache of remembering to select their value. Since a lot of people use partials for their forms, this seemed like a good route.

There's also some difficulties related to if you want to return a string versus a list in form_for. (This is a concern when someone selects a Country that has no states ... we still need to give them an option to fill in a state as a text field.) Simple form facilitated some pretty complex things without writing a ton of code.

I can understand wanting form_for to work though - I get it that legacy code needs to be supported.

I do need to make sure that I am not breaking simple_form functionality by making this more compatible with form_for though.

It will be more code for the end user to implement no matter what I do because form_for is kind of a lame version of simple_form in most ways. (I believe it underpins simple_form.)

With form_for, you'll have to manually specify what is selected. There isn't much I can do to get around that, unless you'd prefer just passing a form object into a method that you place inside the form_for block.

The problem I have with that is that it becomes so abstract to someone reading the form what is actually happening then.

aldefouw commented 8 years ago

I will be posting updates to this branch:

https://github.com/aldefouw/Country-State-Select/tree/regular_form_attempt

I am not done with it, but it's a start.

arvindvyas commented 8 years ago

@aldefouw which code is breaking for cities ?

aldefouw commented 8 years ago

@arvindvyas

I'll need to look at it again tonight since I am currently at my "day job," but as I recall from my testing, cities are not re-populated correctly for edit forms once you have selected them initially.

I will verify tonight unless you want to do your own testing.

https://github.com/aldefouw/Country-State-Select/tree/regular_form_attempt

You can probably see the behavior manifested in the /edit/ page I added to the demo of the branch I was working on for @variousred.

arvindvyas commented 8 years ago

@variousred if you use form like <%= form_for @location do |f| %> <%= f.select :test_country, CountryStateSelect.countries_collection %> <%= f.select :test_state, [] %> <%= f.select :test_city, [] %> <%end%>

this will work

In the next version of country_state_select simple form will be removed as an dependency

aldefouw commented 8 years ago

@arvindvyas - I agree that the code you are showing technically works for a #new action.

I would caution you though; remember that some people will need a way to reload the data they've already submitted for the #edit action.

They could use a different form for the edit action and load things appropriately, it's true - but many people use a form partial for their #new and #edit forms, and it's painful to have to create separate forms.

The ideal thing would be to create an abstracted method that could be called on a common form partial that would take care of all of the messy logic that determines the appropriate data to be loaded.

I am not sure of the cleanest way to do it, but that's my two cents anyway.

variousred commented 8 years ago

Thanks for your work on this, guys

arvindvyas commented 8 years ago

@aldefouw you are right, this is a temporary solution for those who want to use form for not simple form , I have also removed simple_form from the dependencies so that people who really want to use simple form can add gem inside their application . I will look into the form_for for edit now and once it will complete will do new release.

aldefouw commented 8 years ago

Sounds good, @arvindvyas. I understand where you are coming from.