forked from amatsuda/active_decorator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2011 Akira Matsuda | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# ActiveDecorator | ||
|
||
A simple and Rubyish view helper for ActiveRecord models. Keep your helpers and views Object-Oriented! | ||
|
||
|
||
## Features ## | ||
|
||
1. automatically mixes decorator module into corresponding model only when: | ||
1. passing a model or collection of models or an instance of ActiveRecord::Relation from controllers to views | ||
2. rendering partials with models (using `:collection` or `:object` or `:locals` explicitly or implicitly) | ||
2. the decorator module runs in the model's context. So, you can directly call any attributes or methods in the decorator module | ||
3. since decorators are considered as sort of helpers, you can also call any ActionView's helper methods such as `content_tag` or `link_to` | ||
|
||
|
||
## Supported versions ## | ||
|
||
Rails 3.0.x, 3.1.x, and 3.2 (edge) | ||
|
||
|
||
## Usage ## | ||
|
||
1. bundle 'active_decorator' gem | ||
2. create a decorator module for each AR model. For example, a decorator for a model `User` should be named `UserDecorator`. | ||
You can use the generator for doing this ( `% rails g decorator user` ) | ||
3. Then it's all done. Without altering any single line of the existing code, the decorator modules will be automatically mixed into your models only in the view context. | ||
|
||
|
||
## Examples ## | ||
|
||
# app/models/user.rb | ||
class User < ActiveRecord::Base | ||
# first_name:string last_name:string website:string | ||
end | ||
|
||
# app/decorators/user_decorator.rb | ||
module UserDecorator | ||
def full_name | ||
"#{first_name} #{last_name}" | ||
end | ||
|
||
def link | ||
link_to full_name, website | ||
end | ||
end | ||
|
||
# app/controllers/users_controller.rb | ||
class UsersController < ApplicationController | ||
def index | ||
User.all | ||
end | ||
end | ||
|
||
# app/views/users/index.html.erb | ||
<% @users.each do |user| %> | ||
<%= user.link %><br> | ||
<% end %> | ||
|
||
|
||
## Contributing to ActiveDecorator ## | ||
|
||
* Fork, fix, then send me a pull request. | ||
|
||
|
||
## Copyright ## | ||
|
||
Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters