Skip to content

Fix README description of User.status.values - #387

Merged
nashby merged 1 commit into
brainspec:masterfrom
MichaelHoste:master
Dec 3, 2021
Merged

nashby merged 1 commit into
brainspec:masterfrom
MichaelHoste:master

Conversation

@MichaelHoste

@MichaelHoste MichaelHoste commented Dec 1, 2021 •

Copy link
Copy Markdown
Contributor

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

diesl commented Dec 3, 2021

Copy link
Copy Markdown

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

@MichaelHoste

Copy link
Copy Markdown
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

nashby commented Dec 3, 2021 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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

nashby commented Dec 6, 2021

Copy link
Copy Markdown
Member

@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
Copy Markdown
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