draper_new 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +230 -0
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +16 -0
- data/Guardfile +26 -0
- data/LICENSE +7 -0
- data/README.md +587 -0
- data/Rakefile +69 -0
- data/draper_new.gemspec +31 -0
- data/gemfiles/4.0.gemfile +3 -0
- data/gemfiles/4.1.gemfile +3 -0
- data/gemfiles/4.2.6.gemfile +3 -0
- data/gemfiles/4.2.gemfile +3 -0
- data/lib/draper.rb +63 -0
- data/lib/draper/automatic_delegation.rb +56 -0
- data/lib/draper/collection_decorator.rb +100 -0
- data/lib/draper/decoratable.rb +96 -0
- data/lib/draper/decoratable/equality.rb +26 -0
- data/lib/draper/decorated_association.rb +35 -0
- data/lib/draper/decorates_assigned.rb +44 -0
- data/lib/draper/decorator.rb +293 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/factory.rb +91 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +44 -0
- data/lib/draper/helper_support.rb +5 -0
- data/lib/draper/lazy_helpers.rb +15 -0
- data/lib/draper/railtie.rb +70 -0
- data/lib/draper/tasks/test.rake +22 -0
- data/lib/draper/test/devise_helper.rb +30 -0
- data/lib/draper/test/minitest_integration.rb +6 -0
- data/lib/draper/test/rspec_integration.rb +20 -0
- data/lib/draper/test_case.rb +42 -0
- data/lib/draper/undecorate.rb +9 -0
- data/lib/draper/version.rb +3 -0
- data/lib/draper/view_context.rb +104 -0
- data/lib/draper/view_context/build_strategy.rb +48 -0
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/generators/controller_override.rb +17 -0
- data/lib/generators/mini_test/decorator_generator.rb +20 -0
- data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
- data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
- data/lib/generators/rails/decorator_generator.rb +36 -0
- data/lib/generators/rails/templates/decorator.rb +19 -0
- data/lib/generators/rspec/decorator_generator.rb +9 -0
- data/lib/generators/rspec/templates/decorator_spec.rb +4 -0
- data/lib/generators/test_unit/decorator_generator.rb +9 -0
- data/lib/generators/test_unit/templates/decorator_test.rb +4 -0
- data/spec/draper/collection_decorator_spec.rb +307 -0
- data/spec/draper/decoratable/equality_spec.rb +10 -0
- data/spec/draper/decoratable_spec.rb +202 -0
- data/spec/draper/decorated_association_spec.rb +84 -0
- data/spec/draper/decorates_assigned_spec.rb +71 -0
- data/spec/draper/decorator_spec.rb +816 -0
- data/spec/draper/factory_spec.rb +251 -0
- data/spec/draper/finders_spec.rb +166 -0
- data/spec/draper/helper_proxy_spec.rb +61 -0
- data/spec/draper/lazy_helpers_spec.rb +21 -0
- data/spec/draper/undecorate_spec.rb +19 -0
- data/spec/draper/view_context/build_strategy_spec.rb +116 -0
- data/spec/draper/view_context_spec.rb +154 -0
- data/spec/draper/view_helpers_spec.rb +8 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/localized_urls.rb +5 -0
- data/spec/dummy/app/controllers/posts_controller.rb +20 -0
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +4 -0
- data/spec/dummy/app/decorators/post_decorator.rb +60 -0
- data/spec/dummy/app/helpers/application_helper.rb +5 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -0
- data/spec/dummy/app/mailers/post_mailer.rb +19 -0
- data/spec/dummy/app/models/admin.rb +5 -0
- data/spec/dummy/app/models/mongoid_post.rb +5 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
- data/spec/dummy/app/views/posts/_post.html.erb +40 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +71 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +33 -0
- data/spec/dummy/config/environments/production.rb +57 -0
- data/spec/dummy/config/environments/test.rb +31 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/mongoid.yml +79 -0
- data/spec/dummy/config/routes.rb +9 -0
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
- data/spec/dummy/db/schema.rb +21 -0
- data/spec/dummy/db/seeds.rb +2 -0
- data/spec/dummy/fast_spec/post_decorator_spec.rb +37 -0
- data/spec/dummy/lib/tasks/test.rake +16 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +16 -0
- data/spec/dummy/spec/decorators/devise_spec.rb +64 -0
- data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +66 -0
- data/spec/dummy/spec/decorators/spec_type_spec.rb +7 -0
- data/spec/dummy/spec/decorators/view_context_spec.rb +22 -0
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +33 -0
- data/spec/dummy/spec/models/mongoid_post_spec.rb +8 -0
- data/spec/dummy/spec/models/post_spec.rb +6 -0
- data/spec/dummy/spec/shared_examples/decoratable.rb +24 -0
- data/spec/dummy/spec/spec_helper.rb +8 -0
- data/spec/dummy/test/decorators/minitest/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/minitest/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/minitest/spec_type_test.rb +52 -0
- data/spec/dummy/test/decorators/minitest/view_context_test.rb +24 -0
- data/spec/dummy/test/decorators/test_unit/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/test_unit/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/test_unit/view_context_test.rb +24 -0
- data/spec/dummy/test/minitest_helper.rb +2 -0
- data/spec/dummy/test/test_helper.rb +3 -0
- data/spec/generators/controller/controller_generator_spec.rb +22 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +92 -0
- data/spec/integration/integration_spec.rb +66 -0
- data/spec/performance/active_record.rb +4 -0
- data/spec/performance/benchmark.rb +55 -0
- data/spec/performance/decorators.rb +45 -0
- data/spec/performance/models.rb +20 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/dummy_app.rb +85 -0
- data/spec/support/matchers/have_text.rb +50 -0
- data/spec/support/shared_examples/decoratable_equality.rb +40 -0
- data/spec/support/shared_examples/view_helpers.rb +39 -0
- metadata +420 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8ba8f592d552b09dce1bea6e3d1248b9b436468f
|
4
|
+
data.tar.gz: 20a001a27b9e3e1c4abbe87b4ef26c1d38758d4e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70c09369a5e7f0c89a01f88e03847f46e8c02f2b86f0deca2fe83dd7384ae6324b06a7e41214bb24d9fc563bbeb5f5582cff3a70626dd84b9ff49f41b64f6fde
|
7
|
+
data.tar.gz: b00d4f3119f372cb6a919f131d462dc39221091662362d1374a8246a6ca7f76fa648a7d6aee4f0a0d748eece52f743f93fc967802525ca283ba77a0efd12212e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
yardoc 'lib/draper/**/*.rb' -m markdown --no-private
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# Draper Changelog
|
2
|
+
|
3
|
+
## 2.1.0 - 2015-03-26
|
4
|
+
|
5
|
+
* Cleared most issues and merged a few PRs
|
6
|
+
* Improved behavior when decorating structs
|
7
|
+
* Improved how equality is handled
|
8
|
+
* Minor improvements to the README
|
9
|
+
|
10
|
+
## 2.0.0 - 2015-03-26
|
11
|
+
|
12
|
+
Working to breathe new life into the project by shedding baggage.
|
13
|
+
|
14
|
+
* Rails 3.2 support dropped
|
15
|
+
* Ruby 1.9.3 and 2.0 support dropped
|
16
|
+
* Add support for Rails 4.2 and Ruby 2.2
|
17
|
+
* Rewrite tests to get over RSpec deprecations
|
18
|
+
* Get RSpec up to 3.2
|
19
|
+
* Try to un-screw the challenges of ActiveModelSerializers
|
20
|
+
|
21
|
+
Due to the breakages of dropping legacy support, we've bumped the major version. From here,
|
22
|
+
development effort will likely focus on a version 3.0 that removes features and simplifies
|
23
|
+
usage of the library.
|
24
|
+
|
25
|
+
## 1.3.0 - 2013-10-26
|
26
|
+
|
27
|
+
[30 commits by 11 authors](https://github.com/drapergem/draper/compare/v1.2.1...v1.3.0)
|
28
|
+
|
29
|
+
* [Add `decorator_class?` method](https://github.com/drapergem/draper/commit/53e1df5c3ee169144a2778c6d2ee13cc6af99429)
|
30
|
+
|
31
|
+
* [Clear ViewContext before specs instead of after](https://github.com/drapergem/draper/pull/547)
|
32
|
+
|
33
|
+
* [Add alias method `#{model_name}` to the decorated object ](https://github.com/drapergem/draper/commit/f5f27243c3f0c37bff4872e1e78521e570ff1e4f)
|
34
|
+
|
35
|
+
* [Hook into `controller_generator` and `scaffold_controller_generator` instead of resource generator](https://github.com/drapergem/draper/commit/cd4f298987c3b4ad8046563ad4a087055fc7efe2)
|
36
|
+
|
37
|
+
* [Delegate `to_s` method to the object](https://github.com/drapergem/draper/commit/58b8181050c2a9a86f54660e7bb6bfefa5fd0b64)
|
38
|
+
|
39
|
+
## 1.2.1 - 2013-05-05
|
40
|
+
|
41
|
+
[28 commits by 4 authors](https://github.com/drapergem/draper/compare/v1.2.0...v1.2.1)
|
42
|
+
|
43
|
+
* [Document stubbing route helpers](https://github.com/drapergem/draper/commit/dbe8a81ca7d4d9ae87b4b62926a0ba6379397fbc)
|
44
|
+
|
45
|
+
* [Rename `source` to `object`. `source` still works, but will be deprecated in a future release.](https://github.com/drapergem/draper/commit/4b933ef39d252ecfe93c573a072633be545c49fb)
|
46
|
+
|
47
|
+
Various bugfixes, as always.
|
48
|
+
|
49
|
+
## 1.2.0 - 2013-04-01
|
50
|
+
|
51
|
+
[78 commits by 14 authors](https://github.com/drapergem/draper/compare/v1.1.0...v1.2.0)
|
52
|
+
|
53
|
+
* [Added license to the gemspec](https://github.com/drapergem/draper/commit/731fa85f7744a12da1364a3aa94cdf6994efa9e2)
|
54
|
+
|
55
|
+
* [Improvements in serialization](https://github.com/drapergem/draper/pull/448)
|
56
|
+
|
57
|
+
* [Fix support for Guard in development](https://github.com/drapergem/draper/commit/93c95200277fd3922e30e74c7a7e05563747e896)
|
58
|
+
|
59
|
+
* [Improved support for #capture](https://github.com/drapergem/draper/commit/efb934a6f59b9d8afe4a7fe29e9a94aae983b05c)
|
60
|
+
|
61
|
+
* [CollectionDecorator now has #decorated?](https://github.com/drapergem/draper/commit/65e3c4e4573173b510440b7e80f95a87109a1d15)
|
62
|
+
|
63
|
+
* [Added #respond_to_missing?](https://github.com/drapergem/draper/commit/11bb59bcf89f8e0be4ba2851eb78634caf783ecd)
|
64
|
+
|
65
|
+
* [Add proper STI support](https://github.com/drapergem/draper/commit/7802d97446cf6ea130a66c2781f5a7a087d28c0a)
|
66
|
+
|
67
|
+
* [Added before_remove_const callback for ActiveSupport](https://github.com/drapergem/draper/commit/9efda27c6a4f8680df4fad8f67ecb58e3f91703f)
|
68
|
+
|
69
|
+
* [Accept a lambda for context](https://github.com/drapergem/draper/commit/3ab3b35e875b8b2bd99a57e4add388313e765230)
|
70
|
+
|
71
|
+
* [Start testing against Ruby 2.0.0](https://github.com/drapergem/draper/commit/dbd1cbceedb0ddb3f060196cd31eb967db8a370b)
|
72
|
+
|
73
|
+
* [Fix our use of #scoped per Rails 4 deprecation](https://github.com/haines/draper/commit/47ee29c3b739eee3fc28a561432ad2363c14813c)
|
74
|
+
|
75
|
+
* [Properly swallow NameErrors](https://github.com/haines/draper/commit/09ad84fb712a30dd4302b9daa03d11281ac7d169)
|
76
|
+
|
77
|
+
* [Delegate CollectionDecorator#kind_of? to the underlying collection](https://github.com/drapergem/draper/commit/d5975f2c5b810306a96d9485fd39d550896dc2a1)
|
78
|
+
|
79
|
+
* [Make CollectionDecorators respond to #decorated_with?](https://github.com/drapergem/draper/commit/dc430b3e82de0d9cae86f7297f816e5b69d9ca58)
|
80
|
+
|
81
|
+
* [Avoid using #extend in Decorator#== for performance reasons](https://github.com/drapergem/draper/commit/205a0d43b4141f7b1756fe2b44b877545eb37517)
|
82
|
+
|
83
|
+
## 1.1.0 - 2013-01-28
|
84
|
+
|
85
|
+
[44 commits by 6 authors](https://github.com/drapergem/draper/compare/v1.0.0...v1.1.0)
|
86
|
+
|
87
|
+
* README improvements.
|
88
|
+
* Rails 4 compatibility.
|
89
|
+
[b2401c7](https://github.com/drapergem/draper/commit/b2401c71e092470e3b912b5da475115c22b55734)
|
90
|
+
* Added support for testing decorators without loading `ApplicationController`.
|
91
|
+
See the [README](https://github.com/drapergem/draper/blob/v1.1.0/README.md#isolated-tests) for details.
|
92
|
+
[4d0181f](https://github.com/drapergem/draper/commit/4d0181fb9c65dc769b05ed19bfcec2119d6e88f7)
|
93
|
+
* Improved the `==` method to check for `decorated?` before attempting to
|
94
|
+
compare with `source`.
|
95
|
+
[6c31617](https://github.com/drapergem/draper/commit/6c316176f5039a5861491fbcaa81f64ac4b36768)
|
96
|
+
* Changed the way helpers are accessed, so that helper methods may be stubbed
|
97
|
+
in tests.
|
98
|
+
[7a04619](https://github.com/drapergem/draper/commit/7a04619a06f832801bd4aedaaf5985d6e3e5e1af)
|
99
|
+
* Made the Devise test helper `sign_in` method independent of mocking
|
100
|
+
framework.
|
101
|
+
[b0902ab](https://github.com/drapergem/draper/commit/b0902ab0fe01916b7fddb0a3d97aa0c7cca09482)
|
102
|
+
* Stopped attempting to call `to_a` on decorated objects (a now-redundant lazy
|
103
|
+
query workaround).
|
104
|
+
[34c6390](https://github.com/drapergem/draper/commit/34c6390583f7fc7704d04e38bc974b65fc92517c)
|
105
|
+
* Fixed a minor bug where view contexts could be accidentally shared between
|
106
|
+
tests.
|
107
|
+
[3d07cb3](https://github.com/drapergem/draper/commit/3d07cb387b1cae6f97897dfb85512e30f5e888e9)
|
108
|
+
* Improved helper method performance.
|
109
|
+
[e6f88a5](https://github.com/drapergem/draper/commit/e6f88a5e7dada3f9db480e13e16d1acc964ba098)
|
110
|
+
* Our specs now use the new RSpec `expect( ).to` syntax.
|
111
|
+
[9a3b319](https://github.com/drapergem/draper/commit/9a3b319d6d54cd78fb2654a94bbe893e36359754)
|
112
|
+
|
113
|
+
## 1.0.0 - 2013-01-14
|
114
|
+
|
115
|
+
[249 commits by 19 authors](https://github.com/drapergem/draper/compare/v0.18.0...v1.0.0)
|
116
|
+
|
117
|
+
Major changes are described [in the upgrade guide](https://github.com/drapergem/draper/wiki/Upgrading-to-1.0).
|
118
|
+
|
119
|
+
* Infer collection decorators.
|
120
|
+
[e8253df](https://github.com/drapergem/draper/commit/e8253df7dc6c90a542444c0f4ef289909fce4f90)
|
121
|
+
* Prevent calls to `scoped` on decorated associations.
|
122
|
+
[5dcc6c3](https://github.com/drapergem/draper/commit/5dcc6c31ecf408753158d15fed9fb23fbfdc3734)
|
123
|
+
* Add `helper` method to tests.
|
124
|
+
[551961e](https://github.com/drapergem/draper/commit/551961e72ee92355bc9c848bedfcc573856d12b0)
|
125
|
+
* Inherit method security.
|
126
|
+
[1865ed3](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
|
127
|
+
* Test against all versions of Rails 3.
|
128
|
+
[1865ed3](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
|
129
|
+
* Pretend to be `instance_of?(source.class)`.
|
130
|
+
[30d209f](https://github.com/drapergem/draper/commit/30d209f990847e84b221ac798e84b976f5775cc0)
|
131
|
+
* Remove security from `Decorator`. Do manual delegation with `:delegate`.
|
132
|
+
[c6f8aaa](https://github.com/drapergem/draper/commit/c6f8aaa2b2bd4679738050aede2503aa8e9db130)
|
133
|
+
* Add generators for MiniTest.
|
134
|
+
[1fac02b](https://github.com/drapergem/draper/commit/1fac02b65b15e32f06e8292cb858c97cb1c1da2c)
|
135
|
+
* Test against edge rails.
|
136
|
+
[e9b71e3](https://github.com/drapergem/draper/commit/e9b71e3cf55a800b48c083ff257a7c1cbe1b601b)
|
137
|
+
|
138
|
+
### 1.0.0.beta6 - 2012-12-31
|
139
|
+
|
140
|
+
* Fix up README to include changes made.
|
141
|
+
[5e6e4d1](https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6)
|
142
|
+
* `CollectionDecorator` no longer freezes its collection: direct access is
|
143
|
+
discouraged by making access private.
|
144
|
+
[c6d60e6](https://github.com/drapergem/draper/commit/c6d60e6577ed396385f3f1151c3f188fe47e9a57)
|
145
|
+
* A fix for `Decoratable#==`.
|
146
|
+
[e4fa239](https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65)
|
147
|
+
* Ensure we coerce to an array in the right place.
|
148
|
+
[9eb9fc9](https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095)
|
149
|
+
|
150
|
+
### 1.0.0.beta5 - 2012-12-27
|
151
|
+
|
152
|
+
* Change CollectionDecorator to freeze its collection.
|
153
|
+
[04d7796](https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4)
|
154
|
+
* Bugfix on `CollectionDecorator#to_s`.
|
155
|
+
[eefd7d0](https://github.com/drapergem/draper/commit/eefd7d09cac97d531b9235246378c3746d153f08)
|
156
|
+
* Upgrade `request_store` dependency to take advantage of a bugfix.
|
157
|
+
[9f17212](https://github.com/drapergem/draper/commit/9f17212fd1fb656ef1314327d60fe45e0acf60a2)
|
158
|
+
|
159
|
+
### 1.0.0.beta4 - 2012-12-18
|
160
|
+
|
161
|
+
* Fixed a race condition with capybara integration.
|
162
|
+
[e794649](https://github.com/drapergem/draper/commit/e79464931e7b98c85ed5d78ed9ca38d51f43006e)
|
163
|
+
* `[]` can be decorated again.
|
164
|
+
[597fbdf](https://github.com/drapergem/draper/commit/597fbdf0c80583f5ea6df9f7350fefeaa0cca989)
|
165
|
+
* `model == decorator` as well as `decorator == model`.
|
166
|
+
[46f8a68](https://github.com/drapergem/draper/commit/46f8a6823c50c13e5c9ab3c07723f335c4e291bc)
|
167
|
+
* Preliminary Mongoid integration.
|
168
|
+
[892d195](https://github.com/drapergem/draper/commit/892d1954202c61fd082a07213c8d4a23560687bc)
|
169
|
+
* Add a helper method `sign_in` for devise in decorator specs.
|
170
|
+
[66a3009](https://github.com/drapergem/draper/commit/66a30093ed4207d02d8fa60bda4df2da091d85a3)
|
171
|
+
* Brought back `context`.
|
172
|
+
[9609156](https://github.com/drapergem/draper/commit/9609156b997b3a469386eef3a5f043b24d8a2fba)
|
173
|
+
* Fixed issue where classes were incorrectly being looked up.
|
174
|
+
[ee2a015](https://github.com/drapergem/draper/commit/ee2a015514ff87dfd2158926457e988c2fc3fd79)
|
175
|
+
* Integrate RequestStore for per-request storage.
|
176
|
+
[fde1cde](https://github.com/drapergem/draper/commit/fde1cde9adfb856750c1f616d8b62d221ef97fc6)
|
177
|
+
|
178
|
+
### 1.0.0.beta3 - 2012-12-03
|
179
|
+
|
180
|
+
* Relaxed Rails version requirement to 3.0. Support for < 3.2 should be
|
181
|
+
considered experimental. Please file bug reports.
|
182
|
+
|
183
|
+
### 1.0.0.beta2 - 2012-12-03
|
184
|
+
|
185
|
+
* `has_finders` is now `decorates_finders`.
|
186
|
+
[33f18aa](https://github.com/drapergem/draper/commit/33f18aa062e0d3848443dbd81047f20d5665579f)
|
187
|
+
* If a finder method is used, and the source class is not set and cannot be
|
188
|
+
inferred, an `UninferrableSourceError` is raised.
|
189
|
+
[8ef5bf2](https://github.com/drapergem/draper/commit/8ef5bf2f02f7033e3cd4f1f5de7397b02c984fe3)
|
190
|
+
* Class methods are now properly delegated again.
|
191
|
+
[731995a](https://github.com/drapergem/draper/commit/731995a5feac4cd06cf9328d2892c0eca9992db6)
|
192
|
+
* We no longer `respond_to?` private methods on the source.
|
193
|
+
[18ebac8](https://github.com/drapergem/draper/commit/18ebac81533a6413aa20a3c26f23e91d0b12b031)
|
194
|
+
* Rails versioning relaxed to support Rails 4.
|
195
|
+
[8bfd393](https://github.com/drapergem/draper/commit/8bfd393b5baa7aa1488076a5e2cb88648efaa815)
|
196
|
+
|
197
|
+
### 1.0.0.beta1 - 2012-11-30
|
198
|
+
|
199
|
+
* Renaming `Draper::Base` to `Draper::Decorator`. This is the most significant
|
200
|
+
change you'll need to upgrade your application.
|
201
|
+
[025742c](https://github.com/drapergem/draper/commit/025742cb3b295d259cf0ecf3669c24817d6f2df1)
|
202
|
+
* Added an internal Rails application for integration tests. This won't affect
|
203
|
+
your application, but we're now running a set of Cucumber tests inside of a
|
204
|
+
Rails app in both development and production mode to help ensure that we
|
205
|
+
don't make changes that break Draper.
|
206
|
+
[90a4859](https://github.com/drapergem/draper/commit/90a4859085cab158658d23d77cd3108b6037e36f)
|
207
|
+
* Add `#decorated?` method. This gives us a free RSpec matcher,
|
208
|
+
`be_decorated`.
|
209
|
+
[834a6fd](https://github.com/drapergem/draper/commit/834a6fd1f24b5646c333a04a99fe9846a58965d6)
|
210
|
+
* `#decorates` is no longer needed inside your models, and should be removed.
|
211
|
+
Decorators automatically infer the class they decorate.
|
212
|
+
[e1214d9](https://github.com/drapergem/draper/commit/e1214d97b62f2cab45227cc650029734160dcdfe)
|
213
|
+
* Decorators do not automatically come with 'finders' by default. If you'd like
|
214
|
+
to use `SomeDecorator.find(1)`, for example, simply add `#has_finders` to
|
215
|
+
the decorator to include them.
|
216
|
+
[42b6f78](https://github.com/drapergem/draper/commit/42b6f78fda4f51845dab4d35da68880f1989d178)
|
217
|
+
* To refer to the object being decorated, `#source` is now the preferred
|
218
|
+
method.
|
219
|
+
[1e84fcb](https://github.com/drapergem/draper/commit/1e84fcb4a0eab0d12f5feda6886ce1caa239cb16)
|
220
|
+
* `ActiveModel::Serialization` is included in Decorators if you've requred
|
221
|
+
`ActiveModel::Serializers`, so that decorators can be serialized.
|
222
|
+
[c4b3527](https://github.com/drapergem/draper/commit/c4b352799067506849abcbf14963ea36abda301c)
|
223
|
+
* Properly support Test::Unit.
|
224
|
+
[087e134](https://github.com/drapergem/draper/commit/087e134ed0885ec11325ffabe8ab2bebef77a33a)
|
225
|
+
|
226
|
+
And many small bug fixes and refactorings.
|
227
|
+
|
228
|
+
## 0.x
|
229
|
+
|
230
|
+
See changes prior to version 1.0 [here](https://github.com/drapergem/draper/blob/16140fed55f57d18f8b10a0789dd1fa5b3115a8d/CHANGELOG.markdown).
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Contributing to Draper
|
2
|
+
===================
|
3
|
+
|
4
|
+
First of all, **thank you** for wanting to help and reading this!
|
5
|
+
|
6
|
+
If you have found a problem with Draper, please [check to see](https://github.com/drapergem/draper/issues) if there's already an issue and if there's not, [create a new report](https://github.com/drapergem/draper/issues/new). Please include your versions of Draper and Rails (and any other gems that are relevant to your issue, e.g. RSpec if you're having trouble in your tests).
|
7
|
+
|
8
|
+
## Sending a pull request
|
9
|
+
|
10
|
+
Thanks again! Here's a quick how-to:
|
11
|
+
|
12
|
+
1. [Fork the project](https://help.github.com/articles/fork-a-repo).
|
13
|
+
2. Create a branch - `git checkout -b adding_magic`
|
14
|
+
3. Make your changes, and add some tests!
|
15
|
+
4. Check that the tests pass - `bundle exec rake`
|
16
|
+
5. Commit your changes - `git commit -am "Added some magic"`
|
17
|
+
6. Push the branch to Github - `git push origin adding_magic`
|
18
|
+
7. Send us a [pull request](https://help.github.com/articles/using-pull-requests)!
|
19
|
+
|
20
|
+
:heart: :sparkling_heart: :heart:
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
platforms :ruby do
|
6
|
+
gem "sqlite3"
|
7
|
+
end
|
8
|
+
|
9
|
+
platforms :jruby do
|
10
|
+
gem "minitest"
|
11
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
12
|
+
end
|
13
|
+
|
14
|
+
version = ENV["RAILS_VERSION"] || "4.1"
|
15
|
+
|
16
|
+
eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
|
data/Guardfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
def rspec_guard(options = {}, &block)
|
2
|
+
options = {
|
3
|
+
:version => 2,
|
4
|
+
:notification => false
|
5
|
+
}.merge(options)
|
6
|
+
|
7
|
+
guard 'rspec', options, &block
|
8
|
+
end
|
9
|
+
|
10
|
+
rspec_guard :spec_paths => %w{spec/draper spec/generators} do
|
11
|
+
watch(%r{^spec/.+_spec\.rb$})
|
12
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
13
|
+
watch('spec/spec_helper.rb') { "spec" }
|
14
|
+
end
|
15
|
+
|
16
|
+
rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'development'} do
|
17
|
+
watch(%r{^spec/.+_spec\.rb$})
|
18
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
+
watch('spec/spec_helper.rb') { "spec" }
|
20
|
+
end
|
21
|
+
|
22
|
+
rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'production'} do
|
23
|
+
watch(%r{^spec/.+_spec\.rb$})
|
24
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
25
|
+
watch('spec/spec_helper.rb') { "spec" }
|
26
|
+
end
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,587 @@
|
|
1
|
+
# Draper: View Models for Rails
|
2
|
+
|
3
|
+
[![TravisCI Build Status](https://travis-ci.org/drapergem/draper.svg?branch=master)](http://travis-ci.org/drapergem/draper)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/drapergem/draper.png)](https://codeclimate.com/github/drapergem/draper)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/drapergem/draper.png?branch=master)](http://inch-ci.org/github/drapergem/draper)
|
6
|
+
|
7
|
+
Draper adds an object-oriented layer of presentation logic to your Rails
|
8
|
+
application.
|
9
|
+
|
10
|
+
Without Draper, this functionality might have been tangled up in procedural
|
11
|
+
helpers or adding bulk to your models. With Draper decorators, you can wrap your
|
12
|
+
models with presentation-related logic to organise - and test - this layer of
|
13
|
+
your app much more effectively.
|
14
|
+
|
15
|
+
## Why Use a Decorator?
|
16
|
+
|
17
|
+
Imagine your application has an `Article` model. With Draper, you'd create a
|
18
|
+
corresponding `ArticleDecorator`. The decorator wraps the model, and deals
|
19
|
+
*only* with presentational concerns. In the controller, you decorate the article
|
20
|
+
before handing it off to the view:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# app/controllers/articles_controller.rb
|
24
|
+
def show
|
25
|
+
@article = Article.find(params[:id]).decorate
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
In the view, you can use the decorator in exactly the same way as you would have
|
30
|
+
used the model. But whenever you start needing logic in the view or start
|
31
|
+
thinking about a helper method, you can implement a method on the decorator
|
32
|
+
instead.
|
33
|
+
|
34
|
+
Let's look at how you could convert an existing Rails helper to a decorator
|
35
|
+
method. You have this existing helper:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# app/helpers/articles_helper.rb
|
39
|
+
def publication_status(article)
|
40
|
+
if article.published?
|
41
|
+
"Published at #{article.published_at.strftime('%A, %B %e')}"
|
42
|
+
else
|
43
|
+
"Unpublished"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
But it makes you a little uncomfortable. `publication_status` lives in a
|
49
|
+
nebulous namespace spread across all controllers and view. Down the road, you
|
50
|
+
might want to display the publication status of a `Book`. And, of course, your
|
51
|
+
design calls for a slighly different formatting to the date for a `Book`.
|
52
|
+
|
53
|
+
Now your helper method can either switch based on the input class type (poor
|
54
|
+
Ruby style), or you break it out into two methods, `book_publication_status` and
|
55
|
+
`article_publication_status`. And keep adding methods for each publication
|
56
|
+
type...to the global helper namespace. And you'll have to remember all the names. Ick.
|
57
|
+
|
58
|
+
Ruby thrives when we use Object-Oriented style. If you didn't know Rails'
|
59
|
+
helpers existed, you'd probably imagine that your view template could feature
|
60
|
+
something like this:
|
61
|
+
|
62
|
+
```erb
|
63
|
+
<%= @article.publication_status %>
|
64
|
+
```
|
65
|
+
|
66
|
+
Without a decorator, you'd have to implement the `publication_status` method in
|
67
|
+
the `Article` model. That method is presentation-centric, and thus does not
|
68
|
+
belong in a model.
|
69
|
+
|
70
|
+
Instead, you implement a decorator:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# app/decorators/article_decorator.rb
|
74
|
+
class ArticleDecorator < Draper::Decorator
|
75
|
+
delegate_all
|
76
|
+
|
77
|
+
def publication_status
|
78
|
+
if published?
|
79
|
+
"Published at #{published_at}"
|
80
|
+
else
|
81
|
+
"Unpublished"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def published_at
|
86
|
+
object.published_at.strftime("%A, %B %e")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
Within the `publication_status` method we use the `published?` method. Where
|
92
|
+
does that come from? It's a method of the source `Article`, whose methods have
|
93
|
+
been made available on the decorator by the `delegate_all` call above.
|
94
|
+
|
95
|
+
You might have heard this sort of decorator called a "presenter", an "exhibit",
|
96
|
+
a "view model", or even just a "view" (in that nomenclature, what Rails calls
|
97
|
+
"views" are actually "templates"). Whatever you call it, it's a great way to
|
98
|
+
replace procedural helpers like the one above with "real" object-oriented
|
99
|
+
programming.
|
100
|
+
|
101
|
+
Decorators are the ideal place to:
|
102
|
+
* format complex data for user display
|
103
|
+
* define commonly-used representations of an object, like a `name` method that
|
104
|
+
combines `first_name` and `last_name` attributes
|
105
|
+
* mark up attributes with a little semantic HTML, like turning a `url` field
|
106
|
+
into a hyperlink
|
107
|
+
|
108
|
+
## Installation
|
109
|
+
|
110
|
+
Add Draper to your Gemfile:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
gem 'draper', '~> 1.3'
|
114
|
+
```
|
115
|
+
|
116
|
+
And run `bundle install` within your app's directory.
|
117
|
+
|
118
|
+
If you're upgrading from a 0.x release, the major changes are outlined [in the
|
119
|
+
wiki](https://github.com/drapergem/draper/wiki/Upgrading-to-1.0).
|
120
|
+
|
121
|
+
## Writing Decorators
|
122
|
+
|
123
|
+
Decorators inherit from `Draper::Decorator`, live in your `app/decorators`
|
124
|
+
directory, and are named for the model that they decorate:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
# app/decorators/article_decorator.rb
|
128
|
+
class ArticleDecorator < Draper::Decorator
|
129
|
+
# ...
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
### Generators
|
134
|
+
|
135
|
+
When you have Draper installed and generate a controller...
|
136
|
+
|
137
|
+
```
|
138
|
+
rails generate resource Article
|
139
|
+
```
|
140
|
+
|
141
|
+
...you'll get a decorator for free!
|
142
|
+
|
143
|
+
But if the `Article` model already exists, you can run...
|
144
|
+
|
145
|
+
```
|
146
|
+
rails generate decorator Article
|
147
|
+
```
|
148
|
+
|
149
|
+
...to create the `ArticleDecorator`.
|
150
|
+
|
151
|
+
### Accessing Helpers
|
152
|
+
|
153
|
+
Normal Rails helpers are still useful for lots of tasks. Both Rails' provided
|
154
|
+
helpers and those defined in your app can be accessed within a decorator via the `h` method:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class ArticleDecorator < Draper::Decorator
|
158
|
+
def emphatic
|
159
|
+
h.content_tag(:strong, "Awesome")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
If writing `h.` frequently is getting you down, you can add...
|
165
|
+
|
166
|
+
```
|
167
|
+
include Draper::LazyHelpers
|
168
|
+
```
|
169
|
+
|
170
|
+
...at the top of your decorator class - you'll mix in a bazillion methods and
|
171
|
+
never have to type `h.` again.
|
172
|
+
|
173
|
+
(*Note*: the `capture` method is only available through `h` or `helpers`)
|
174
|
+
|
175
|
+
### Accessing the model
|
176
|
+
|
177
|
+
When writing decorator methods you'll usually need to access the wrapped model.
|
178
|
+
While you may choose to use delegation ([covered below](#delegating-methods))
|
179
|
+
for convenience, you can always use the `object` (or its alias `model`):
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
class ArticleDecorator < Draper::Decorator
|
183
|
+
def published_at
|
184
|
+
object.published_at.strftime("%A, %B %e")
|
185
|
+
end
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
## Decorating Objects
|
190
|
+
|
191
|
+
### Single Objects
|
192
|
+
|
193
|
+
Ok, so you've written a sweet decorator, now you're going to want to put it into
|
194
|
+
action! A simple option is to call the `decorate` method on your model:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
@article = Article.first.decorate
|
198
|
+
```
|
199
|
+
|
200
|
+
This infers the decorator from the object being decorated. If you want more
|
201
|
+
control - say you want to decorate a `Widget` with a more general
|
202
|
+
`ProductDecorator` - then you can instantiate a decorator directly:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
@widget = ProductDecorator.new(Widget.first)
|
206
|
+
# or, equivalently
|
207
|
+
@widget = ProductDecorator.decorate(Widget.first)
|
208
|
+
```
|
209
|
+
|
210
|
+
### Collections
|
211
|
+
|
212
|
+
#### Decorating Individual Elements
|
213
|
+
|
214
|
+
If you have a collection of objects, you can decorate them all in one fell
|
215
|
+
swoop:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
@articles = ArticleDecorator.decorate_collection(Article.all)
|
219
|
+
```
|
220
|
+
|
221
|
+
If your collection is an ActiveRecord query, you can use this:
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
@articles = Article.popular.decorate
|
225
|
+
```
|
226
|
+
|
227
|
+
*Note:* In Rails 3, the `.all` method returns an array and not a query. Thus you
|
228
|
+
_cannot_ use the technique of `Article.all.decorate` in Rails 3. In Rails 4,
|
229
|
+
`.all` returns a query so this techique would work fine.
|
230
|
+
|
231
|
+
#### Decorating the Collection Itself
|
232
|
+
|
233
|
+
If you want to add methods to your decorated collection (for example, for
|
234
|
+
pagination), you can subclass `Draper::CollectionDecorator`:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
# app/decorators/articles_decorator.rb
|
238
|
+
class ArticlesDecorator < Draper::CollectionDecorator
|
239
|
+
def page_number
|
240
|
+
42
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# elsewhere...
|
245
|
+
@articles = ArticlesDecorator.new(Article.all)
|
246
|
+
# or, equivalently
|
247
|
+
@articles = ArticlesDecorator.decorate(Article.all)
|
248
|
+
```
|
249
|
+
|
250
|
+
Draper decorates each item by calling the `decorate` method. Alternatively, you can
|
251
|
+
specify a decorator by overriding the collection decorator's `decorator_class`
|
252
|
+
method, or by passing the `:with` option to the constructor.
|
253
|
+
|
254
|
+
#### Using pagination
|
255
|
+
|
256
|
+
Some pagination gems add methods to `ActiveRecord::Relation`. For example,
|
257
|
+
[Kaminari](https://github.com/amatsuda/kaminari)'s `paginate` helper method
|
258
|
+
requires the collection to implement `current_page`, `total_pages`, and
|
259
|
+
`limit_value`. To expose these on a collection decorator, you can delegate to
|
260
|
+
the `object`:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
class PaginatingDecorator < Draper::CollectionDecorator
|
264
|
+
delegate :current_page, :total_pages, :limit_value, :entry_name, :total_count, :offset_value, :last_page?
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
The `delegate` method used here is the same as that added by [Active
|
269
|
+
Support](http://api.rubyonrails.org/classes/Module.html#method-i-delegate),
|
270
|
+
except that the `:to` option is not required; it defaults to `:object` when
|
271
|
+
omitted.
|
272
|
+
|
273
|
+
[will_paginate](https://github.com/mislav/will_paginate) needs the following delegations:
|
274
|
+
|
275
|
+
```ruby
|
276
|
+
delegate :current_page, :per_page, :offset, :total_entries, :total_pages
|
277
|
+
```
|
278
|
+
|
279
|
+
### Decorating Associated Objects
|
280
|
+
|
281
|
+
You can automatically decorate associated models when the primary model is
|
282
|
+
decorated. Assuming an `Article` model has an associated `Author` object:
|
283
|
+
|
284
|
+
```ruby
|
285
|
+
class ArticleDecorator < Draper::Decorator
|
286
|
+
decorates_association :author
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
When `ArticleDecorator` decorates an `Article`, it will also use
|
291
|
+
`AuthorDecorator` to decorate the associated `Author`.
|
292
|
+
|
293
|
+
### Decorated Finders
|
294
|
+
|
295
|
+
You can call `decorates_finders` in a decorator...
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
class ArticleDecorator < Draper::Decorator
|
299
|
+
decorates_finders
|
300
|
+
end
|
301
|
+
```
|
302
|
+
|
303
|
+
...which allows you to then call all the normal ActiveRecord-style finders on
|
304
|
+
your `ArticleDecorator` and they'll return decorated objects:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
@article = ArticleDecorator.find(params[:id])
|
308
|
+
```
|
309
|
+
|
310
|
+
### When to Decorate Objects
|
311
|
+
|
312
|
+
Decorators are supposed to behave very much like the models they decorate, and
|
313
|
+
for that reason it is very tempting to just decorate your objects at the start
|
314
|
+
of your controller action and then use the decorators throughout. *Don't*.
|
315
|
+
|
316
|
+
Because decorators are designed to be consumed by the view, you should only be
|
317
|
+
accessing them there. Manipulate your models to get things ready, then decorate
|
318
|
+
at the last minute, right before you render the view. This avoids many of the
|
319
|
+
common pitfalls that arise from attempting to modify decorators (in particular,
|
320
|
+
collection decorators) after creating them.
|
321
|
+
|
322
|
+
To help you make your decorators read-only, we have the `decorates_assigned`
|
323
|
+
method in your controller. It adds a helper method that returns the decorated
|
324
|
+
version of an instance variable:
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
# app/controllers/articles_controller.rb
|
328
|
+
class ArticlesController < ApplicationController
|
329
|
+
decorates_assigned :article
|
330
|
+
|
331
|
+
def show
|
332
|
+
@article = Article.find(params[:id])
|
333
|
+
end
|
334
|
+
end
|
335
|
+
```
|
336
|
+
|
337
|
+
The `decorates_assigned :article` bit is roughly equivalent to
|
338
|
+
|
339
|
+
```ruby
|
340
|
+
def article
|
341
|
+
@decorated_article ||= @article.decorate
|
342
|
+
end
|
343
|
+
helper_method :article
|
344
|
+
```
|
345
|
+
|
346
|
+
This means that you can just replace `@article` with `article` in your views and
|
347
|
+
you'll have access to an ArticleDecorator object instead. In your controller you
|
348
|
+
can continue to use the `@article` instance variable to manipulate the model -
|
349
|
+
for example, `@article.comments.build` to add a new blank comment for a form.
|
350
|
+
|
351
|
+
## Testing
|
352
|
+
|
353
|
+
Draper supports RSpec, MiniTest::Rails, and Test::Unit, and will add the
|
354
|
+
appropriate tests when you generate a decorator.
|
355
|
+
|
356
|
+
### RSpec
|
357
|
+
|
358
|
+
Your specs are expected to live in `spec/decorators`. If you use a different
|
359
|
+
path, you need to tag them with `type: :decorator`.
|
360
|
+
|
361
|
+
In a controller spec, you might want to check whether your instance variables
|
362
|
+
are being decorated properly. You can use the handy predicate matchers:
|
363
|
+
|
364
|
+
```ruby
|
365
|
+
assigns(:article).should be_decorated
|
366
|
+
|
367
|
+
# or, if you want to be more specific
|
368
|
+
assigns(:article).should be_decorated_with ArticleDecorator
|
369
|
+
```
|
370
|
+
|
371
|
+
Note that `model.decorate == model`, so your existing specs shouldn't break when
|
372
|
+
you add the decoration.
|
373
|
+
|
374
|
+
#### Spork Users
|
375
|
+
|
376
|
+
In your `Spork.prefork` block of `spec_helper.rb`, add this:
|
377
|
+
|
378
|
+
```ruby
|
379
|
+
require 'draper/test/rspec_integration'
|
380
|
+
```
|
381
|
+
|
382
|
+
### Isolated Tests
|
383
|
+
|
384
|
+
In tests, Draper needs to build a view context to access helper methods. By
|
385
|
+
default, it will create an `ApplicationController` and then use its view
|
386
|
+
context. If you are speeding up your test suite by testing each component in
|
387
|
+
isolation, you can eliminate this dependency by putting the following in your
|
388
|
+
`spec_helper` or similar:
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
Draper::ViewContext.test_strategy :fast
|
392
|
+
```
|
393
|
+
|
394
|
+
In doing so, your decorators will no longer have access to your application's
|
395
|
+
helpers. If you need to selectively include such helpers, you can pass a block:
|
396
|
+
|
397
|
+
```ruby
|
398
|
+
Draper::ViewContext.test_strategy :fast do
|
399
|
+
include ApplicationHelper
|
400
|
+
end
|
401
|
+
```
|
402
|
+
|
403
|
+
#### Stubbing Route Helper Functions
|
404
|
+
|
405
|
+
If you are writing isolated tests for Draper methods that call route helper
|
406
|
+
methods, you can stub them instead of needing to require Rails.
|
407
|
+
|
408
|
+
If you are using RSpec, minitest-rails, or the Test::Unit syntax of minitest,
|
409
|
+
you already have access to the Draper `helpers` in your tests since they
|
410
|
+
inherit from `Draper::TestCase`. If you are using minitest's spec syntax
|
411
|
+
without minitest-rails, you can explicitly include the Draper `helpers`:
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
describe YourDecorator do
|
415
|
+
include Draper::ViewHelpers
|
416
|
+
end
|
417
|
+
```
|
418
|
+
|
419
|
+
Then you can stub the specific route helper functions you need using your
|
420
|
+
preferred stubbing technique (this example uses RSpec's `stub` method):
|
421
|
+
|
422
|
+
```ruby
|
423
|
+
helpers.stub(users_path: '/users')
|
424
|
+
```
|
425
|
+
|
426
|
+
## Advanced usage
|
427
|
+
|
428
|
+
### Shared Decorator Methods
|
429
|
+
|
430
|
+
You might have several decorators that share similar needs. Since decorators are
|
431
|
+
just Ruby objects, you can use any normal Ruby technique for sharing
|
432
|
+
functionality.
|
433
|
+
|
434
|
+
In Rails controllers, common functionality is organized by having all
|
435
|
+
controllers inherit from `ApplicationController`. You can apply this same
|
436
|
+
pattern to your decorators:
|
437
|
+
|
438
|
+
```ruby
|
439
|
+
# app/decorators/application_decorator.rb
|
440
|
+
class ApplicationDecorator < Draper::Decorator
|
441
|
+
# ...
|
442
|
+
end
|
443
|
+
```
|
444
|
+
|
445
|
+
Then modify your decorators to inherit from that `ApplicationDecorator` instead
|
446
|
+
of directly from `Draper::Decorator`:
|
447
|
+
|
448
|
+
```ruby
|
449
|
+
class ArticleDecorator < ApplicationDecorator
|
450
|
+
# decorator methods
|
451
|
+
end
|
452
|
+
```
|
453
|
+
|
454
|
+
### Delegating Methods
|
455
|
+
|
456
|
+
When your decorator calls `delegate_all`, any method called on the decorator not
|
457
|
+
defined in the decorator itself will be delegated to the decorated object. This
|
458
|
+
is a very permissive interface.
|
459
|
+
|
460
|
+
If you want to strictly control which methods are called within views, you can
|
461
|
+
choose to only delegate certain methods from the decorator to the source model:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
class ArticleDecorator < Draper::Decorator
|
465
|
+
delegate :title, :body
|
466
|
+
end
|
467
|
+
```
|
468
|
+
|
469
|
+
We omit the `:to` argument here as it defaults to the `object` being decorated.
|
470
|
+
You could choose to delegate methods to other places like this:
|
471
|
+
|
472
|
+
```ruby
|
473
|
+
class ArticleDecorator < Draper::Decorator
|
474
|
+
delegate :title, :body
|
475
|
+
delegate :name, :title, to: :author, prefix: true
|
476
|
+
end
|
477
|
+
```
|
478
|
+
|
479
|
+
From your view template, assuming `@article` is decorated, you could do any of
|
480
|
+
the following:
|
481
|
+
|
482
|
+
```ruby
|
483
|
+
@article.title # Returns the article's `.title`
|
484
|
+
@article.body # Returns the article's `.body`
|
485
|
+
@article.author_name # Returns the article's `author.name`
|
486
|
+
@article.author_title # Returns the article's `author.title`
|
487
|
+
```
|
488
|
+
|
489
|
+
### Adding Context
|
490
|
+
|
491
|
+
If you need to pass extra data to your decorators, you can use a `context` hash.
|
492
|
+
Methods that create decorators take it as an option, for example:
|
493
|
+
|
494
|
+
```ruby
|
495
|
+
Article.first.decorate(context: {role: :admin})
|
496
|
+
```
|
497
|
+
|
498
|
+
The value passed to the `:context` option is then available in the decorator
|
499
|
+
through the `context` method.
|
500
|
+
|
501
|
+
If you use `decorates_association`, the context of the parent decorator is
|
502
|
+
passed to the associated decorators. You can override this with the `:context`
|
503
|
+
option:
|
504
|
+
|
505
|
+
```ruby
|
506
|
+
class ArticleDecorator < Draper::Decorator
|
507
|
+
decorates_association :author, context: {foo: "bar"}
|
508
|
+
end
|
509
|
+
```
|
510
|
+
|
511
|
+
or, if you want to modify the parent's context, use a lambda that takes a hash
|
512
|
+
and returns a new hash:
|
513
|
+
|
514
|
+
```ruby
|
515
|
+
class ArticleDecorator < Draper::Decorator
|
516
|
+
decorates_association :author,
|
517
|
+
context: ->(parent_context){ parent_context.merge(foo: "bar") }
|
518
|
+
end
|
519
|
+
```
|
520
|
+
|
521
|
+
### Specifying Decorators
|
522
|
+
|
523
|
+
When you're using `decorates_association`, Draper uses the `decorate` method on
|
524
|
+
the associated record(s) to perform the decoration. If you want use a specific
|
525
|
+
decorator, you can use the `:with` option:
|
526
|
+
|
527
|
+
```ruby
|
528
|
+
class ArticleDecorator < Draper::Decorator
|
529
|
+
decorates_association :author, with: FancyPersonDecorator
|
530
|
+
end
|
531
|
+
```
|
532
|
+
|
533
|
+
For a collection association, you can specify a `CollectionDecorator` subclass,
|
534
|
+
which is applied to the whole collection, or a singular `Decorator` subclass,
|
535
|
+
which is applied to each item individually.
|
536
|
+
|
537
|
+
### Scoping Associations
|
538
|
+
|
539
|
+
If you want your decorated association to be ordered, limited, or otherwise
|
540
|
+
scoped, you can pass a `:scope` option to `decorates_association`, which will be
|
541
|
+
applied to the collection *before* decoration:
|
542
|
+
|
543
|
+
```ruby
|
544
|
+
class ArticleDecorator < Draper::Decorator
|
545
|
+
decorates_association :comments, scope: :recent
|
546
|
+
end
|
547
|
+
```
|
548
|
+
|
549
|
+
### Proxying Class Methods
|
550
|
+
|
551
|
+
If you want to proxy class methods to the wrapped model class, including when
|
552
|
+
using `decorates_finders`, Draper needs to know the model class. By default, it
|
553
|
+
assumes that your decorators are named `SomeModelDecorator`, and then attempts
|
554
|
+
to proxy unknown class methods to `SomeModel`.
|
555
|
+
|
556
|
+
If your model name can't be inferred from your decorator name in this way, you
|
557
|
+
need to use the `decorates` method:
|
558
|
+
|
559
|
+
```ruby
|
560
|
+
class MySpecialArticleDecorator < Draper::Decorator
|
561
|
+
decorates :article
|
562
|
+
end
|
563
|
+
```
|
564
|
+
|
565
|
+
This is only necessary when proxying class methods.
|
566
|
+
|
567
|
+
### Making Models Decoratable
|
568
|
+
|
569
|
+
Models get their `decorate` method from the `Draper::Decoratable` module, which
|
570
|
+
is included in `ActiveRecord::Base` and `Mongoid::Document` by default. If
|
571
|
+
you're [using another
|
572
|
+
ORM](https://github.com/drapergem/draper/wiki/Using-other-ORMs) (including
|
573
|
+
versions of Mongoid prior to 3.0), or want to decorate plain old Ruby objects,
|
574
|
+
you can include this module manually.
|
575
|
+
|
576
|
+
## Contributors
|
577
|
+
|
578
|
+
Draper was conceived by Jeff Casimir and heavily refined by Steve Klabnik and a
|
579
|
+
great community of open source
|
580
|
+
[contributors](https://github.com/drapergem/draper/contributors).
|
581
|
+
|
582
|
+
### Core Team
|
583
|
+
|
584
|
+
* Jeff Casimir (jeff@jumpstartlab.com)
|
585
|
+
* Steve Klabnik (steve@jumpstartlab.com)
|
586
|
+
* Vasiliy Ermolovich
|
587
|
+
* Andrew Haines
|