mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Updating README.
Thanks @beerlington!
This commit is contained in:
parent
3356b5ffe7
commit
f1bd79b71d
1 changed files with 8 additions and 16 deletions
|
@ -155,20 +155,12 @@ gem "draper"
|
|||
|
||||
Then run `bundle` from the project directory.
|
||||
|
||||
### Run the draper:install command
|
||||
|
||||
This will create the `app/decorators` directory and the `ApplicationDecorator` inside it.
|
||||
|
||||
```
|
||||
rails generate draper:install
|
||||
```
|
||||
|
||||
### Generate the Decorator
|
||||
|
||||
To decorate a model named `Article`:
|
||||
|
||||
```
|
||||
rails generate draper:decorator article
|
||||
rails generate decorator article
|
||||
```
|
||||
|
||||
### Writing Methods
|
||||
|
@ -176,7 +168,7 @@ rails generate draper:decorator article
|
|||
Open the decorator model (ex: `app/decorators/article_decorator.rb`) and add normal instance methods. To access the wrapped source object, use a method named after the `decorates` argument:
|
||||
|
||||
```ruby
|
||||
class ArticleDecorator < ApplicationDecorator
|
||||
class ArticleDecorator < Draper::Base
|
||||
decorates :article
|
||||
|
||||
def author_name
|
||||
|
@ -190,7 +182,7 @@ end
|
|||
You probably want to make use of Rails helpers and those defined in your application. Use the `helpers` or `h` method proxy:
|
||||
|
||||
```ruby
|
||||
class ArticleDecorator < ApplicationDecorator
|
||||
class ArticleDecorator < Draper::Base
|
||||
decorates :article
|
||||
|
||||
def published_at
|
||||
|
@ -206,7 +198,7 @@ end
|
|||
Hate seeing that `h.` proxy all over? Willing to mix a bazillion methods into your decorator? Then try lazy helpers:
|
||||
|
||||
```ruby
|
||||
class ArticleDecorator < ApplicationDecorator
|
||||
class ArticleDecorator < Draper::Base
|
||||
decorates :article
|
||||
include Draper::LazyHelpers
|
||||
|
||||
|
@ -313,7 +305,7 @@ end
|
|||
Then you need to perform the wrapping in your controller. Here's the simplest method:
|
||||
|
||||
```ruby
|
||||
class ArticlesController < ApplicationController
|
||||
class ArticlesController < Draper::Decorator
|
||||
def show
|
||||
@article = ArticleDecorator.find params[:id]
|
||||
end
|
||||
|
@ -329,7 +321,7 @@ Then within your views you can utilize both the normal data methods and your new
|
|||
Ta-da! Object-oriented data formatting for your view layer. Below is the complete decorator with extra comments removed:
|
||||
|
||||
```ruby
|
||||
class ArticleDecorator < ApplicationDecorator
|
||||
class ArticleDecorator < Draper::Base
|
||||
decorates :article
|
||||
|
||||
def published_at
|
||||
|
@ -345,12 +337,12 @@ end
|
|||
Add a `decorates_association :association_name` to gain access to a decorated version of your target association.
|
||||
|
||||
```ruby
|
||||
class ArticleDecorator < ApplicationDecorator
|
||||
class ArticleDecorator < Draper::Base
|
||||
decorates :article
|
||||
decorates_association :author # belongs_to :author association
|
||||
end
|
||||
|
||||
class AuthorDecorator < ApplicationDecorator
|
||||
class AuthorDecorator < Draper::Base
|
||||
decorates :author
|
||||
|
||||
def fancy_name
|
||||
|
|
Loading…
Reference in a new issue