Active Entity
Active Entity is a Rails virtual model solution based on ActiveModel and it's design for Rails 6+.
Active Entity is forked from Active Record by removing all database relates codes, so it nearly no need to learn how to use.
About Virtual Model
Virtual Model is the model not backed by a database table, usually used as "form model" or "presenter", because it's implement interfaces of Active Model, so you can use it like a normal Active Record model in your Rails app.
Features
Attribute declaration
class Book < ActiveEntity::Base
attribute :title, :string
attribute :tags, :string, array: true, default: []
end
Same usage with Active Record, Learn more.
One enhancement is array: true
that transform the attribute to an array that can accept multiple values.
Nested attributes
Active Entity supports its own variant of nested attributes via the embeds_one
/ embeds_many
macros. The intention is to be mostly compatible with ActiveRecord's accepts_nested_attributes_for
functionality.
class Holiday < ActiveEntity::Base
attribute :date, :date
validates :date, presence: true
end
class HolidaysForm < ActiveEntity::Base
:holidays
accepts_nested_attributes_for :holidays, reject_if: :all_blank
end
Validations
class Book < ActiveEntity::Base
attribute :title, :string
validates :title, presence: true
end
Supported Active Record validations:
Common validation options supported too.
subset
validation
Because Active Entity supports array attribute, for some reason, you may want to test values of an array attribute are all included in a given set.
Active Entity provides subset
validation to achieve that, it usage similar to inclusion
or exclusion
class Steak < ActiveEntity::Base
attribute :side_dishes, :string, array: true, default: []
validates :side_dishes, subset: { in: %w(chips mashed_potato salad) }
end
uniqueness_in_embeds
validation
Active Entity provides uniqueness_in_embeds
validation to test duplicate nesting virtual record.
Argument key
is attribute name of nested model, it also supports multiple attributes by given an array.
class Category < ActiveEntity::Base
attribute :name, :string
end
class Reviewer < ActiveEntity::Base
attribute :first_name, :string
attribute :last_name, :string
end
class Book < ActiveEntity::Base
:categories
validates :categories, uniqueness_in_embeds: {key: :name}
:reviewers
validates :categories, uniqueness_in_embeds: {key: [:first_name, :last_name]}
end
uniqueness_in_active_record
validation
Active Entity provides uniqueness_in_active_record
validation to test given scope
doesn't present in ActiveRecord model.
The usage same as uniqueness in addition you must give a AR model class_name
class Candidate < ActiveEntity::Base
attribute :name, :string
validates :name,
uniqueness_on_active_record: {
class_name: "Staff"
}
end
Others
These Active Record feature also available in Active Entity
composed_of
enum
(You must declare the attribute before usingenum
)serializable_hash
serialize
store
I18n
Same to Active Record I18n, the only different is the root of locale YAML is active_entity
instead of activerecord
Read-only attributes
You can use attr_readonly :title, :author
to prevent assign value to attribute after initialized.
You can use enable_readonly!
and disable_readonly!
to control the behavior.
Important: It's no effect with embeds or array attributes !!!
Extending
Most of Active Model plugins are compatible with Active Entity.
You need to include them manually.
Tested extensions:
Installation
Add this line to your application's Gemfile:
gem 'activeentity', require: "active_entity/railtie"
And then execute:
$ bundle
Or install it yourself as:
$ gem install activeentity
Other awesome gems
Contributing
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
License
The gem is available as open source under the terms of the MIT License.