zmbacker/enum_help — pull request #10 by thiagoa (2014-09-25 23:39:36+00:00)
Title: Fix simple_form regression

Hi,

I just fixed a regression on the simple_form integration module, I’m sorry! I guess we need some tests, but that’s OK for now :)

It blew up with a collection like this, which previously worked:

```ruby
f.input collection: [:editing, :approved] # Ops...
```

Note that it also accepts an input format like this (shown in the README file):

```ruby
f.input collection: Foo.statuses.except(:draft)
```

I also need to changed the format of the hash returned by “Foo.collection_i18n” to something more useful. It was in the following format: { ‘Translated key’ => integer code }. I realized the integer code had little to no use, because integer codes are meant to be used internally by the model. They shouldn’t be needed on the views. I guess the “Foo.statuses” (or similar) method that comes with Rails is meant for querying the database, and I see little use outside that context. For example:

```ruby
Foo.where status: Foo.statuses[‘editing'] # Fetches all foos with “editing” status
```

The new i18n collection hash can be used like this, which is much more useful:

```ruby
translated_statuses = Foo.statuses_i18n
translated_statuses['editing’] # Returns the translation of the “editing” status
```

That method is also useful because:

- There is a centralised place to fetch all the translations for an enum attribute, if the user needs to (the simple form extension also fetches the translations from that method)
- The user can easily query a translation for any enum label, like shown above
- The user doesn’t need to mess with a raw i18n query (enums.model.field.label)

I’ll be glad to hear an opinion from you, thanks.