[RUBY] When you want to change the wording to be displayed when making a select box from enum

I don't think this is the case, but when creating a select box from an enum, make a note of how to change the wording. As a premise, ʻenumerizeandreform` are used for the gem.

product_form.rb


extend Enumerize

enumerize :fluit, in: {
  apple:      1,
  orange:     2,
  peach:      3,
  strawberry: 4,
  cherry:     5,
}, predicates: { prefix: true }

property :fluit

ja.yml


product:
  fluit:
    apple:Apple
    orange:Orange
    peach:Peaches
    strawberry:Strawberry
    cherry:Cherry

If you write it normally, this is fine.

new.slim


= f.select :fluit, f.object.class.fluit.options, {prompt: 'Select fruit'}

However, suppose that he was told, "I want to put the words" (on special sale) "for" apples "and" cherries ". That said, I don't want to change ja.yml. Writing one by one without using f.object.class.fluit.options is also poorly maintainable.

Therefore, I created a method that turns the Array generated by f.object.class.fluit.options with a map and adds the wording and returns only when it is a specific value.

product_form.rb


def fruit_select_display
  self.class.fluit.options.map do |key, value|
    if value == 'apple' || value == 'cherry'
      [key + '(During special sale)', value]
    else
      [key, value]
    end
  end
end

new.slim


= f.select :fluit, f.object.fruit_select_display, {prompt: 'Select fruit'}

With this, I was able to use it once.

Recommended Posts

When you want to change the wording to be displayed when making a select box from enum
When you want to change the MySQL password of docker-compose
If you want to change the Java development environment from Eclipse
When you want to check whether the contents of a property can be converted to a specific type
A memo when you want to clear the time part of the calendar
When you want to use the method outside
How to create a route directly from the URL you want to specify + α
[rails] After option useful when you want to change the order of DB columns
I want to set the conditions to be displayed in collection_check_boxes
How to create a form to select a date from the calendar
I want to create a form to select the [Rails] category
I want to give a class name to the select attribute
[Enum_help] Use enum_help to create a select box displayed in Japanese!
[Spring Dtata JPA] How to deal with the problem that DB change cannot be detected when you want to process API synchronously with a single thread in Spring Boot.
[Swift] When you want to know if the number of characters in a String matches a certain number ...
A collection of patterns that you want to be aware of so as not to complicate the code
Object-oriented design that can be used when you want to return a response in form format
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
[Output] Learn Ruby from what you want to do at the fastest speed-Part 2 "Create a flowchart"
When you want to ZIP download the image data saved locally
An example of a small work when you want to divide the definition value according to the environment but do not want to be aware of it
When you want to add a string type column with a limited length with the `rails generate migration` command
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
What to do if Operation not permitted is displayed when you execute a command in the terminal
You cannot change the project facet version from a dynamic web module to x.x. How to deal with
When you want Rails to disable a session for a specific controller only
Don't forget to release it when you get the object from S3!
I want you to use Enum # name () for the Key of SharedPreference
What to do when you want to delete a migration file that is "NO FILE"
What to do if you can't activate the select box created by bootstrap-select
If you are using Android Room and want to change the column definition
Learn while making a WEB server Introduction to WEB application development from the basics
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
[Rails] How to write when making a subquery
A note when you want Tuple in Java
[Ruby] When you want to replace multiple characters
Rspec: I want to test the post-execution state when I set a method on subject
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...