Inter
Easily add arbitrary attributes to an ActiveRecord model on the fly, without the need of any extra new database columns or migrations.
Why?
Ever added a 5th boolean to a model just so you could keep track if something had happened to this user, article, etc? With Inter you can add any arbitrary attributes to any ActiveRecord model on the fly without the need to add yet another database migration. Instead all interactions are stored in a seperate table and hooked up using polymorphism. Using a simple DSL you can then quickly manipulate and query interactions.
Benefits:
- Cleaner models
- Simple and powerful DSL
- More lightweight than a full state machine
- Can store any value
Usage
Dependencies
Installation
Add the gem to your Gemfile.
gem "inter"
Then generate and run the database migrations.
rails generate inter:install
rake db:migrate
And finally define an ActiveRecord model as an interactable.
class Article < ActiveRecord::Base
include Inter::Actable
end
Basic usage
Instance methods
Setting a value on an Article is now easy. Just pick a key name and set it to a desired value.
article.is_published!
article.isnt_published!
article.is_published?
article.is_published_and_not_promoted!
article.is_published_and_promoted?
article.is_published_and_not_promoted?
article.set :tags, [:ruby, :rails, :gem]
article.get :tags
article.has? :tags
article.set :tags, nil
article.has? :tags
Class methods
You can query interactable objects in a similar fasion.
Article.is_published
Article.isnt_published
Article.is_published_and_promoted
Interaction history
One of the advantages of Interactions is that they are kept as a permanent log. Thefefore you can easily look up if an interaction has ever happened in the past.
article.was_published?
article.is_published!
article.isnt_published!
article.is_published?
article.was_published?
article.wasnt_published?
article.was_published_and_promoted?
article.history :published
article.history "published"
article.history :published, :promoted
article.history ["published", "promoted"]
Advanced usage
A lot of the magic methods have underlying dumb methods that can be used too to further customize your needs.
Instance methods
article.is "published"
article.is :published, :promoted
article.is [:published, :not_promoted]
article.is? :published, :promoted
article.is? [:published, :not_promoted]
article.get :promoted
Class methods
Article.inter(published: true)
Article.inter(published: true).first
Article.interactions(published: true)
Related lookup methods
Finally if 2 related models are both interactable then you can query related models by interaction status.
class Article < ActiveRecord::Base
include Inter::Actable
has_many :comments
end
class Comment < ActiveRecord::Base
include Inter::Actable
belongs_to :article
end
article.comments.first.is_spam!
article.spam_comments
article.not_spam_comments
article.spam_comments_ids
article.spam_comments_count
article.comments.inter(spam: true)
Changelog
- 0.0.2 Copied over implementation
- 0.0.1 Gem skeleton
Contributors
Todos
- Add a generator
- Add tests
- Add option to disable history
License
See LICENSE