ActionModel
Clean up complex model logic using action.
Installation
Add this line to your application's Gemfile:
gem 'actionmodel'
And then execute:
$ bundle
Or install it yourself as:
$ gem install actionmodel
Usage
If you have complex model class it can be split into clear model class and actions. For example, Post
model
class Post
scope :search, ->(title) { where tile: title }
scope :page, ->(page, size) { ... }
def add_tags(tags)
...
end
def remove_tags(tags)
...
end
end
can be split into pretty Post
model and actions: searchable
, pageable
, tagable
class Post
include ActionModel::Concern
acts_as_searchable :title, ignorecase: true
acts_as :pageable, :tagable
end
Searchable action
module Actions
module Searchable
extend ActiveSupport::Concern
included do
scope :search, ->(text) do
actions[:searchable].fields.each do |field, options|
# field is title
# options is { ignorecase: true }
...
end
end
end
end
end
Pageable action
module Actions
module Pageable
extend ActiveSupport::Concern
included do
scope :page, ->(page, size) { ... }
end
end
end
Tagable action
module Actions
module Tagable
def add_tags(tags)
...
end
def remove_tags(tags)
...
end
end
end
Action is a ruby module and can be included into model class with DSL acts_as
very easy
acts_as :action1, :action2, ...
or with fields and options
acts_as_action1 :field1, :field2, option1: 1, options2: 2
Inside action module you can get fields and options through actions method.
module Actions
module Action1
# class method actions
# get action fields
actions(:action1).field.keys
# iterate each field with options
actions(:action1).field.each do |field, options|
...
end
def do_somthing
# instance method actions do the same
end
end
end
ActionModel works with rails folder structure. Actions can be stored into some folders:
- local action folder path is
/app/model/post
- global action folder path is
/app/models/actions
and/lib/actions
Local action has more priority then global. When actions with the same name are stored in both folder, then local action will be included. It helps to override some action for specific model.
Contributing
- Fork it ( http://github.com//actionmodel/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request