Project

enum_label

0.0
No release in over 3 years
Easily add human-readable labels (Japanese, English, etc.) to your Rails enum attributes with a simple DSL.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

 Project Readme

EnumLabel

Rails の enum に人間が読めるラベルを簡単に付けられる gem です。

日本語はもちろん、どの言語のラベルでも使えます。

Installation

# Gemfile
gem "enum_label"
bundle install

Usage

class Article < ApplicationRecord
  enum :status, { draft: 0, published: 1, archived: 2 }
  enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"
end

Instance method

article = Article.new(status: :draft)
article.status_label  #=> "下書き"

article.status = :published
article.status_label  #=> "公開済み"

Class method

Article.status_labels
#=> { "draft" => "下書き", "published" => "公開済み", "archived" => "アーカイブ" }

セレクトボックスにそのまま使えます:

<%= f.select :status, Article.status_labels.invert %>

Multiple enums

class Article < ApplicationRecord
  enum :status, { draft: 0, published: 1, archived: 2 }
  enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"

  enum :visibility, { visible: 0, hidden: 1 }
  enum_label :visibility, visible: "公開", hidden: "非公開"
end

article = Article.new(status: :draft, visibility: :hidden)
article.status_label      #=> "下書き"
article.visibility_label  #=> "非公開"

Requirements

  • Ruby >= 3.2
  • Rails >= 7.0

License

The gem is available as open source under the terms of the MIT License.