|
4 | 4 | [](https://codeclimate.com/github/light-ruby/light-decorator) |
5 | 5 | [](https://codeclimate.com/github/light-ruby/light-decorator/coverage) |
6 | 6 |
|
| 7 | +Easiest and fast way to decorate Ruby on Rails models. |
| 8 | + |
| 9 | +Decorator pattern - What is it? |
| 10 | +- [Wikipedia](https://en.wikipedia.org/wiki/Decorator_pattern) |
| 11 | +- [Thoughtbot](https://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in) |
| 12 | + |
7 | 13 | ## Installation |
8 | 14 |
|
9 | 15 | Add this line to your application's Gemfile: |
10 | 16 |
|
11 | 17 | ```ruby |
12 | | -gem 'light-decorator' |
| 18 | +gem 'light-decorator', '~> 0.5.0' |
13 | 19 | ``` |
14 | 20 |
|
15 | | -And then execute: |
| 21 | +## Usage |
16 | 22 |
|
17 | | - $ bundle |
| 23 | +Create the `ApplicationDecorator` |
18 | 24 |
|
19 | | -Or install it yourself as: |
| 25 | +```ruby |
| 26 | +# app/decorators/application_decorator.rb |
| 27 | +class ApplicationDecorator < Light::Decorator::Decorate |
| 28 | +end |
| 29 | +``` |
20 | 30 |
|
21 | | - $ gem install light-decorator |
| 31 | +Create decorator for your model. For example we will use the `User` model. |
22 | 32 |
|
23 | | -## Usage |
| 33 | +```ruby |
| 34 | +# app/decorators/user_decorator.rb |
| 35 | +class UserDecorator < Application |
| 36 | + def full_name |
| 37 | + "#{object.first_name} #{object.last_name}" |
| 38 | + end |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +Decorate your model in controller or anywhere. |
24 | 43 |
|
25 | | -TODO: Write usage instructions here |
| 44 | +```ruby |
| 45 | +# Single record |
| 46 | +User.find(params[:id]).decorate |
| 47 | +User.find_and_decorate(params[:id]) |
| 48 | +User.decorate.find(params[:id]) |
| 49 | + |
| 50 | +# Collection |
| 51 | +User.all.decorate |
| 52 | +User.decorate |
| 53 | +User.limit(10).decorate |
| 54 | +User.decorate.limit(10) |
| 55 | + |
| 56 | +# Options |
| 57 | +User.find_and_decorate(params[:id], with: AnotherUserDecorator) |
| 58 | + |
| 59 | +# Associations will be decorated automatically |
| 60 | +user = User.find_and_decorate(params[:id]) |
| 61 | +user.decorated? # true |
| 62 | +user.comments.decorated? # true |
| 63 | +user.comments.first.decorated? # true |
| 64 | +``` |
| 65 | + |
| 66 | +Example of Decorator |
| 67 | +```ruby |
| 68 | +class UserDecorator < ApplicationDecorator |
| 69 | + def full_name |
| 70 | + "#{object.first_name} #{object.last_name}" |
| 71 | + # or |
| 72 | + "#{o.first_name} #{o.last_name}" |
| 73 | + end |
| 74 | + |
| 75 | + def full_name_link |
| 76 | + helpers.link_to full_name, user_path(object) |
| 77 | + # or |
| 78 | + h.link_to full_name, user_path(object) |
| 79 | + end |
| 80 | +end |
| 81 | +``` |
26 | 82 |
|
27 | | -## Development |
| 83 | +## Next steps |
28 | 84 |
|
29 | | -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 85 | +- [ ] Create rake task `light-decorator:install` |
| 86 | +- [ ] Create generators |
| 87 | +- [ ] Create configuration file |
30 | 88 |
|
31 | 89 | ## Contributing |
32 | 90 |
|
|
0 commit comments