draper 1.0.0.beta5 → 1.0.0.beta6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,15 @@
1
1
  # Draper Changelog
2
2
 
3
+ ## 1.0.0.beta6
4
+
5
+ * Fix up README to include changes made. [https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6](https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6)
6
+
7
+ * `CollectionDecorator` no longer freezes its collection: direct access is discouraged by making access private [https://github.com/drapergem/draper/commit/c6d60e6577ed396385f3f1151c3f188fe47e9a57](https://github.com/drapergem/draper/commit/c6d60e6577ed396385f3f1151c3f188fe47e9a57)
8
+
9
+ * A fix for `Decoratable#==` [https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65](https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65)
10
+
11
+ * Ensure we coerce to an array in the right place. [https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095](https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095)
12
+
3
13
  ## 1.0.0.beta5
4
14
 
5
15
  * Change CollectionDecorator to freeze its collection [https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4](https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4)
@@ -0,0 +1,11 @@
1
+ # Contributing to Draper
2
+
3
+ So you want to help us out... thanks! Here's a quick how-to:
4
+
5
+ 1. [Fork the project](https://help.github.com/articles/fork-a-repo).
6
+ 2. Create a branch - `git checkout -b adding_magic`
7
+ 3. Make your changes, and add some tests!
8
+ 4. Check that the tests pass - `bundle exec rake`
9
+ 5. Commit your changes - `git commit -am "Added some magic"`
10
+ 6. Push the branch to Github - `git push origin adding_magic`
11
+ 7. Send us a [pull request](https://help.github.com/articles/using-pull-requests)!
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ (The MIT License)
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
@@ -0,0 +1,327 @@
1
+ # Draper: View Models for Rails
2
+
3
+ [![TravisCI Build Status](https://secure.travis-ci.org/drapergem/draper.png?branch=master)](http://travis-ci.org/drapergem/draper)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/drapergem/draper)
5
+
6
+ Draper adds a nicely-separated object-oriented layer of presentation logic to your Rails apps. Previously, this logic might have been tangled up in procedural helpers, or contributing to your fat models' weight problems. Now, you can wrap your models up in decorators to organise - and test - this layer of your app much more effectively.
7
+
8
+
9
+ ## Overview
10
+
11
+ With Draper, your `Article` model has a corresponding `ArticleDecorator`. The decorator wraps the model, and deals only with presentational concerns. In the controller, you simply decorate your article before handing it off to the view.
12
+
13
+ ```ruby
14
+ # app/controllers/articles_controller.rb
15
+ def show
16
+ @article = Article.find(params[:id]).decorate
17
+ end
18
+ ```
19
+
20
+ In the view, you can use the decorator in exactly the same way as you would have used the model. The difference is, any time you find yourself needing to write a helper, you can implement a method on the decorator instead. For example, this helper:
21
+
22
+ ```ruby
23
+ # app/helpers/articles_helper.rb
24
+ def publication_status(article)
25
+ if article.published?
26
+ "Published at #{article.published_at.strftime('%A, %B %e')}"
27
+ else
28
+ "Unpublished"
29
+ end
30
+ end
31
+ ```
32
+
33
+ could be better written as:
34
+
35
+ ```ruby
36
+ # app/decorators/article_decorator.rb
37
+ class ArticleDecorator < Draper::Decorator
38
+ def publication_status
39
+ if published?
40
+ "Published at #{published_at}"
41
+ else
42
+ "Unpublished"
43
+ end
44
+ end
45
+
46
+ def published_at
47
+ source.published_at.strftime("%A, %B %e")
48
+ end
49
+ end
50
+ ```
51
+
52
+ Notice that the `published?` method can be called even though `ArticleDecorator` doesn't define it - the decorator delegates methods to the source model. However, we can override methods like `published_at` to add presentation-specific formatting, in which case we access the underlying model using the `source` method.
53
+
54
+ You might have heard this sort of decorator called a "presenter", an "exhibit", a "view model", or even just a "view" (in that nomenclature, what Rails calls "views" are actually "templates"). Whatever you call it, it's a great way to replace procedural helpers like the one above with "real" object-oriented programming.
55
+
56
+ Decorators are the ideal place to:
57
+ * format dates and times using `strftime`,
58
+ * define commonly-used representations of an object, like a `name` method that combines `first_name` and `last_name` attributes,
59
+ * mark up attributes with a little semantic HTML, like turning a `url` field into a hyperlink.
60
+
61
+
62
+ ## Installation
63
+
64
+ Add Draper to your Gemfile:
65
+
66
+ ```ruby
67
+ gem 'draper', '~> 1.0'
68
+ ```
69
+
70
+ And run `bundle install` within your app's directory.
71
+
72
+
73
+ ## Writing decorators
74
+
75
+ Decorators inherit from `Draper::Decorator`, live in your `app/decorators` directory, and are named for the model that they decorate:
76
+
77
+ ```ruby
78
+ # app/decorators/article_decorator.rb
79
+ class ArticleDecorator < Draper::Decorator
80
+ # ...
81
+ end
82
+ ```
83
+
84
+ ### Generators
85
+
86
+ When you generate a resource with `rails generate resource Article`, you get a decorator for free! But if the `Article` model already exists, you can run `rails generate decorator Article` to create the `ArticleDecorator`.
87
+
88
+ ### Accessing helpers
89
+
90
+ Procedural helpers are still useful for generic tasks like generating HTML, and as such you can access all this goodness (both built-in Rails helpers, and your own) through the `helpers` method:
91
+
92
+ ```ruby
93
+ class ArticleDecorator < Draper::Decorator
94
+ def emphatic
95
+ helpers.content_tag(:strong, "Awesome")
96
+ end
97
+ end
98
+ ```
99
+
100
+ To save your typing fingers it's aliased to `h`. If that's still too much effort, just pop `include Draper::LazyHelpers` at the top of your decorator class - you'll mix in a bazillion methods and never have to type `h.` again... [if that's your sort of thing](https://github.com/garybernhardt/base).
101
+
102
+ ### Accessing the model
103
+
104
+ Decorators will delegate methods to the model where possible, which means in most cases you can replace a model with a decorator and your view won't notice the difference. When you need to get your hands on the underlying model the `source` method is your friend (and its aliases `model` and `to_source`):
105
+
106
+ ```ruby
107
+ class ArticleDecorator < Draper::Decorator
108
+ def published_at
109
+ source.published_at.strftime("%A, %B %e")
110
+ end
111
+ end
112
+ ```
113
+
114
+
115
+ ## Decorating
116
+
117
+ ### Single objects
118
+
119
+ Ok, so you've written a sweet decorator, now you're going to want to put it in action! A simple option is to call the `decorate` method on your model:
120
+
121
+ ```ruby
122
+ @article = Article.first.decorate
123
+ ```
124
+
125
+ This infers the decorator from the object being decorated. If you want more control - say you want to decorate a `Widget` with a more general `ProductDecorator` - then you can instantiate a decorator directly:
126
+
127
+ ```ruby
128
+ @widget = ProductDecorator.new(Widget.first)
129
+ # or, equivalently
130
+ @widget = ProductDecorator.decorate(Widget.first)
131
+ ```
132
+
133
+ ### Collections
134
+
135
+ If you have a whole bunch of objects, you can decorate them all in one fell swoop:
136
+
137
+ ```ruby
138
+ @articles = ArticleDecorator.decorate_collection(Article.all)
139
+ # or, for scopes (but not `all`)
140
+ @articles = Article.popular.decorate
141
+ ```
142
+
143
+ If you want to add methods to your decorated collection (for example, for pagination), you can subclass `Draper::CollectionDecorator`:
144
+
145
+ ```ruby
146
+ # app/decorators/articles_decorator.rb
147
+ class ArticlesDecorator < Draper::CollectionDecorator
148
+ def page_number
149
+ 42
150
+ end
151
+ end
152
+
153
+ # elsewhere...
154
+ @articles = ArticlesDecorator.new(Article.all)
155
+ # or, equivalently
156
+ @articles = ArticlesDecorator.decorate(Article.all)
157
+ ```
158
+
159
+ Draper guesses the decorator used for each item from the name of the collection decorator (`ArticlesDecorator` becomes `ArticleDecorator`). If that fails, it falls back to using each item's `decorate` method. Alternatively, you can specify a decorator by overriding the collection decorator's `decorator_class` method.
160
+
161
+ Some pagination gems add methods to `ActiveRecord::Relation`. For example, [Kaminari](https://github.com/amatsuda/kaminari)'s `paginate` helper method requires the collection to implement `current_page`, `total_pages`, and `limit_value`. To expose these on a collection decorator, you can simply delegate to `source`:
162
+
163
+ ```ruby
164
+ class PaginatingDecorator < Draper::CollectionDecorator
165
+ delegate :current_page, :total_pages, :limit_value, to: :source
166
+ end
167
+ ```
168
+
169
+ ### Handy shortcuts
170
+
171
+ You can automatically decorate associated models:
172
+
173
+ ```ruby
174
+ class ArticleDecorator < Draper::Decorator
175
+ decorates_association :author
176
+ end
177
+ ```
178
+
179
+ And, if you want, you can add decorated finder methods:
180
+
181
+ ```ruby
182
+ class ArticleDecorator < Draper::Decorator
183
+ decorates_finders
184
+ end
185
+ ```
186
+
187
+ so that you can do:
188
+
189
+ ```ruby
190
+ @article = ArticleDecorator.find(params[:id])
191
+ ```
192
+
193
+
194
+ ## Testing
195
+
196
+ Draper supports RSpec and Minitest::Rails out of the box, and should work with Test::Unit as well.
197
+
198
+ ### RSpec
199
+
200
+ Your specs should live in `spec/decorators` (if not, you need to tag them with `type: :decorator`).
201
+
202
+ In controller specs, you might want to check whether your instance variables are being decorated properly. You can use the handy predicate matchers:
203
+
204
+ ```ruby
205
+ assigns(:article).should be_decorated
206
+ # or, if you want to be more specific
207
+ assigns(:article).should be_decorated_with ArticleDecorator
208
+ ```
209
+
210
+ Note that `model.decorate == model`, so your existing specs shouldn't break when you add the decoration.
211
+
212
+ Spork users should `require 'draper/test/rspec_integration'` in the `Spork.prefork` block.
213
+
214
+
215
+ ## Advanced usage
216
+
217
+ ### `ApplicationDecorator`
218
+
219
+ If you need common methods in your decorators, you can create an `ApplicationDecorator`:
220
+
221
+ ```ruby
222
+ # app/decorators/application_decorator.rb
223
+ class ApplicationDecorator < Draper::Decorator
224
+ # ...
225
+ end
226
+ ```
227
+
228
+ and inherit from it instead of directly from `Draper::Decorator`.
229
+
230
+ ### Enforcing an interface between controllers and views
231
+
232
+ If you want to strictly control which methods are called in your views, you can restrict the methods that the decorator delegates to the model. Use `denies` to blacklist methods:
233
+
234
+ ```ruby
235
+ class ArticleDecorator < Draper::Decorator
236
+ # allow everything except `title` and `author` to be delegated
237
+ denies :title, :author
238
+ end
239
+ ```
240
+
241
+ or, better, use `allows` for a whitelist:
242
+
243
+ ```ruby
244
+ class ArticleDecorator < Draper::Decorator
245
+ # only allow `title` and `author` to be delegated to the model
246
+ allows :title, :author
247
+ end
248
+ ```
249
+
250
+ You can prevent method delegation altogether using `denies_all`.
251
+
252
+ ### Adding context
253
+
254
+ If you need to pass extra data to your decorators, you can use a `context` hash. Methods that create decorators take it as an option, for example
255
+
256
+ ```ruby
257
+ Article.first.decorate(context: {role: :admin})
258
+ ```
259
+
260
+ The value passed to the `:context` option is then available in the decorator through the `context` method.
261
+
262
+ If you use `decorates_association`, the context of the parent decorator is passed to the associated decorators. You can override this with the `:context` option:
263
+
264
+ ```ruby
265
+ class ArticleDecorator < Draper::Decorator
266
+ decorates_association :author, context: {foo: "bar"}
267
+ end
268
+ ```
269
+
270
+ or, if you simply want to modify the parent's context, use a lambda that takes a hash and returns a new hash:
271
+
272
+ ```ruby
273
+ class ArticleDecorator < Draper::Decorator
274
+ decorates_association :author,
275
+ context: ->(parent_context){ parent_context.merge(foo: "bar") }
276
+ end
277
+ ```
278
+
279
+ ### Specifying decorators
280
+
281
+ When you're using `decorates_association`, Draper uses the `decorate` method on the associated record (or each associated record, in the case of a collection association) to perform the decoration. If you want use a specific decorator, you can use the `:with` option:
282
+
283
+ ```ruby
284
+ class ArticleDecorator < Draper::Decorator
285
+ decorates_association :author, with: FancyPersonDecorator
286
+ end
287
+ ```
288
+
289
+ For a collection association, you can specify a `CollectionDecorator` subclass, which is applied to the whole collection, or a singular `Decorator` subclass, which is applied to each item individually.
290
+
291
+ ### Scoping associations
292
+
293
+ If you want your decorated association to be ordered, limited, or otherwise scoped, you can pass a `:scope` option to `decorates_association`, which will be applied to the collection before decoration:
294
+
295
+ ```ruby
296
+ class ArticleDecorator < Draper::Decorator
297
+ decorates_association :comments, scope: :recent
298
+ end
299
+ ```
300
+
301
+ ### Breaking with convention
302
+
303
+ If, as well as instance methods, you want to proxy class methods to the model through the decorator (including when using `decorates_finders`), Draper needs to know the model class. By default, it assumes that your decorators are named `SomeModelDecorator`, and then attempts to proxy unknown class methods to `SomeModel`. If your model name can't be inferred from your decorator name in this way, you need to use the `decorates` method:
304
+
305
+ ```ruby
306
+ class MySpecialArticleDecorator < Draper::Decorator
307
+ decorates :article
308
+ end
309
+ ```
310
+
311
+ You don't need to worry about this if you don't want to proxy class methods.
312
+
313
+ ### Making models decoratable
314
+
315
+ Models get their `decorate` method from the `Draper::Decoratable` module, which is included in `ActiveRecord::Base` and `Mongoid::Document` by default. If you're using another ORM, or want to decorate plain old Ruby objects, you can include this module manually.
316
+
317
+
318
+ ## Contributors
319
+
320
+ Draper was conceived by Jeff Casimir and heavily refined by Steve Klabnik and a great community of open source [contributors](https://github.com/drapergem/draper/contributors).
321
+
322
+ ### Core Team
323
+
324
+ * Jeff Casimir (jeff@jumpstartlab.com)
325
+ * Steve Klabnik (steve@jumpstartlab.com)
326
+ * Vasiliy Ermolovich
327
+ * Andrew Haines
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Jeff Casimir", "Steve Klabnik"]
9
9
  s.email = ["jeff@casimircreative.com", "steve@steveklabnik.com"]
10
10
  s.homepage = "http://github.com/drapergem/draper"
11
- s.summary = "Decorator pattern implementation for Rails."
12
- s.description = "Draper implements a decorator or presenter pattern for Rails applications."
11
+ s.summary = "View Models for Rails"
12
+ s.description = "Draper adds a nicely-separated object-oriented layer of presentation logic to your Rails apps."
13
13
  s.rubyforge_project = "draper"
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -3,20 +3,17 @@ module Draper
3
3
  include Enumerable
4
4
  include ViewHelpers
5
5
 
6
- attr_reader :source
7
- alias_method :to_source, :source
8
-
9
6
  attr_accessor :context
10
7
 
11
8
  array_methods = Array.instance_methods - Object.instance_methods
12
- delegate :as_json, *array_methods, to: :decorated_collection
9
+ delegate :==, :as_json, *array_methods, to: :decorated_collection
13
10
 
14
11
  # @param source collection to decorate
15
12
  # @option options [Class] :with the class used to decorate items
16
13
  # @option options [Hash] :context context available to each item's decorator
17
14
  def initialize(source, options = {})
18
15
  options.assert_valid_keys(:with, :context)
19
- @source = source.dup.freeze
16
+ @source = source
20
17
  @decorator_class = options[:with]
21
18
  @context = options.fetch(:context, {})
22
19
  end
@@ -26,7 +23,7 @@ module Draper
26
23
  end
27
24
 
28
25
  def decorated_collection
29
- @decorated_collection ||= source.map{|item| decorate_item(item)}.freeze
26
+ @decorated_collection ||= source.map{|item| decorate_item(item)}
30
27
  end
31
28
 
32
29
  def find(*args, &block)
@@ -37,10 +34,6 @@ module Draper
37
34
  end
38
35
  end
39
36
 
40
- def ==(other)
41
- source == (other.respond_to?(:source) ? other.source : other)
42
- end
43
-
44
37
  def to_s
45
38
  klass = begin
46
39
  decorator_class
@@ -62,6 +55,8 @@ module Draper
62
55
 
63
56
  protected
64
57
 
58
+ attr_reader :source
59
+
65
60
  def decorate_item(item)
66
61
  item_decorator.call(item, context: context)
67
62
  end
@@ -22,7 +22,7 @@ module Draper::Decoratable
22
22
  end
23
23
 
24
24
  def ==(other)
25
- super || (other.respond_to?(:source) && super(other.source))
25
+ super || (other.respond_to?(:source) && self == other.source)
26
26
  end
27
27
 
28
28
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module Draper
2
- VERSION = "1.0.0.beta5"
2
+ VERSION = "1.0.0.beta6"
3
3
  end
@@ -16,27 +16,6 @@ describe Draper::CollectionDecorator do
16
16
  subject.map{|item| item.source}.should == source
17
17
  end
18
18
 
19
- describe "#source" do
20
- it "duplicates the source collection" do
21
- subject.source.should == source
22
- subject.source.should_not be source
23
- end
24
-
25
- it "is frozen" do
26
- subject.source.should be_frozen
27
- end
28
-
29
- it "is aliased to #to_source" do
30
- subject.to_source.should == source
31
- end
32
- end
33
-
34
- describe "#decorated_collection" do
35
- it "is frozen" do
36
- subject.decorated_collection.should be_frozen
37
- end
38
- end
39
-
40
19
  context "with context" do
41
20
  subject { Draper::CollectionDecorator.new(source, with: ProductDecorator, context: {some: 'context'}) }
42
21
 
@@ -151,7 +130,6 @@ describe Draper::CollectionDecorator do
151
130
  describe "#find" do
152
131
  context "with a block" do
153
132
  it "decorates Enumerable#find" do
154
- subject.stub decorated_collection: []
155
133
  subject.decorated_collection.should_receive(:find)
156
134
  subject.find {|p| p.title == "title" }
157
135
  end
@@ -208,13 +186,12 @@ describe Draper::CollectionDecorator do
208
186
  describe "#to_ary" do
209
187
  # required for `render @collection` in Rails
210
188
  it "delegates to the decorated collection" do
211
- subject.stub decorated_collection: double(to_ary: :an_array)
189
+ subject.decorated_collection.stub to_ary: :an_array
212
190
  subject.to_ary.should == :an_array
213
191
  end
214
192
  end
215
193
 
216
194
  it "delegates array methods to the decorated collection" do
217
- subject.stub decorated_collection: []
218
195
  subject.decorated_collection.should_receive(:[]).with(42).and_return(:the_answer)
219
196
  subject[42].should == :the_answer
220
197
  end
@@ -251,6 +228,16 @@ describe Draper::CollectionDecorator do
251
228
  a.should_not == b
252
229
  end
253
230
  end
231
+
232
+ context "when the decorated collection has been modified" do
233
+ it "is no longer equal to the source" do
234
+ a = Draper::CollectionDecorator.new(source, with: ProductDecorator)
235
+ b = source.dup
236
+
237
+ a << Product.new.decorate
238
+ a.should_not == b
239
+ end
240
+ end
254
241
  end
255
242
 
256
243
  describe "#to_s" do
@@ -150,7 +150,7 @@ describe Draper::Decoratable do
150
150
 
151
151
  decorator.should be_a Draper::CollectionDecorator
152
152
  decorator.decorator_class.should be WidgetDecorator
153
- decorator.source.should == Product.scoped
153
+ decorator.should == Product.scoped
154
154
  end
155
155
 
156
156
  it "accepts context" do
@@ -86,9 +86,9 @@ describe Draper::DecoratedAssociation do
86
86
  let(:options) { {scope: :foo} }
87
87
 
88
88
  it "applies the scope before decoration" do
89
- scoped = [:scoped]
89
+ scoped = [Product.new]
90
90
  associated.should_receive(:foo).and_return(scoped)
91
- decorated_association.call.source.should == scoped
91
+ decorated_association.call.should == scoped
92
92
  end
93
93
  end
94
94
  end
@@ -94,7 +94,7 @@ describe Draper::Decorator do
94
94
 
95
95
  it "returns a collection decorator" do
96
96
  subject.should be_a Draper::CollectionDecorator
97
- subject.source.should == source
97
+ subject.should == source
98
98
  end
99
99
 
100
100
  it "uses itself as the item decorator by default" do
@@ -73,7 +73,7 @@ describe Draper::Finders do
73
73
  Product.stub(:find_all_by_name).and_return(found)
74
74
  decorator = ProductDecorator.find_all_by_name("apples")
75
75
  decorator.should be_a Draper::CollectionDecorator
76
- decorator.source.should == found
76
+ decorator.should == found
77
77
  end
78
78
  end
79
79
 
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Post do
4
+ describe "#==" do
5
+ before { Post.create }
6
+ subject { Post.first }
7
+
8
+ it "is true for other instances' decorators" do
9
+ other = Post.first
10
+ subject.should_not be other
11
+ (subject == other.decorate).should be_true
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta5
4
+ version: 1.0.0.beta6
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-27 00:00:00.000000000 Z
13
+ date: 2012-12-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -172,7 +172,8 @@ dependencies:
172
172
  - - ! '>='
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
- description: Draper implements a decorator or presenter pattern for Rails applications.
175
+ description: Draper adds a nicely-separated object-oriented layer of presentation
176
+ logic to your Rails apps.
176
177
  email:
177
178
  - jeff@casimircreative.com
178
179
  - steve@steveklabnik.com
@@ -184,11 +185,13 @@ files:
184
185
  - .rspec
185
186
  - .travis.yml
186
187
  - .yardopts
187
- - CHANGELOG.markdown
188
+ - CHANGELOG.md
189
+ - CONTRIBUTING.md
188
190
  - Gemfile
189
191
  - Guardfile
192
+ - LICENSE
193
+ - README.md
190
194
  - Rakefile
191
- - Readme.markdown
192
195
  - draper.gemspec
193
196
  - lib/draper.rb
194
197
  - lib/draper/collection_decorator.rb
@@ -266,6 +269,7 @@ files:
266
269
  - spec/dummy/script/rails
267
270
  - spec/dummy/spec/decorators/post_decorator_spec.rb
268
271
  - spec/dummy/spec/mailers/post_mailer_spec.rb
272
+ - spec/dummy/spec/models/post_spec.rb
269
273
  - spec/dummy/spec/spec_helper.rb
270
274
  - spec/generators/decorator/decorator_generator_spec.rb
271
275
  - spec/integration/integration_spec.rb
@@ -317,7 +321,7 @@ rubyforge_project: draper
317
321
  rubygems_version: 1.8.23
318
322
  signing_key:
319
323
  specification_version: 3
320
- summary: Decorator pattern implementation for Rails.
324
+ summary: View Models for Rails
321
325
  test_files:
322
326
  - spec/draper/collection_decorator_spec.rb
323
327
  - spec/draper/decoratable_spec.rb
@@ -370,6 +374,7 @@ test_files:
370
374
  - spec/dummy/script/rails
371
375
  - spec/dummy/spec/decorators/post_decorator_spec.rb
372
376
  - spec/dummy/spec/mailers/post_mailer_spec.rb
377
+ - spec/dummy/spec/models/post_spec.rb
373
378
  - spec/dummy/spec/spec_helper.rb
374
379
  - spec/generators/decorator/decorator_generator_spec.rb
375
380
  - spec/integration/integration_spec.rb
@@ -1,383 +0,0 @@
1
- # Draper: View Models for Rails
2
-
3
- [![TravisCI Build Status](https://secure.travis-ci.org/drapergem/draper.png?branch=master)](http://travis-ci.org/drapergem/draper)
4
- [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/drapergem/draper)
5
-
6
- ## Quick Start
7
-
8
- 1. Add `gem 'draper'` to your `Gemfile` and `bundle`
9
- 2. When you generate a resource with `rails g resource YourModel`, you get a decorator automatically!
10
- 3. If `YourModel` already exists, run `rails g decorator YourModel` to create `YourModelDecorator`
11
- 4. Edit `app/decorators/[your_model]_decorator.rb` using:
12
- 1. `h` to proxy to Rails/application helpers like `h.current_user`
13
- 2. the name of your decorated model to access the wrapped object like `article.created_at`
14
- 5. Wrap models in your controller with the decorator using:
15
- 1. `.decorate` method with a single object,
16
- ex: `ArticleDecorator.decorate(Article.first)`
17
- 2. `.new` method with single object
18
- ex: `ArticleDecorator.new(Article.first)`
19
- 3. `.decorate_collection` method with a collection,
20
- ex: `ArticleDecorator.decorate_collection(Article.all)`
21
- 6. Call decorator methods from your view templates
22
- ex: `<%= @article_decorator.created_at %>`
23
-
24
- If you need common methods in your decorators, create an `app/decorators/application_decorator.rb`:
25
-
26
- ``` ruby
27
- class ApplicationDecorator < Draper::Decorator
28
- # your methods go here
29
- end
30
- ```
31
-
32
- and make your decorators inherit from it. Newly generated decorators will respect this choice and inherit from `ApplicationDecorator`.
33
-
34
- ## Goals
35
-
36
- This gem makes it easy to apply the decorator pattern to domain models in a Rails application. This pattern gives you three wins:
37
-
38
- 1. Replace most helpers with an object-oriented approach
39
- 2. Filter data at the presentation level
40
- 3. Enforce an interface between your controllers and view templates.
41
-
42
- ### 1. Object Oriented Helpers
43
-
44
- Why hate normal helpers? In Ruby/Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.The job of a helper is to take in data and output a presentation-ready string. We can do that with a decorator.
45
-
46
- A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this:
47
-
48
- ```ruby
49
- class ArticleDecorator < Draper::Decorator
50
- def published_at
51
- date = h.content_tag(:span, article.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
52
- time = h.content_tag(:span, article.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
53
- h.content_tag :span, date + time, :class => 'created_at'
54
- end
55
- end
56
- ```
57
-
58
- ### 2. View-Layer Data Filtering
59
-
60
- Have you ever written a `to_xml` or `to_json` method in your model? Did it feel weird to put presentation logic in your model?
61
-
62
- Or, in the course of formatting this data, did you wish you could access `current_user` down in the model? Maybe for guests your `to_json` is only going to show three attributes, but if the user is an admin they get to see them all.
63
-
64
- How would you handle this in the model layer? You'd probably pass the `current_user` or some role/flag down to `to_json`. That should still feel slimy.
65
-
66
- When you use a decorator you have the power of a Ruby object but it's a part of the view layer. This is where your `to_json` belongs. You can access your `current_user` helper method using the `h` proxy available in the decorator:
67
-
68
- ```ruby
69
- class ArticleDecorator < ApplicationDecorator
70
- ADMIN_VISIBLE_ATTRIBUTES = [:title, :body, :author, :status]
71
- PUBLIC_VISIBLE_ATTRIBUTES = [:title, :body]
72
-
73
- def to_json
74
- attr_set = h.current_user.admin? ? ADMIN_VISIBLE_ATTRIBUTES : PUBLIC_VISIBLE_ATTRIBUTES
75
- article.to_json(:only => attr_set)
76
- end
77
- end
78
- ```
79
-
80
- ### 3. Enforcing an Interface
81
-
82
- Want to strictly control what methods are proxied to the original object? Use `denies` or `allows`.
83
-
84
- #### Using `denies`
85
-
86
- The `denies` method takes a blacklist approach. For instance:
87
-
88
- ```ruby
89
- class ArticleDecorator < ApplicationDecorator
90
- denies :title
91
- end
92
- ```
93
-
94
- Then, to test it:
95
-
96
- ```irb
97
- > ad = ArticleDecorator.new(Article.find(1))
98
- => #<ArticleDecorator:0x000001020d7728 @model=#<Article id: 1, title: "Hello, World">>
99
- > ad.title
100
- NoMethodError: undefined method `title' for #<ArticleDecorator:0x000001020d7728>
101
- ```
102
-
103
- You may also blacklist all methods by using `denies_all`:
104
-
105
- ```ruby
106
- class ArticleDecorate < ApplicationDecorator
107
- denies_all
108
- end
109
- ```
110
-
111
- #### Using `allows`
112
-
113
- A better approach is to define a whitelist using `allows`:
114
-
115
- ```ruby
116
- class ArticleDecorator < ApplicationDecorator
117
- allows :title, :description
118
- end
119
- ```
120
-
121
- ```irb
122
- > ad = ArticleDecorator.new(Article.find(1))
123
- => #<ArticleDecorator:0x000001020d7728 @model=#<Article id: 1, title: "Hello, World">>
124
- > ad.title
125
- => "Hello, World"
126
- > ad.created_at
127
- NoMethodError: undefined method `created_at' for #<ArticleDecorator:0x000001020d7728>
128
- ```
129
-
130
- ## Up and Running
131
-
132
- ### Setup
133
-
134
- Add the dependency to your `Gemfile`:
135
-
136
- ```
137
- gem "draper"
138
- ```
139
-
140
- Then run `bundle` from the project directory.
141
-
142
- ### Generate the Decorator
143
-
144
- To decorate a model named `Article`:
145
-
146
- ```
147
- rails generate decorator article
148
- ```
149
-
150
- ### Writing Methods
151
-
152
- Open the decorator model (ex: `app/decorators/article_decorator.rb`) and add normal instance methods. To access the wrapped source object, use the `model` method:
153
-
154
- ```ruby
155
- class ArticleDecorator < Draper::Decorator
156
- def author_name
157
- model.author.first_name + " " + model.author.last_name
158
- end
159
- end
160
- ```
161
-
162
- ### Using Existing Helpers
163
-
164
- You probably want to make use of Rails helpers and those defined in your application. Use the `helpers` or `h` method proxy:
165
-
166
- ```ruby
167
- class ArticleDecorator < Draper::Decorator
168
- def published_at
169
- date = h.content_tag(:span, article.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
170
- time = h.content_tag(:span, article.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
171
- h.content_tag :span, date + time, :class => 'created_at'
172
- end
173
- end
174
- ```
175
-
176
- #### Lazy Helpers
177
-
178
- Hate seeing that `h.` proxy all over? Willing to mix a bazillion methods into your decorator? Then try lazy helpers:
179
-
180
- ```ruby
181
- class ArticleDecorator < Draper::Decorator
182
- include Draper::LazyHelpers
183
-
184
- def published_at
185
- date = content_tag(:span, article.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
186
- time = content_tag(:span, article.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
187
- content_tag :span, date + time, :class => 'created_at'
188
- end
189
- end
190
- ```
191
-
192
- ### In the Controller
193
-
194
- When writing your controller actions, you have three options:
195
-
196
- * Call `.new` and pass in the object to be wrapped
197
-
198
- ```ruby
199
- ArticleDecorator.new(Article.find(params[:id]))
200
- ```
201
-
202
- * Call `.decorate` and pass in an object or collection of objects to be wrapped:
203
-
204
- ```ruby
205
- ArticleDecorator.decorate(Article.first) # Returns one instance of ArticleDecorator
206
- ArticleDecorator.decorate_collection(Article.all) # Returns CollectionDecorator of ArticleDecorator
207
- ```
208
-
209
- ### In Your Views
210
-
211
- Use the new methods in your views like any other model method (ex: `@article.published_at`):
212
-
213
- ```erb
214
- <h1><%= @article.title %> <%= @article.published_at %></h1>
215
- ```
216
-
217
- ### Integration with RSpec
218
-
219
- Using the provided generator, Draper will place specs for your new decorator in `spec/decorators/`.
220
-
221
- By default, specs in `spec/decorators` will be tagged as `type => :decorator`. Any spec tagged as `decorator` will make helpers available to the decorator.
222
-
223
- If your decorator specs live somewhere else, which they shouldn't, make sure to tag them with `type => :decorator`. If you don't tag them, Draper's helpers won't be available to your decorator while testing.
224
-
225
- Note: If you're using Spork, you need to `require 'draper/test/rspec_integration'` in your Spork.prefork block.
226
-
227
- ## Possible Decoration Methods
228
-
229
- Here are some ideas of what you might do in decorator methods:
230
-
231
- * Implement output formatting for `to_csv`, `to_json`, or `to_xml`
232
- * Format dates and times using `strftime`
233
- * Implement a commonly used representation of the data object like a `.name` method that combines `first_name` and `last_name` attributes
234
-
235
- ## Learning Resources
236
-
237
- ### RailsCast
238
-
239
- Ryan Bates has put together an excellent RailsCast on Draper based on the 0.8.0 release: http://railscasts.com/episodes/286-draper
240
-
241
- ### Example Using a Decorator
242
-
243
- For a brief tutorial with sample project, check this out: http://tutorials.jumpstartlab.com/topics/decorators.html
244
-
245
- 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:
246
-
247
- ```html
248
- <span class='published_at'>
249
- <span class='date'>Monday, May 6</span>
250
- <span class='time'>8:52AM</span>
251
- </span>
252
- ```
253
-
254
- 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.
255
-
256
- First, follow the steps above to add the dependency and update your bundle.
257
-
258
- Since we're talking about the `Article` model we'll create an `ArticleDecorator` class. You could do it by hand, but use the provided generator:
259
-
260
- ```
261
- rails generate decorator Article
262
- ```
263
-
264
- Now open up the created `app/decorators/article_decorator.rb` and you'll find an `ArticleDecorator` class. Add this method:
265
-
266
- ```ruby
267
- def published_at
268
- date = h.content_tag(:span, article.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
269
- time = h.content_tag(:span, article.published_at.strftime("%l:%M%p").delete(" "), :class => 'time')
270
- h.content_tag :span, date + time, :class => 'published_at'
271
- end
272
- ```
273
-
274
- Then you need to perform the wrapping in your controller. Here's the simplest method:
275
-
276
- ```ruby
277
- class ArticlesController < ApplicationController
278
- def show
279
- @article = ArticleDecorator.new(Article.find(params[:id]))
280
- end
281
- end
282
- ```
283
-
284
- Then within your views you can utilize both the normal data methods and your new presentation methods:
285
-
286
- ```ruby
287
- <%= @article.published_at %>
288
- ```
289
-
290
- Ta-da! Object-oriented data formatting for your view layer. Below is the complete decorator with extra comments removed:
291
-
292
- ```ruby
293
- class ArticleDecorator < Draper::Decorator
294
- def published_at
295
- date = h.content_tag(:span, article.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
296
- time = h.content_tag(:span, article.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
297
- h.content_tag :span, date + time, :class => 'published_at'
298
- end
299
- end
300
- ```
301
-
302
- ### Example of Decorated Associations
303
-
304
- Add a `decorates_association :association_name` to gain access to a decorated version of your target association.
305
-
306
- ```ruby
307
- class ArticleDecorator < Draper::Decorator
308
- decorates_association :author # belongs_to :author association
309
- end
310
-
311
- class AuthorDecorator < Draper::Decorator
312
- def fancy_name
313
- "#{model.title}. #{model.first_name} #{model.middle_name[0]}. #{model.last_name}"
314
- end
315
- end
316
- ```
317
-
318
- Now when you call the association it will use a decorator.
319
-
320
- ```ruby
321
- <%= @article.author.fancy_name %>
322
- ```
323
-
324
- ### A note on Rails configuration
325
-
326
- Draper loads your application's decorators when Rails start. This may lead to an issue with configuring I18n, whereby the settings you provide in `./config/application.rb` are ignored. This happens when you use the `I18n` constant at the class-level in one of the models that have a decorator, as in the following example:
327
-
328
- ```ruby
329
- # app/models/user.rb
330
- class User < ActiveRecord::Base
331
- validates :email, presence: { message: I18n.t('invalid_email') }
332
- end
333
-
334
- # app/decorators/user_decorator.rb
335
- class UserDecorator < ApplicationDecorator
336
- end
337
- ```
338
-
339
- Note how the `validates` line is executed when the `User` class is loaded, triggering the initialization of the I18n framework _before_ Rails had a chance to apply its configuration.
340
-
341
- Using `I18n` directly in your model definition **is an antipattern**. The preferred solution would be to not override the `message` option in your `validates` macro, but provide the `activerecord.errors.models.attributes.user.email.presence` key in your translation files.
342
-
343
- ## Extension gems
344
-
345
- For automatic decoration, check out [decorates_before_rendering](https://github.com/ohwillie/decorates_before_rendering).
346
-
347
- ## Contributing
348
-
349
- 1. Fork it.
350
- 2. Create a branch (`git checkout -b my_awesome_branch`)
351
- 3. Commit your changes (`git commit -am "Added some magic"`)
352
- 4. Push to the branch (`git push origin my_awesome_branch`)
353
- 5. Send pull request
354
-
355
- ## Running tests
356
-
357
- If it's the first time you want to run the tests, start by creating the dummy database:
358
-
359
- ```
360
- $ rake db:migrate
361
- ```
362
-
363
- You can then run tests by executing `rake`.
364
-
365
- ## Contributors
366
-
367
- Draper was conceived by Jeff Casimir and heavily refined by Steve Klabnik and a great community of open source contributors.
368
-
369
- ### Core Team
370
-
371
- * Jeff Casimir (jeff@jumpstartlab.com)
372
- * Steve Klabnik (steve@jumpstartlab.com)
373
- * Vasiliy Ermolovich
374
-
375
- ## License
376
-
377
- (The MIT License)
378
-
379
- 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:
380
-
381
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
382
-
383
- 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.