Sha256: 24e80ddf69f866496ec375cde8c4f64d7541c1c23d2642568a2ff389f55a4de7

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# 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.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_decorator-0.1.0 README.md