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

Fix README description of User.status.values #387

Merged
merged 1 commit into from
Dec 3, 2021

Conversation

MichaelHoste
Copy link
Contributor

@MichaelHoste MichaelHoste commented Dec 1, 2021

User.status.values doesn't seem to return "I18n text values" as mentioned in the README, but just "values".

class User < ApplicationRecord
  extend Enumerize

  enumerize :status, in: [:student, :employed, :retired]
end

with:

en:
  enumerize:
    user:
      status:
        student: "Student"
        employed: "Employed"
        retired: "Retiree"

User.status.values will return ["student", "employed", "retired"] and not ["Student", "Employed", "Retiree"].

Or maybe am I missing something?

It doesn't return "I18n text values" but just "values"
@diesl
Copy link

diesl commented Dec 3, 2021

I can confirm this, as I stumbled across this today too

@MichaelHoste
Copy link
Contributor Author

The wrong description was apparently introduced 6 months ago during a README reorganization:

4c6a4a8#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L158-L163

@nashby nashby merged commit 6d8fa89 into brainspec:master Dec 3, 2021
@nashby
Copy link
Member

nashby commented Dec 3, 2021

Thanks! User.status.values returns Value objects and you can get translation by calling "student".text from your example but you're right, default behavior is just string value indeed. See how it works there https://github.com/brainspec/enumerize/blob/master/test/value_test.rb#L37

@MichaelHoste
Copy link
Contributor Author

Thank you! I didn't know it returned Value and not strings.

Here is a small refactoring of this section, including User.status.values.collect(&:text) that may be useful for some.

I'll let you decide if you like it better.


I18n Helper Methods

*_text / .text

Attribute's I18n text value:

@user.status_text # or @user.status.text

values

List of possible values for an enumerized attribute:

User.status.values # or User.enumerized_attributes[:status].values
# => ['student', 'employed', 'retired']

I18n text values

List of possible I18n text values for an enumerized attribute:

User.status.values.collect(&:text)
# => ['Student', 'Employed', 'Retiree']

Form example

Use it with forms (it supports :only and :except options):

<%= form_for @user do |f| %>
  <%= f.select :status, User.status.options %>
<% end %>

@nashby
Copy link
Member

nashby commented Dec 6, 2021

@MichaelHoste looks good! Please send a Pr with that 🙇

MichaelHoste added a commit to MichaelHoste/enumerize that referenced this pull request Dec 6, 2021
@MichaelHoste
Copy link
Contributor Author

Done #388

Thank you for this! I'm sure it will help some people.

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.

3 participants