Enumerize Integrations
masterEnumerize provides automatic integration with:
- RailsAdmin: Automatically handles enumerated attributes in the admin interface.
repository·master·Indexed 23 days ago
https://github.com/brainspec/enumerizeA Ruby library for defining enumerated attributes on models with support for validation, conversion, and I18n. It integrates with ActiveRecord, Mongoid, MongoMapper, and ActiveModel::Attributes, providing helper methods for human-readable labels and raw values. Supports Ruby 3.1+ and Rails 7.0+, and includes built-in RSpec matchers for testing attribute configurations.
Enumerize provides automatic integration with:
To provide human-readable labels for enumerators, use I18n localization files. You can scope translations by the model name or use a defaults scope for shared attributes.
Use the i18n_scope option to specify where translations are stored. This can be a string, an array of strings, or a proc that returns a string.
Note: For plain Ruby objects (non-ActiveRecord/Mongoid), you must also extend ActiveModel::Naming to enable I18n support.
# Localization file structure (en.yml)
en:
enumerize:
user:
status:
student: "Student"
employed: "Employed"
retired: "Retiree"
# Usage with custom scope
class Person
extend Enumerize
extend ActiveModel::Naming
enumerize :status, in: %w[student employed retired], i18n_scope: "status"
enumerize :roles, in: %w[user admin], i18n_scope: ["user.roles", "roles"]
enumerize :color, in: %w[green blue], i18n_scope: proc { |value| "color" }
endWhen using ActiveRecord, ensure your migration creates a column (typically a string) for the attribute. You can provide a default value, which can be a static symbol or a lambda that accepts the model instance.
Important: By default, enumerize adds an inclusion validation. To skip this, use the skip_validations option (can be a boolean or a lambda).
class User < ActiveRecord::Base
extend Enumerize
# Using a lambda for dynamic default
enumerize :status, in: [:student, :employed, :retired], default: lambda { |user| StatusIdentifier.status_for_age(user.age).to_sym }
# Using a static default
enumerize :role, in: [:user, :admin], default: :user
# Skipping validations conditionally
enumerize :status, in: [:student, :employed, :retired], skip_validations: lambda { |user| user.new_record? }
# Skipping validations entirely
enumerize :role, in: [:user, :admin], skip_validations: true
endTo use Enumerize in your Ruby application, add it to your Gemfile:
gem 'enumerize'Then run bundle install. Alternatively, you can install it directly via the command line:
$ gem install enumerizeSupported Versions:
gem 'enumerize'To use the RSpec matchers within Minitest (specifically when using shoulda), add the following configuration to your test_helper.rb inside the ActiveSupport::TestCase class definition:
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
require 'enumerize/integrations/rspec'
extend Enumerize::Integrations::RSpec
# ...
endclass ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
require 'enumerize/integrations/rspec'
extend Enumerize::Integrations::RSpec
...
endEnumerize provides a built-in RSpec matcher to verify that attributes are correctly enumerated. You can use the standard should enumerize(:attribute) syntax or the RSpec 3 is_expected.to enumerize(:attribute) syntax.
To use it, ensure your class extends Enumerize and then use the matcher in your describe blocks.
class User
extend Enumerize
enumerize :status, in: [:student, :employed, :retired]
end
describe User do
it { should enumerize(:status) }
# or with RSpec 3 expect syntax
it { is_expected.to enumerize(:status) }
endWhen using Enumerize with ActiveRecord, the enumerize method is enhanced to provide seamless integration with ActiveRecord's lifecycle and type casting.
Key behaviors include:
Enumerize::Value objects.after_initialize to set default values, ensuring compatibility with how Rails allocates and initializes models.ActiveRecord::Relation (and associated proxy classes) to ensure that bulk updates via update_all correctly translate human-readable values into their underlying database values.ActiveRecord::Store (stored attributes) and ensures that reload correctly restores enumerized values from the database or the store.When used with ActiveModel::Attributes, Enumerize provides a custom type that manages the lifecycle of the attribute value:
multiple: true, it uses find_values to process arrays.multiple: true and the value is an Array, it uses find_values.find_value(value).value) to ensure the correct primitive is stored.The Enumerize::Value class is a specialized String object used to represent an enumerated item. It is designed to behave like its underlying string value for most operations but provides specific logic for equality and serialization:
== operator checks if the object matches another object by comparing the string representation or the underlying @value.as_json, the object serializes to its string representation.encode_with for custom encoding (e.g., in YAML), representing the object via its superclass and the underlying @value.Enumerize implements a custom ActiveRecord::Type::Value subclass to manage the conversion between database values and Enumerize::Value objects.
When a value is assigned to an enumerized attribute, the cast logic follows this priority:
Enumerize::Value, it is returned as-is.find_value method.This ensures that both strings (like 'active') and integers (like 1) can be correctly resolved to the appropriate Enumerize::Value object.
extend Enumerize instead of include Enumerize (the latter is deprecated). Extending a class with Enumerize automatically includes Enumerize::Base and extends the class with Enumerize::Predicates. It also provides automatic support for various ORMs and frameworks if they are defined in your environment (such as ActiveRecord, Mongoid, Sequel, or ActiveModel).Enumerize provides an extension for SimpleForm::FormBuilder that automatically configures input options for enumerated attributes. When using input or input_field in a SimpleForm builder, the extension automatically detects if an attribute is enumerated and applies the following logic:
:collection option with the enumerated attribute's options.Enumerize::Multiple, it automatically adds multiple: true to the :input_html options, unless you have explicitly set :as => :check_boxes.