0.0
No commit activity in last 3 years
No release in over 3 years
Scopeless enums, and some syntactic sugar for string enums
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 6

Runtime

 Project Readme

Active Record Enum Extensions

Installation

Add it to your Gemfile:

gem 'enum_extensions'

Then:

bundle

Simple enum

ActiveRecord's enum creates scopes, and bang and query methods. simple_enum doesn't. It just adds the enum values to the attribute. So, following Rails docs' enum example, this is what we get:

class Conversation < ActiveRecord::Base
  simple_enum status: [ :active, :archived ]
end

# conversation.update! status: 0
conversation.status = 0
conversation.status     # => "active"

# conversation.update! status: 1
conversation.status = 1
conversation.status     # => "archived"

# conversation.update! status: 1
conversation.status = "archived"

# conversation.update! status: nil
conversation.status = nil
conversation.status.nil?  # => true
conversation.status       # => nil

# mapping values
Conversation.statuses # => { "active" => 0, "archived" => 1 }

This is useful when the scopes and added methods are not only ambiguous, or clashing with other enum attributes with the same values, but useless.

String enums

ActiveRecord's enum allows us to create enums attributes with string values, like this:

enum status: { :active => 'active', :archived => 'archived' }

Don't you hate those duplicated symbols/strings? I do. So this gem allows you to do:

enum_s status: [ :active, :archived ]

resulting in the enum attribute having string values. Also, can be applied to the simple_enum:

simple_enum_s status: [ :active, :archived ]

Compatibility/Requirements

This gem has been tested and is known to work with ActiveRecord 6, using Ruby 2.6.

Credits

  • simple_enum is simply a copy of Rails's enum code, with the scope and query attributes code removed.