jamesgolick-draper 1.1.1a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +16 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +22 -0
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +150 -0
  6. data/CONTRIBUTING.md +15 -0
  7. data/Gemfile +39 -0
  8. data/Guardfile +26 -0
  9. data/LICENSE +7 -0
  10. data/README.md +417 -0
  11. data/Rakefile +69 -0
  12. data/draper.gemspec +32 -0
  13. data/lib/draper.rb +64 -0
  14. data/lib/draper/automatic_delegation.rb +56 -0
  15. data/lib/draper/collection_decorator.rb +96 -0
  16. data/lib/draper/decoratable.rb +82 -0
  17. data/lib/draper/decoratable/equality.rb +18 -0
  18. data/lib/draper/decorated_association.rb +35 -0
  19. data/lib/draper/decorates_assigned.rb +44 -0
  20. data/lib/draper/decorator.rb +248 -0
  21. data/lib/draper/delegation.rb +13 -0
  22. data/lib/draper/factory.rb +87 -0
  23. data/lib/draper/finders.rb +37 -0
  24. data/lib/draper/helper_proxy.rb +38 -0
  25. data/lib/draper/helper_support.rb +5 -0
  26. data/lib/draper/lazy_helpers.rb +15 -0
  27. data/lib/draper/railtie.rb +63 -0
  28. data/lib/draper/tasks/test.rake +22 -0
  29. data/lib/draper/test/devise_helper.rb +30 -0
  30. data/lib/draper/test/minitest_integration.rb +6 -0
  31. data/lib/draper/test/rspec_integration.rb +16 -0
  32. data/lib/draper/test_case.rb +53 -0
  33. data/lib/draper/version.rb +3 -0
  34. data/lib/draper/view_context.rb +99 -0
  35. data/lib/draper/view_context/build_strategy.rb +48 -0
  36. data/lib/draper/view_helpers.rb +37 -0
  37. data/lib/generators/decorator/decorator_generator.rb +36 -0
  38. data/lib/generators/decorator/templates/decorator.rb +19 -0
  39. data/lib/generators/mini_test/decorator_generator.rb +20 -0
  40. data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
  41. data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
  42. data/lib/generators/resource_override.rb +12 -0
  43. data/lib/generators/rspec/decorator_generator.rb +9 -0
  44. data/lib/generators/rspec/templates/decorator_spec.rb +4 -0
  45. data/lib/generators/test_unit/decorator_generator.rb +9 -0
  46. data/lib/generators/test_unit/templates/decorator_test.rb +4 -0
  47. data/spec/draper/collection_decorator_spec.rb +279 -0
  48. data/spec/draper/decoratable/equality_spec.rb +10 -0
  49. data/spec/draper/decoratable_spec.rb +176 -0
  50. data/spec/draper/decorated_association_spec.rb +84 -0
  51. data/spec/draper/decorates_assigned_spec.rb +71 -0
  52. data/spec/draper/decorator_spec.rb +634 -0
  53. data/spec/draper/factory_spec.rb +238 -0
  54. data/spec/draper/finders_spec.rb +166 -0
  55. data/spec/draper/helper_proxy_spec.rb +53 -0
  56. data/spec/draper/lazy_helpers_spec.rb +21 -0
  57. data/spec/draper/view_context/build_strategy_spec.rb +116 -0
  58. data/spec/draper/view_context_spec.rb +154 -0
  59. data/spec/draper/view_helpers_spec.rb +8 -0
  60. data/spec/dummy/.rspec +2 -0
  61. data/spec/dummy/Rakefile +7 -0
  62. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  63. data/spec/dummy/app/controllers/localized_urls.rb +5 -0
  64. data/spec/dummy/app/controllers/posts_controller.rb +20 -0
  65. data/spec/dummy/app/decorators/mongoid_post_decorator.rb +2 -0
  66. data/spec/dummy/app/decorators/post_decorator.rb +56 -0
  67. data/spec/dummy/app/helpers/application_helper.rb +5 -0
  68. data/spec/dummy/app/mailers/application_mailer.rb +3 -0
  69. data/spec/dummy/app/mailers/post_mailer.rb +19 -0
  70. data/spec/dummy/app/models/admin.rb +5 -0
  71. data/spec/dummy/app/models/mongoid_post.rb +5 -0
  72. data/spec/dummy/app/models/post.rb +3 -0
  73. data/spec/dummy/app/models/user.rb +5 -0
  74. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  75. data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
  76. data/spec/dummy/app/views/posts/_post.html.erb +34 -0
  77. data/spec/dummy/app/views/posts/show.html.erb +1 -0
  78. data/spec/dummy/bin/rails +4 -0
  79. data/spec/dummy/config.ru +4 -0
  80. data/spec/dummy/config/application.rb +71 -0
  81. data/spec/dummy/config/boot.rb +5 -0
  82. data/spec/dummy/config/database.yml +25 -0
  83. data/spec/dummy/config/environment.rb +5 -0
  84. data/spec/dummy/config/environments/development.rb +33 -0
  85. data/spec/dummy/config/environments/production.rb +57 -0
  86. data/spec/dummy/config/environments/test.rb +31 -0
  87. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  88. data/spec/dummy/config/initializers/inflections.rb +15 -0
  89. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  90. data/spec/dummy/config/initializers/secret_token.rb +8 -0
  91. data/spec/dummy/config/initializers/session_store.rb +8 -0
  92. data/spec/dummy/config/locales/en.yml +5 -0
  93. data/spec/dummy/config/mongoid.yml +80 -0
  94. data/spec/dummy/config/routes.rb +9 -0
  95. data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
  96. data/spec/dummy/db/schema.rb +21 -0
  97. data/spec/dummy/db/seeds.rb +2 -0
  98. data/spec/dummy/fast_spec/post_decorator_spec.rb +38 -0
  99. data/spec/dummy/lib/tasks/test.rake +16 -0
  100. data/spec/dummy/public/404.html +26 -0
  101. data/spec/dummy/public/422.html +26 -0
  102. data/spec/dummy/public/500.html +25 -0
  103. data/spec/dummy/public/favicon.ico +0 -0
  104. data/spec/dummy/script/rails +6 -0
  105. data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +11 -0
  106. data/spec/dummy/spec/decorators/devise_spec.rb +64 -0
  107. data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
  108. data/spec/dummy/spec/decorators/post_decorator_spec.rb +58 -0
  109. data/spec/dummy/spec/decorators/spec_type_spec.rb +7 -0
  110. data/spec/dummy/spec/decorators/view_context_spec.rb +22 -0
  111. data/spec/dummy/spec/mailers/post_mailer_spec.rb +33 -0
  112. data/spec/dummy/spec/models/mongoid_post_spec.rb +8 -0
  113. data/spec/dummy/spec/models/post_spec.rb +6 -0
  114. data/spec/dummy/spec/shared_examples/decoratable.rb +24 -0
  115. data/spec/dummy/spec/spec_helper.rb +9 -0
  116. data/spec/dummy/test/decorators/minitest/devise_test.rb +64 -0
  117. data/spec/dummy/test/decorators/minitest/helpers_test.rb +21 -0
  118. data/spec/dummy/test/decorators/minitest/spec_type_test.rb +52 -0
  119. data/spec/dummy/test/decorators/minitest/view_context_test.rb +24 -0
  120. data/spec/dummy/test/decorators/test_unit/devise_test.rb +64 -0
  121. data/spec/dummy/test/decorators/test_unit/helpers_test.rb +21 -0
  122. data/spec/dummy/test/decorators/test_unit/view_context_test.rb +24 -0
  123. data/spec/dummy/test/minitest_helper.rb +4 -0
  124. data/spec/dummy/test/test_helper.rb +3 -0
  125. data/spec/generators/decorator/decorator_generator_spec.rb +130 -0
  126. data/spec/integration/integration_spec.rb +58 -0
  127. data/spec/performance/active_record.rb +4 -0
  128. data/spec/performance/benchmark.rb +55 -0
  129. data/spec/performance/decorators.rb +45 -0
  130. data/spec/performance/models.rb +20 -0
  131. data/spec/spec_helper.rb +37 -0
  132. data/spec/support/dummy_app.rb +85 -0
  133. data/spec/support/matchers/have_text.rb +50 -0
  134. data/spec/support/shared_examples/decoratable_equality.rb +40 -0
  135. data/spec/support/shared_examples/view_helpers.rb +39 -0
  136. metadata +451 -0
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rvmrc
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
6
+ coverage.data
7
+ coverage/*
8
+ .yardoc
9
+ doc/*
10
+ tmp
11
+ vendor/bundle
12
+ *.swp
13
+ *.swo
14
+ *.DS_Store
15
+ spec/dummy/log/*
16
+ spec/dummy/db/*.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order rand
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+
3
+ services:
4
+ - mongodb
5
+
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - rbx-19mode
10
+ - jruby-19mode
11
+ - ruby-head
12
+
13
+ env:
14
+ - "RAILS_VERSION=3.2"
15
+ - "RAILS_VERSION=3.1"
16
+ - "RAILS_VERSION=3.0"
17
+ - "RAILS_VERSION=master"
18
+
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: ruby-head
22
+ - env: "RAILS_VERSION=master"
@@ -0,0 +1 @@
1
+ yardoc 'lib/draper/**/*.rb' -m markdown --no-private
@@ -0,0 +1,150 @@
1
+ # Draper Changelog
2
+
3
+ ## 1.1.0
4
+
5
+ [44 commits by 6 authors](https://github.com/drapergem/draper/compare/v1.0.0...v1.1.0)
6
+
7
+ * README improvements.
8
+ * Rails 4 compatibility.
9
+ [b2401c7](https://github.com/drapergem/draper/commit/b2401c71e092470e3b912b5da475115c22b55734)
10
+ * Added support for testing decorators without loading `ApplicationController`.
11
+ See the [README](https://github.com/drapergem/draper/blob/v1.1.0/README.md#isolated-tests) for details.
12
+ [4d0181f](https://github.com/drapergem/draper/commit/4d0181fb9c65dc769b05ed19bfcec2119d6e88f7)
13
+ * Improved the `==` method to check for `decorated?` before attempting to
14
+ compare with `source`.
15
+ [6c31617](https://github.com/drapergem/draper/commit/6c316176f5039a5861491fbcaa81f64ac4b36768)
16
+ * Changed the way helpers are accessed, so that helper methods may be stubbed
17
+ in tests.
18
+ [7a04619](https://github.com/drapergem/draper/commit/7a04619a06f832801bd4aedaaf5985d6e3e5e1af)
19
+ * Made the Devise test helper `sign_in` method independent of mocking
20
+ framework.
21
+ [b0902ab](https://github.com/drapergem/draper/commit/b0902ab0fe01916b7fddb0a3d97aa0c7cca09482)
22
+ * Stopped attempting to call `to_a` on decorated objects (a now-redundant lazy
23
+ query workaround).
24
+ [34c6390](https://github.com/drapergem/draper/commit/34c6390583f7fc7704d04e38bc974b65fc92517c)
25
+ * Fixed a minor bug where view contexts could be accidentally shared between
26
+ tests.
27
+ [3d07cb3](https://github.com/drapergem/draper/commit/3d07cb387b1cae6f97897dfb85512e30f5e888e9)
28
+ * Improved helper method performance.
29
+ [e6f88a5](https://github.com/drapergem/draper/commit/e6f88a5e7dada3f9db480e13e16d1acc964ba098)
30
+ * Our specs now use the new RSpec `expect( ).to` syntax.
31
+ [9a3b319](https://github.com/drapergem/draper/commit/9a3b319d6d54cd78fb2654a94bbe893e36359754)
32
+
33
+ ## 1.0.0
34
+
35
+ [249 commits by 19 authors](https://github.com/drapergem/draper/compare/v0.18.0...v1.0.0)
36
+
37
+ Major changes are described [in the upgrade guide](https://github.com/drapergem/draper/wiki/Upgrading-to-1.0).
38
+
39
+ * Infer collection decorators.
40
+ [e8253df](https://github.com/drapergem/draper/commit/e8253df7dc6c90a542444c0f4ef289909fce4f90)
41
+ * Prevent calls to `scoped` on decorated associations.
42
+ [5dcc6c3](https://github.com/drapergem/draper/commit/5dcc6c31ecf408753158d15fed9fb23fbfdc3734)
43
+ * Add `helper` method to tests.
44
+ [551961e](https://github.com/drapergem/draper/commit/551961e72ee92355bc9c848bedfcc573856d12b0)
45
+ * Inherit method security.
46
+ [1865ed3](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
47
+ * Test against all versions of Rails 3.
48
+ [1865ed3](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
49
+ * Pretend to be `instance_of?(source.class)`.
50
+ [30d209f](https://github.com/drapergem/draper/commit/30d209f990847e84b221ac798e84b976f5775cc0)
51
+ * Remove security from `Decorator`. Do manual delegation with `:delegate`.
52
+ [c6f8aaa](https://github.com/drapergem/draper/commit/c6f8aaa2b2bd4679738050aede2503aa8e9db130)
53
+ * Add generators for MiniTest.
54
+ [1fac02b](https://github.com/drapergem/draper/commit/1fac02b65b15e32f06e8292cb858c97cb1c1da2c)
55
+ * Test against edge rails.
56
+ [e9b71e3](https://github.com/drapergem/draper/commit/e9b71e3cf55a800b48c083ff257a7c1cbe1b601b)
57
+
58
+ ### 1.0.0.beta6
59
+
60
+ * Fix up README to include changes made.
61
+ [5e6e4d1](https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6)
62
+ * `CollectionDecorator` no longer freezes its collection: direct access is
63
+ discouraged by making access private.
64
+ [c6d60e6](https://github.com/drapergem/draper/commit/c6d60e6577ed396385f3f1151c3f188fe47e9a57)
65
+ * A fix for `Decoratable#==`.
66
+ [e4fa239](https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65)
67
+ * Ensure we coerce to an array in the right place.
68
+ [9eb9fc9](https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095)
69
+
70
+ ### 1.0.0.beta5
71
+
72
+ * Change CollectionDecorator to freeze its collection.
73
+ [04d7796](https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4)
74
+ * Bugfix on `CollectionDecorator#to_s`.
75
+ [eefd7d0](https://github.com/drapergem/draper/commit/eefd7d09cac97d531b9235246378c3746d153f08)
76
+ * Upgrade `request_store` dependency to take advantage of a bugfix.
77
+ [9f17212](https://github.com/drapergem/draper/commit/9f17212fd1fb656ef1314327d60fe45e0acf60a2)
78
+
79
+ ### 1.0.0.beta4
80
+
81
+ * Fixed a race condition with capybara integration.
82
+ [e794649](https://github.com/drapergem/draper/commit/e79464931e7b98c85ed5d78ed9ca38d51f43006e)
83
+ * `[]` can be decorated again.
84
+ [597fbdf](https://github.com/drapergem/draper/commit/597fbdf0c80583f5ea6df9f7350fefeaa0cca989)
85
+ * `model == decorator` as well as `decorator == model`.
86
+ [46f8a68](https://github.com/drapergem/draper/commit/46f8a6823c50c13e5c9ab3c07723f335c4e291bc)
87
+ * Preliminary Mongoid integration.
88
+ [892d195](https://github.com/drapergem/draper/commit/892d1954202c61fd082a07213c8d4a23560687bc)
89
+ * Add a helper method `sign_in` for devise in decorator specs.
90
+ [66a3009](https://github.com/drapergem/draper/commit/66a30093ed4207d02d8fa60bda4df2da091d85a3)
91
+ * Brought back `context`.
92
+ [9609156](https://github.com/drapergem/draper/commit/9609156b997b3a469386eef3a5f043b24d8a2fba)
93
+ * Fixed issue where classes were incorrectly being looked up.
94
+ [ee2a015](https://github.com/drapergem/draper/commit/ee2a015514ff87dfd2158926457e988c2fc3fd79)
95
+ * Integrate RequestStore for per-request storage.
96
+ [fde1cde](https://github.com/drapergem/draper/commit/fde1cde9adfb856750c1f616d8b62d221ef97fc6)
97
+
98
+ ### 1.0.0.beta3
99
+
100
+ * Relaxed Rails version requirement to 3.0. Support for < 3.2 should be
101
+ considered experimental. Please file bug reports.
102
+
103
+ ### 1.0.0.beta2
104
+
105
+ * `has_finders` is now `decorates_finders`.
106
+ [33f18aa](https://github.com/drapergem/draper/commit/33f18aa062e0d3848443dbd81047f20d5665579f)
107
+ * If a finder method is used, and the source class is not set and cannot be
108
+ inferred, an `UninferrableSourceError` is raised.
109
+ [8ef5bf2](https://github.com/drapergem/draper/commit/8ef5bf2f02f7033e3cd4f1f5de7397b02c984fe3)
110
+ * Class methods are now properly delegated again.
111
+ [731995a](https://github.com/drapergem/draper/commit/731995a5feac4cd06cf9328d2892c0eca9992db6)
112
+ * We no longer `respond_to?` private methods on the source.
113
+ [18ebac8](https://github.com/drapergem/draper/commit/18ebac81533a6413aa20a3c26f23e91d0b12b031)
114
+ * Rails versioning relaxed to support Rails 4.
115
+ [8bfd393](https://github.com/drapergem/draper/commit/8bfd393b5baa7aa1488076a5e2cb88648efaa815)
116
+
117
+ ### 1.0.0.beta1
118
+
119
+ * Renaming `Draper::Base` to `Draper::Decorator`. This is the most significant
120
+ change you'll need to upgrade your application.
121
+ [025742c](https://github.com/drapergem/draper/commit/025742cb3b295d259cf0ecf3669c24817d6f2df1)
122
+ * Added an internal Rails application for integration tests. This won't affect
123
+ your application, but we're now running a set of Cucumber tests inside of a
124
+ Rails app in both development and production mode to help ensure that we
125
+ don't make changes that break Draper.
126
+ [90a4859](https://github.com/drapergem/draper/commit/90a4859085cab158658d23d77cd3108b6037e36f)
127
+ * Add `#decorated?` method. This gives us a free RSpec matcher,
128
+ `be_decorated`.
129
+ [834a6fd](https://github.com/drapergem/draper/commit/834a6fd1f24b5646c333a04a99fe9846a58965d6)
130
+ * `#decorates` is no longer needed inside your models, and should be removed.
131
+ Decorators automatically infer the class they decorate.
132
+ [e1214d9](https://github.com/drapergem/draper/commit/e1214d97b62f2cab45227cc650029734160dcdfe)
133
+ * Decorators do not automatically come with 'finders' by default. If you'd like
134
+ to use `SomeDecorator.find(1)`, for example, simply add `#has_finders` to
135
+ the decorator to include them.
136
+ [42b6f78](https://github.com/drapergem/draper/commit/42b6f78fda4f51845dab4d35da68880f1989d178)
137
+ * To refer to the object being decorated, `#source` is now the preferred
138
+ method.
139
+ [1e84fcb](https://github.com/drapergem/draper/commit/1e84fcb4a0eab0d12f5feda6886ce1caa239cb16)
140
+ * `ActiveModel::Serialization` is included in Decorators if you've requred
141
+ `ActiveModel::Serializers`, so that decorators can be serialized.
142
+ [c4b3527](https://github.com/drapergem/draper/commit/c4b352799067506849abcbf14963ea36abda301c)
143
+ * Properly support Test::Unit.
144
+ [087e134](https://github.com/drapergem/draper/commit/087e134ed0885ec11325ffabe8ab2bebef77a33a)
145
+
146
+ And many small bug fixes and refactorings.
147
+
148
+ ## 0.x
149
+
150
+ See changes prior to version 1.0 [here](https://github.com/drapergem/draper/blob/16140fed55f57d18f8b10a0789dd1fa5b3115a8d/CHANGELOG.markdown).
@@ -0,0 +1,15 @@
1
+ ## Filing issues
2
+
3
+ To help us fix the problem you've found, please let us know 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).
4
+
5
+ ## Sending a pull request
6
+
7
+ So you want to help us out... thanks! Here's a quick how-to:
8
+
9
+ 1. [Fork the project](https://help.github.com/articles/fork-a-repo).
10
+ 2. Create a branch - `git checkout -b adding_magic`
11
+ 3. Make your changes, and add some tests!
12
+ 4. Check that the tests pass - `bundle exec rake`
13
+ 5. Commit your changes - `git commit -am "Added some magic"`
14
+ 6. Push the branch to Github - `git push origin adding_magic`
15
+ 7. Send us a [pull request](https://help.github.com/articles/using-pull-requests)!
data/Gemfile ADDED
@@ -0,0 +1,39 @@
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", ">= 3.0"
11
+ gem "activerecord-jdbcsqlite3-adapter"
12
+ end
13
+
14
+ version = ENV["RAILS_VERSION"] || "3.2"
15
+
16
+ rails = case version
17
+ when "master"
18
+ {github: "rails/rails"}
19
+ else
20
+ "~> #{version}.0"
21
+ end
22
+
23
+ mongoid = case version
24
+ when "master"
25
+ {github: "mongoid/mongoid"}
26
+ when "3.2"
27
+ "~> 3.1.0"
28
+ when "3.1"
29
+ "~> 3.0.0"
30
+ end
31
+
32
+ devise = case version
33
+ when "3.1", "3.2"
34
+ "~> 2.2"
35
+ end
36
+
37
+ gem "rails", rails
38
+ gem "mongoid", mongoid if mongoid
39
+ gem "devise", devise if devise
@@ -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.
@@ -0,0 +1,417 @@
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/github/drapergem/draper.png)](https://codeclimate.com/github/drapergem/draper)
5
+
6
+ Draper adds an object-oriented layer of presentation logic to your Rails application.
7
+
8
+ Without Draper, this functionality might have been tangled up in procedural helpers or adding bulk to your models. With Draper decorators, you can wrap your models with presentation-related logic to organise - and test - this layer of your app much more effectively.
9
+
10
+ ## Why Use a Decorator?
11
+
12
+ Imagine your application has an `Article` model. With Draper, you'd create a corresponding `ArticleDecorator`. The decorator wraps the model, and deals *only* with presentational concerns. In the controller, you decorate the article before handing it off to the view:
13
+
14
+ ```ruby
15
+ # app/controllers/articles_controller.rb
16
+ def show
17
+ @article = Article.find(params[:id]).decorate
18
+ end
19
+ ```
20
+
21
+ In the view, you can use the decorator in exactly the same way as you would have used the model. But whenever you start needing logic in the view or start thinking about a helper method, you can implement a method on the decorator instead.
22
+
23
+ Let's look at how you could convert an existing Rails helper to a decorator method. You have this existing helper:
24
+
25
+ ```ruby
26
+ # app/helpers/articles_helper.rb
27
+ def publication_status(article)
28
+ if article.published?
29
+ "Published at #{article.published_at.strftime('%A, %B %e')}"
30
+ else
31
+ "Unpublished"
32
+ end
33
+ end
34
+ ```
35
+
36
+ But it makes you a little uncomfortable. `publication_status` lives in a nebulous namespace spread across all controllers and view. Down the road, you might want to display the publication status of a `Book`. And, of course, your design calls for a slighly different formatting to the date for a `Book`.
37
+
38
+ Now your helper method can either switch based on the input class type (poor Ruby style), or you break it out into two methods, `book_publication_status` and `article_publication_status`. And keep adding methods for each publication type...to the global helper namespace. And remember all the names. Ick.
39
+
40
+ Ruby thrives when we use Object-Oriented style. If you didn't know Rails' helpers existed, you'd probably imagine that your view template could feature something like this:
41
+
42
+ ```erb
43
+ <%= @article.publication_status %>
44
+ ```
45
+
46
+ Without a decorator, you'd have to implement the `publication_status` method in the `Article` model. That method is presentation-centric, and thus does not belong in a model.
47
+
48
+ Instead, you implement a decorator:
49
+
50
+ ```ruby
51
+ # app/decorators/article_decorator.rb
52
+ class ArticleDecorator < Draper::Decorator
53
+ delegate_all
54
+
55
+ def publication_status
56
+ if published?
57
+ "Published at #{published_at}"
58
+ else
59
+ "Unpublished"
60
+ end
61
+ end
62
+
63
+ def published_at
64
+ source.published_at.strftime("%A, %B %e")
65
+ end
66
+ end
67
+ ```
68
+
69
+ Within the `publication_status` method we use the `published?` method. Where does that come from? It's a method of the source `Article`, whose methods have been made available on the decorator by the `delegate_all` call above.
70
+
71
+ 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.
72
+
73
+ Decorators are the ideal place to:
74
+ * format complex data for user display
75
+ * define commonly-used representations of an object, like a `name` method that combines `first_name` and `last_name` attributes
76
+ * mark up attributes with a little semantic HTML, like turning a `url` field into a hyperlink
77
+
78
+ ## Installation
79
+
80
+ Add Draper to your Gemfile:
81
+
82
+ ```ruby
83
+ gem 'draper', '~> 1.0'
84
+ ```
85
+
86
+ And run `bundle install` within your app's directory.
87
+
88
+ If you're upgrading from a 0.x release, the major changes are outlined [in the wiki](https://github.com/drapergem/draper/wiki/Upgrading-to-1.0).
89
+
90
+ ## Writing Decorators
91
+
92
+ Decorators inherit from `Draper::Decorator`, live in your `app/decorators` directory, and are named for the model that they decorate:
93
+
94
+ ```ruby
95
+ # app/decorators/article_decorator.rb
96
+ class ArticleDecorator < Draper::Decorator
97
+ # ...
98
+ end
99
+ ```
100
+
101
+ ### Generators
102
+
103
+ When you have Draper installed and generate a resource with...
104
+
105
+ ```
106
+ rails generate resource Article
107
+ ```
108
+ ...you'll get a decorator for free!
109
+
110
+ But if the `Article` model already exists, you can run...
111
+
112
+ ```
113
+ rails generate decorator Article
114
+ ```
115
+
116
+ ...to create the `ArticleDecorator`.
117
+
118
+ ### Accessing Helpers
119
+
120
+ Normal Rails helpers are still useful for lots of tasks. Both Rails' provided helper and those defined in your app can be accessed via the `h` method:
121
+
122
+ ```ruby
123
+ class ArticleDecorator < Draper::Decorator
124
+ def emphatic
125
+ h.content_tag(:strong, "Awesome")
126
+ end
127
+ end
128
+ ```
129
+
130
+ If writing `h.` frequently is getting you down, you can add...
131
+
132
+ ```
133
+ include Draper::LazyHelpers
134
+ ```
135
+
136
+ ...at the top of your decorator class - you'll mix in a bazillion methods and never have to type `h.` again.
137
+ (Note: the `capture` method is only available through `h` or `helpers`)
138
+
139
+ ### Accessing the model
140
+
141
+ When writing decorator methods you'll usually need to access the wrapped model. While you may choose to use delegation ([covered below](#delegating-methods)) for convenience, you can always use the `source` (or its alias `model`):
142
+
143
+ ```ruby
144
+ class ArticleDecorator < Draper::Decorator
145
+ def published_at
146
+ source.published_at.strftime("%A, %B %e")
147
+ end
148
+ end
149
+ ```
150
+
151
+ ## Decorating Objects
152
+
153
+ ### Single Objects
154
+
155
+ 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:
156
+
157
+ ```ruby
158
+ @article = Article.first.decorate
159
+ ```
160
+
161
+ 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:
162
+
163
+ ```ruby
164
+ @widget = ProductDecorator.new(Widget.first)
165
+ # or, equivalently
166
+ @widget = ProductDecorator.decorate(Widget.first)
167
+ ```
168
+
169
+ ### Collections
170
+
171
+ If you have a whole bunch of objects, you can decorate them all in one fell swoop:
172
+
173
+ ```ruby
174
+ @articles = ArticleDecorator.decorate_collection(Article.all)
175
+ ```
176
+
177
+ If your collection is an ActiveRecord query, you can use this:
178
+
179
+ ```ruby
180
+ @articles = Article.popular.decorate
181
+ ```
182
+
183
+ *Note:* In Rails 3, the `.all` method returns an array and not a query. Thus you _cannot_ use the technique of `Article.all.decorate` in Rails 3. In Rails 4, `.all` returns a query so this techique would work fine.
184
+
185
+ If you want to add methods to your decorated collection (for example, for pagination), you can subclass `Draper::CollectionDecorator`:
186
+
187
+ ```ruby
188
+ # app/decorators/articles_decorator.rb
189
+ class ArticlesDecorator < Draper::CollectionDecorator
190
+ def page_number
191
+ 42
192
+ end
193
+ end
194
+
195
+ # elsewhere...
196
+ @articles = ArticlesDecorator.new(Article.all)
197
+ # or, equivalently
198
+ @articles = ArticlesDecorator.decorate(Article.all)
199
+ ```
200
+
201
+ Draper decorates each item using its `decorate` method. Alternatively, you can specify a decorator by overriding the collection decorator's `decorator_class` method, or by passing the `:with` option to the constructor.
202
+
203
+ #### Using pagination
204
+
205
+ 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 delegate to the `source`:
206
+
207
+ ```ruby
208
+ class PaginatingDecorator < Draper::CollectionDecorator
209
+ delegate :current_page, :total_pages, :limit_value
210
+ end
211
+ ```
212
+
213
+ The `delegate` method used here is the same as that added by [Active Support](http://api.rubyonrails.org/classes/Module.html#method-i-delegate), except that the `:to` option is not required; it defaults to `:source` when omitted.
214
+
215
+ [will_paginate](https://github.com/mislav/will_paginate) needs you to `delegate :current_page, :per_page, :offset, :total_entries, :total_pages`.
216
+
217
+ ### Decorating Associated Objects
218
+
219
+ You can automatically decorate associated models when the primary model is decorated. Assuming an `Article` model has an associated `Author` object:
220
+
221
+ ```ruby
222
+ class ArticleDecorator < Draper::Decorator
223
+ decorates_association :author
224
+ end
225
+ ```
226
+
227
+ When `ArticleDecorator` decorates an `Article`, it will also use `AuthorDecorator` to decorate the associated `Author`.
228
+
229
+ ### Decorated Finders
230
+
231
+ You can call `decorates_finders` in a decorator...
232
+
233
+ ```ruby
234
+ class ArticleDecorator < Draper::Decorator
235
+ decorates_finders
236
+ end
237
+ ```
238
+
239
+ ...which allows you to then call all the normal ActiveRecord-style finders on your `ArticleDecorator` and they'll return decorated objects:
240
+
241
+ ```ruby
242
+ @article = ArticleDecorator.find(params[:id])
243
+ ```
244
+
245
+ ## Testing
246
+
247
+ Draper supports RSpec, MiniTest::Rails, and Test::Unit, and will add the appropriate tests when you generate a decorator.
248
+
249
+ ### RSpec
250
+
251
+ Your specs are expected to live in `spec/decorators`. If you use a different path, you need to tag them with `type: :decorator`.
252
+
253
+ In a controller spec, you might want to check whether your instance variables are being decorated properly. You can use the handy predicate matchers:
254
+
255
+ ```ruby
256
+ assigns(:article).should be_decorated
257
+
258
+ # or, if you want to be more specific
259
+ assigns(:article).should be_decorated_with ArticleDecorator
260
+ ```
261
+
262
+ Note that `model.decorate == model`, so your existing specs shouldn't break when you add the decoration.
263
+
264
+ #### Spork Users
265
+
266
+ In your `Spork.prefork` block of `spec_helper.rb`, add this:
267
+
268
+ ```ruby
269
+ require 'draper/test/rspec_integration'
270
+ ```
271
+
272
+ ### Isolated tests
273
+
274
+ In tests, Draper needs to build a view context to access helper methods. By default, it will create an `ApplicationController` and then use its view context. If you are speeding up your test suite by testing each component in isolation, you can eliminate this dependency by putting the following in your `spec_helper` or similar:
275
+
276
+ ```ruby
277
+ Draper::ViewContext.test_strategy :fast
278
+ ```
279
+
280
+ In doing so, your decorators will no longer have access to your application's helpers. If you need to selectively include such helpers, you can pass a block:
281
+
282
+ ```ruby
283
+ Draper::ViewContext.test_strategy :fast do
284
+ include ApplicationHelper
285
+ end
286
+ ```
287
+
288
+ ## Advanced usage
289
+
290
+ ### Shared Decorator Methods
291
+
292
+ You might have several decorators that share similar needs. Since decorators are just Ruby objects, you can use any normal Ruby technique for sharing functionality.
293
+
294
+ In Rails controllers, common functionality is organized by having all controllers inherit from `ApplicationController`. You can apply this same pattern to your decorators:
295
+
296
+ ```ruby
297
+ # app/decorators/application_decorator.rb
298
+ class ApplicationDecorator < Draper::Decorator
299
+ # ...
300
+ end
301
+ ```
302
+
303
+ Then modify your decorators to inherit from that `ApplicationDecorator` instead of directly from `Draper::Decorator`:
304
+
305
+ ```ruby
306
+ class ArticleDecorator < ApplicationDecorator
307
+ # decorator methods
308
+ end
309
+ ```
310
+
311
+ ### Delegating Methods
312
+
313
+ When your decorator calls `delegate_all`, any method called on the decorator not defined in the decorator itself will be delegated to the decorated source. This is a very permissive interface.
314
+
315
+ If you want to strictly control which methods are called within views, you can choose to only delegate certain methods from the decorator to the source model:
316
+
317
+ ```ruby
318
+ class ArticleDecorator < Draper::Decorator
319
+ delegate :title, :body
320
+ end
321
+ ```
322
+
323
+ We omit the `:to` argument here as it defaults to the `source` object. You could choose to delegate methods to other places like this:
324
+
325
+ ```ruby
326
+ class ArticleDecorator < Draper::Decorator
327
+ delegate :title, :body
328
+ delegate :name, :title, to: :author, prefix: true
329
+ end
330
+ ```
331
+
332
+ From your view template, assuming `@article` is decorated, you could do any of the following:
333
+
334
+ ```ruby
335
+ @article.title # Returns the article's `.title`
336
+ @article.body # Returns the article's `.body`
337
+ @article.author_name # Returns the article's `author.name`
338
+ @article.author_title # Returns the article's `author.title`
339
+ ```
340
+
341
+ ### Adding context
342
+
343
+ 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:
344
+
345
+ ```ruby
346
+ Article.first.decorate(context: {role: :admin})
347
+ ```
348
+
349
+ The value passed to the `:context` option is then available in the decorator through the `context` method.
350
+
351
+ 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:
352
+
353
+ ```ruby
354
+ class ArticleDecorator < Draper::Decorator
355
+ decorates_association :author, context: {foo: "bar"}
356
+ end
357
+ ```
358
+
359
+ or, if you want to modify the parent's context, use a lambda that takes a hash and returns a new hash:
360
+
361
+ ```ruby
362
+ class ArticleDecorator < Draper::Decorator
363
+ decorates_association :author,
364
+ context: ->(parent_context){ parent_context.merge(foo: "bar") }
365
+ end
366
+ ```
367
+
368
+ ### Specifying Decorators
369
+
370
+ When you're using `decorates_association`, Draper uses the `decorate` method on the associated record(s) to perform the decoration. If you want use a specific decorator, you can use the `:with` option:
371
+
372
+ ```ruby
373
+ class ArticleDecorator < Draper::Decorator
374
+ decorates_association :author, with: FancyPersonDecorator
375
+ end
376
+ ```
377
+
378
+ 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.
379
+
380
+ ### Scoping Associations
381
+
382
+ 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:
383
+
384
+ ```ruby
385
+ class ArticleDecorator < Draper::Decorator
386
+ decorates_association :comments, scope: :recent
387
+ end
388
+ ```
389
+
390
+ ### Proxying Class Methods
391
+
392
+ If you want to proxy class methods to the wrapped model class, 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`.
393
+
394
+ If your model name can't be inferred from your decorator name in this way, you need to use the `decorates` method:
395
+
396
+ ```ruby
397
+ class MySpecialArticleDecorator < Draper::Decorator
398
+ decorates :article
399
+ end
400
+ ```
401
+
402
+ This is only necessary when proxying class methods.
403
+
404
+ ### Making models decoratable
405
+
406
+ 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](https://github.com/drapergem/draper/wiki/Using-other-ORMs) (including versions of Mongoid prior to 3.0), or want to decorate plain old Ruby objects, you can include this module manually.
407
+
408
+ ## Contributors
409
+
410
+ 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).
411
+
412
+ ### Core Team
413
+
414
+ * Jeff Casimir (jeff@jumpstartlab.com)
415
+ * Steve Klabnik (steve@jumpstartlab.com)
416
+ * Vasiliy Ermolovich
417
+ * Andrew Haines