All Projects → jasl → activeentity

jasl / activeentity

Licence: other
Active Record without Database

Programming Languages

ruby
36898 projects - #4 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to activeentity

active model serializers validator
🃏 An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
Stars: ✭ 18 (-60.87%)
Mutual labels:  activerecord, activemodel
u-observers
Simple and powerful implementation of the observer pattern.
Stars: ✭ 31 (-32.61%)
Mutual labels:  activerecord, activemodel
duck record
Used for creating virtual models like ActiveType or ModelAttribute does.
Stars: ✭ 23 (-50%)
Mutual labels:  activerecord, activemodel
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+6195.65%)
Mutual labels:  activerecord, activemodel
nxt state machine
A simple but powerful state machine implementation.
Stars: ✭ 14 (-69.57%)
Mutual labels:  activerecord
alloy
Patchwork now rename to [alloy], alloy is a simple toolkit that makes your iOS / OS X apps development more easier.
Stars: ✭ 61 (+32.61%)
Mutual labels:  activerecord
database cleaner-active record
Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.
Stars: ✭ 35 (-23.91%)
Mutual labels:  activerecord
ebisu connection
EbisuConnection allows access to replica servers
Stars: ✭ 14 (-69.57%)
Mutual labels:  activerecord
squint
Search PostgreSQL jsonb and hstore columns
Stars: ✭ 26 (-43.48%)
Mutual labels:  activerecord
act-form
The simple way to create form objects or command/service objects with ActiveModel
Stars: ✭ 29 (-36.96%)
Mutual labels:  activemodel
paper trail-association tracking
Plugin for the PaperTrail gem to track and reify associations
Stars: ✭ 103 (+123.91%)
Mutual labels:  activerecord
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+78.26%)
Mutual labels:  activerecord
index shotgun
duplicate index checker 🔥 🔫 👮
Stars: ✭ 35 (-23.91%)
Mutual labels:  activerecord
activerecord-mysql2rgeo-adapter
ActiveRecord connection adapter for gis, based on mysql and rgeo
Stars: ✭ 25 (-45.65%)
Mutual labels:  activerecord
activerecord-cockroachdb-adapter
CockroachDB adapter for ActiveRecord.
Stars: ✭ 90 (+95.65%)
Mutual labels:  activerecord
sinatras-skeleton
Basic Sinatra Skeleton MVC CRUD App with Sprockets, Warden, ActiveRecord and PostgresQL
Stars: ✭ 13 (-71.74%)
Mutual labels:  activerecord
sinatra-api-server-toolbox
Sinatra API Server Toolbox (Ruby, Sinatra, ActiveRecord, postgreSQL, JSON, jQuery, AJAX)
Stars: ✭ 21 (-54.35%)
Mutual labels:  activerecord
Neo
Orm框架:基于ActiveRecord思想开发的至简化的java的Orm框架
Stars: ✭ 35 (-23.91%)
Mutual labels:  activerecord
prefixed ids
Friendly Prefixed IDs for your Ruby on Rails models
Stars: ✭ 159 (+245.65%)
Mutual labels:  activerecord
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-54.35%)
Mutual labels:  activerecord

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
  embeds_many :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
  embeds_many :categories, index_errors: true
  validates :categories, uniqueness_in_embeds: {key: :name}

  embeds_many :reviewers
  validates :reviewers, 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

I18n

Same to Active Record I18n, the only different is the root of locale YAML is active_entity instead of activerecord

Enum

You can use the enum class method to define a set of possible values for an attribute. It is similar to the enum functionality in Active Model, but has significant enough quirks that you should think of them as distinct.

class Example < ActiveEntity::Base
  attribute :steve, :integer
  enum steve: [:martin, :carell, :buscemi]
end

example = Example.new
example.attributes # => {"steve"=>nil}
example.steve = :carell
example.carell? # => true
example.attributes # => {"steve"=>"carell"}
example.steve = 2
example.attributes # => {"steve"=>"buscemi"}

# IMPORTANT: the next line will only work if you implement an update! method
example.martin! # => {"steve"=>"martin"}

example.steve = :bannon # ArgumentError ('bannon' is not a valid steve)

The first thing you'll notice about the :steve attribute is that it is an "Integer", even though it might seem logical to define it as a String... TL;DR: don't do this. Internally enum tracks the possible values based on their index position in the array.

It's also possible to provide a Hash of possible values:

class Example < ActiveEntity::Base
  attribute :steve, :integer, default: 9
  enum steve: {martin: 5, carell: 12, buscemi: 9}
end

example = Example.new
example.attributes # => {"steve"=>"buscemi"}

The other quirk of this implementation is that you must create your attribute before you call enum. enum does not create the search scopes that might be familar to Active Model users, since there is no search or where concept in Active Entity. You can, however, access the mapping directly to obtain the index number for a given value:

Example.steves[:buscemi] # => 9

You can define prefixes and suffixes for your enum attributes. Note the underscores:

class Conversation < ActiveEntity::Base
  attribute :status, :integer
  attribute :comments_status, :integer
  enum status: [ :active, :archived ], _suffix: true
  enum comments_status: [ :active, :inactive ], _prefix: :comments
end

conversation = Conversation.new
conversation.active_status! # only if you have an update! method
conversation.archived_status? # => false

conversation.comments_inactive! # only if you have an update! method
conversation.comments_active? # => false

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].