Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkazar committed Feb 14, 2014
1 parent 8276657 commit 14f9453
Showing 1 changed file with 117 additions and 1 deletion.
118 changes: 117 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,123 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
If you have complex model class it can be split into clear model class and actions. For example, `Post` model

```ruby
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`

```ruby
class Post
acts_as_searchable :title, ignorecase: true
acts_as :pageable, :tagable
end
```

Searchable action

```ruby
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

```ruby
module Actions
module Pageable
extend ActiveSupport::Concern

included do
scope :page, ->(page, size) { ... }
end
end
end
```

Tagable action

```ruby
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

```ruby
acts_as :action1, :action2, ...
```

or with fields and options

```ruby
acts_as_action1 :field1, :field2, option1: 1, options2: 2
```

Inside action module you can get fields and options through actions method.

```ruby
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

Expand Down

0 comments on commit 14f9453

Please sign in to comment.