rails_decorators 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +90 -4
- data/lib/generators/decorator/model/templates/module.rb +13 -4
- metadata +1 -1
data/README.markdown
CHANGED
@@ -3,7 +3,7 @@ Rails Decorators
|
|
3
3
|
|
4
4
|
This gem makes it easy to apply the decorator pattern to the models in a Rails application.
|
5
5
|
|
6
|
-
## Why?
|
6
|
+
## Why use decorators?
|
7
7
|
|
8
8
|
Helpers, as they're commonly used, are a bit odd. In both Ruby and Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.
|
9
9
|
|
@@ -11,9 +11,19 @@ The job of a helper is to take in data or a data object and output presentation-
|
|
11
11
|
|
12
12
|
In general, a decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then a decorator might add instance methods like `.formatted_published_at` or `.formatted_title` that output actual HTML.
|
13
13
|
|
14
|
-
## How?
|
14
|
+
## How is it implemented?
|
15
15
|
|
16
|
-
|
16
|
+
To implement the pattern in Rails we can:
|
17
|
+
|
18
|
+
1. Write a module with the decoration methods
|
19
|
+
2. Inject that module into the model
|
20
|
+
3. Utilize those methods within our view layer
|
21
|
+
|
22
|
+
We're not polluting the model layer because it's code stays purely persistence and business logic. The decorator is a part of the view layer, and that's the only place we'd utilize the decoration methods.
|
23
|
+
|
24
|
+
## How do you utilize this gem in your application?
|
25
|
+
|
26
|
+
Here are the steps to utilizing this gem:
|
17
27
|
|
18
28
|
Add the dependency to your `Gemfile`:
|
19
29
|
|
@@ -47,10 +57,86 @@ If you need access to the Rails helpers like `link_to` and `content_tag`, includ
|
|
47
57
|
|
48
58
|
Use the new methods in your views like any other model method (ex: `@article.formatted_published_at`)
|
49
59
|
|
50
|
-
##
|
60
|
+
## Possible Decoration Methods
|
51
61
|
|
52
62
|
Here are some ideas of what you might do in decorator methods:
|
53
63
|
|
54
64
|
* Implement output formatting for `to_csv`, `to_json`, or `to_xml`
|
55
65
|
* Format dates and times using `strftime`
|
56
66
|
* Implement a commonly used representation of the data object like a `.name` method that combines `first_name` and `last_name` attributes
|
67
|
+
|
68
|
+
## Example Using a Decorator
|
69
|
+
|
70
|
+
Say I have a publishing system with `Article` resources. My designer decides that whenever we print the `published_at` timestamp, it should be constructed like this:
|
71
|
+
|
72
|
+
```html
|
73
|
+
<span class='published_at'>
|
74
|
+
<span class='date'>Monday, May 6</span>
|
75
|
+
<span class='time'>8:52AM</span>
|
76
|
+
</span>
|
77
|
+
```
|
78
|
+
|
79
|
+
Could we build that using a partial? Yes. A helper? Uh-huh. But the point of the decorator is to encapsulate logic just like we would a method in our models. Here's how to implement it.
|
80
|
+
|
81
|
+
First, follow the steps above to add the dependency, update your bundle, then run the `rails generate decorator:setup` to prepare your app.
|
82
|
+
|
83
|
+
Each decorator is dedicated to a single model. Since we're talking about the `Article` model we'll create an `ArticleDecorator` module. You could do it by hand, but use the provided generator:
|
84
|
+
|
85
|
+
```
|
86
|
+
rails generate decorator:model Article
|
87
|
+
```
|
88
|
+
|
89
|
+
Now open up the created `app/decorators/article_decorator.rb` and you'll find an `ArticleDecorator` module.
|
90
|
+
|
91
|
+
Why is this a `module`? A traditional decorator implementation uses a second class to completely encapsulate the subject. But because Ruby has such a flexible object model, this isn't necessary. We instead build the decorator as a module and inject that module into the original class. This allows the model's source code to remain purely persistence and business logic while the module contains all the decorations. Neither the model nor the controller layers need to know about the existence of the decorator methods, they can live up at the view layer.
|
92
|
+
|
93
|
+
Within the module, notice that we `extend ActiveSupport::Concern`. This library allows us to mask some of the ugliness of Ruby modules and utilize a simplified syntax. (Here's a great tutorial: http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/)
|
94
|
+
|
95
|
+
Next you'll see commented-out samples for how to utilize Rails' view helper methods. In this example we could make use of the `content_tag` helper, so uncomment the `TagHelper` line. (API Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html)
|
96
|
+
|
97
|
+
Move below those comments and you'll find a nested module named `InstanceMethods`. Any methods you add inside here will be available as instance methods on your object. Within the module, we would add this method:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
def formatted_published_at
|
101
|
+
date = content_tag(:span, published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
|
102
|
+
time = content_tag(:span, published_at.strftime("%l:%M%p").delete(" "), :class => 'time')
|
103
|
+
content_tag :span, date + time, :class => 'published_at'
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
*ASIDE*: Unfortunately, due to the current implementation of `content_tag`, you can't use the style of sending the content is as a block or you'll get an error about `undefined method 'output_buffer='`. Passing in the content as the second argument, as above, works fine.
|
108
|
+
|
109
|
+
Save the decorator file and it'll be auto-loaded on the next request just like the rest of your application. From the view, you can access it like any model method presuming you have an instance of `Article` in the variable `@article`:
|
110
|
+
|
111
|
+
```
|
112
|
+
<%= @article.formatted_published_at %>
|
113
|
+
```
|
114
|
+
|
115
|
+
Ta-da! Object-oriented data formatting for your view layer. Below is the complete decorator with extra comments removed:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
module ArticleDecorator
|
119
|
+
extend ActiveSupport::Concern
|
120
|
+
include ActionView::Helpers::TagHelper
|
121
|
+
|
122
|
+
module InstanceMethods
|
123
|
+
def formatted_published_at
|
124
|
+
date = content_tag(:span, published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
|
125
|
+
time = content_tag(:span, published_at.strftime("%l:%M%p").delete(" "), :class => 'time')
|
126
|
+
content_tag :span, date + time, :class => 'published_at'
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
## License
|
133
|
+
|
134
|
+
(The MIT License)
|
135
|
+
|
136
|
+
Copyright © 2011 Jeff Casimir
|
137
|
+
|
138
|
+
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:
|
139
|
+
|
140
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
141
|
+
|
142
|
+
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.
|
@@ -1,12 +1,21 @@
|
|
1
1
|
module <%= singular_name.camelize %>Decorator
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
|
-
#
|
5
|
-
|
4
|
+
# Access Rails' View Helpers by including any of these:
|
5
|
+
|
6
|
+
# Just the content_tag method and friends:
|
7
|
+
# include ActionView::Helpers::TagHelper
|
8
|
+
#
|
9
|
+
# link_to and button_to:
|
10
|
+
# include ActionView::Helpers::UrlHelper
|
11
|
+
#
|
12
|
+
# pluralize, autolink, and cycle:
|
13
|
+
# include ActionView::Helpers::TextHelper
|
14
|
+
#
|
15
|
+
# Or the whole kitchen sink:
|
16
|
+
# include ActionView::Helpers
|
6
17
|
|
7
18
|
module InstanceMethods
|
8
|
-
# Add your instance methods here
|
9
|
-
|
10
19
|
# Example:
|
11
20
|
# def formatted_created_at
|
12
21
|
# content_tag :span, created_at.strftime("%A")
|