Project

exnum

0.0
Low commit activity in last 3 years
No release in over a year
Exnum is enum extention for Rails. This gem extends enum about i18n and associated parameters.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Exnum

Gem Version CircleCI

Exnum is enum extention for Rails. This gem extends enum about i18n, associated parameters, and array for select box.

Installation

Add this line to your application's Gemfile:

gem 'exnum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exnum

Usage

class User < ActiveRecord::Base
  # the value of the key "val" defines enum values
  # and the others defines associated parameters
  exnum role: {
    guest:   {val: 10, label: :red,   selectable: true, },
    general: {val: 20, label: :green, selectable: true,  permission: false},
    admin:   {val: 30, label: :blue,  selectable: false, permission: true},
  }
end

Assosiated parameter extention

User.role_labels
# => {"guest" => :red, "general" => :green, "admin" => :blue}
User.role_labels{|p| p[:selectable]}
# => {"guest" => :red, "general" => :green}
User.role_permissions
# => {"guest" => nil, "general" => false, "admin" => true}
user = User.new(role: :general)
user.role_label
# => :green
user.role_permission
# => false

I18n extention

ja:
  activerecord:
    enum:
      user:        # model name
        role:      # field name
          guest:   ゲスト
          general: 一般ユーザー
          admin:   管理者
User.roles_i18n
# => {"guest" => "ゲスト", "general" => "一般ユーザー", "admin" => "管理者"}
User.roles_i18n{|p| p[:selectable]}
# => {"guest" => "ゲスト", "general" => "一般ユーザー"}
user = User.new(role: :guest)
user.role_i18n
# => "ゲスト"

Array for select box extention

User.roles_for_select
# => [["ゲスト", "guest"], ["一般ユーザー", "general"], ["管理者", "admin"]]
User.roles_for_select{|p| p[:selectable]}
# => [["ゲスト", "guest"], ["一般ユーザー", "general"]]

You can use it like this:

<%= f.select :role, User.roles_for_select %>

enum

Of course, you can use methods provided by enum.

class User < ActiveRecord::Base
  exnum role: [:guest, :general, :admin]
end
class User < ActiveRecord::Base
  exnum role: {guest: 10, general: 20, admin: 30}
end
class User < ActiveRecord::Base
  exnum role: {guest: 10, general: 20, admin: 30}, _prefix: true
end

Contributing

  1. Fork it!
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

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