draper 0.18.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. data/.gitignore +3 -0
  2. data/.travis.yml +11 -0
  3. data/.yardopts +1 -1
  4. data/CHANGELOG.md +161 -0
  5. data/CONTRIBUTING.md +11 -0
  6. data/Gemfile +20 -0
  7. data/LICENSE +7 -0
  8. data/README.md +324 -0
  9. data/Rakefile +52 -31
  10. data/draper.gemspec +11 -9
  11. data/lib/draper/automatic_delegation.rb +50 -0
  12. data/lib/draper/collection_decorator.rb +107 -0
  13. data/lib/draper/decoratable.rb +83 -0
  14. data/lib/draper/decorated_association.rb +74 -0
  15. data/lib/draper/decorator.rb +238 -0
  16. data/lib/draper/delegation.rb +13 -0
  17. data/lib/draper/finders.rb +37 -0
  18. data/lib/draper/helper_proxy.rb +17 -0
  19. data/lib/draper/lazy_helpers.rb +10 -6
  20. data/lib/draper/railtie.rb +27 -21
  21. data/lib/draper/tasks/test.rake +22 -0
  22. data/lib/draper/test/devise_helper.rb +34 -0
  23. data/lib/draper/test/minitest_integration.rb +2 -3
  24. data/lib/draper/test/rspec_integration.rb +3 -13
  25. data/lib/draper/test_case.rb +33 -0
  26. data/lib/draper/version.rb +1 -1
  27. data/lib/draper/view_context.rb +12 -17
  28. data/lib/draper/view_helpers.rb +37 -0
  29. data/lib/draper.rb +47 -8
  30. data/lib/generators/decorator/decorator_generator.rb +10 -2
  31. data/lib/generators/decorator/templates/decorator.rb +12 -27
  32. data/lib/generators/mini_test/decorator_generator.rb +20 -0
  33. data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
  34. data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
  35. data/lib/generators/test_unit/templates/decorator_test.rb +1 -1
  36. data/spec/draper/collection_decorator_spec.rb +286 -0
  37. data/spec/draper/decoratable_spec.rb +192 -0
  38. data/spec/draper/decorated_association_spec.rb +142 -0
  39. data/spec/draper/decorator_spec.rb +624 -0
  40. data/spec/draper/finders_spec.rb +132 -0
  41. data/spec/draper/helper_proxy_spec.rb +12 -0
  42. data/spec/draper/view_helpers_spec.rb +41 -0
  43. data/spec/dummy/.rspec +2 -0
  44. data/spec/dummy/README.rdoc +261 -0
  45. data/spec/dummy/Rakefile +7 -0
  46. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  47. data/spec/dummy/app/controllers/localized_urls.rb +5 -0
  48. data/spec/dummy/app/controllers/posts_controller.rb +11 -0
  49. data/spec/dummy/app/decorators/post_decorator.rb +32 -0
  50. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  51. data/spec/dummy/app/mailers/application_mailer.rb +3 -0
  52. data/spec/dummy/app/mailers/post_mailer.rb +9 -0
  53. data/spec/dummy/app/models/post.rb +3 -0
  54. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  55. data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
  56. data/spec/dummy/app/views/posts/_post.html.erb +19 -0
  57. data/spec/dummy/app/views/posts/show.html.erb +1 -0
  58. data/spec/dummy/config/application.rb +64 -0
  59. data/spec/dummy/config/boot.rb +10 -0
  60. data/spec/dummy/config/database.yml +25 -0
  61. data/spec/dummy/config/environment.rb +5 -0
  62. data/spec/dummy/config/environments/development.rb +34 -0
  63. data/spec/dummy/config/environments/production.rb +55 -0
  64. data/spec/dummy/config/environments/test.rb +32 -0
  65. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  66. data/spec/dummy/config/initializers/inflections.rb +15 -0
  67. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  68. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  69. data/spec/dummy/config/initializers/session_store.rb +8 -0
  70. data/spec/dummy/config/locales/en.yml +5 -0
  71. data/spec/dummy/config/routes.rb +7 -0
  72. data/spec/dummy/config.ru +4 -0
  73. data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
  74. data/spec/dummy/db/schema.rb +21 -0
  75. data/spec/dummy/db/seeds.rb +2 -0
  76. data/spec/dummy/lib/tasks/test.rake +10 -0
  77. data/spec/dummy/log/.gitkeep +0 -0
  78. data/spec/dummy/mini_test/mini_test_integration_test.rb +46 -0
  79. data/spec/dummy/public/404.html +26 -0
  80. data/spec/dummy/public/422.html +26 -0
  81. data/spec/dummy/public/500.html +25 -0
  82. data/spec/dummy/public/favicon.ico +0 -0
  83. data/spec/dummy/script/rails +6 -0
  84. data/spec/dummy/spec/decorators/post_decorator_spec.rb +26 -0
  85. data/spec/dummy/spec/decorators/rspec_integration_spec.rb +19 -0
  86. data/spec/dummy/spec/mailers/post_mailer_spec.rb +29 -0
  87. data/spec/dummy/spec/models/post_spec.rb +14 -0
  88. data/spec/dummy/spec/spec_helper.rb +8 -0
  89. data/spec/generators/decorator/decorator_generator_spec.rb +47 -7
  90. data/spec/integration/integration_spec.rb +33 -0
  91. data/{performance → spec/performance}/decorators.rb +2 -4
  92. data/spec/spec_helper.rb +20 -45
  93. data/spec/support/action_controller.rb +12 -0
  94. data/spec/support/active_model.rb +7 -0
  95. data/spec/support/active_record.rb +9 -0
  96. data/spec/support/{samples → decorators}/decorator_with_application_helper.rb +1 -1
  97. data/spec/support/decorators/namespaced_product_decorator.rb +5 -0
  98. data/spec/support/decorators/non_active_model_product_decorator.rb +2 -0
  99. data/spec/support/decorators/product_decorator.rb +23 -0
  100. data/spec/support/{samples → decorators}/products_decorator.rb +1 -1
  101. data/spec/support/decorators/some_thing_decorator.rb +2 -0
  102. data/spec/support/{samples → decorators}/specific_product_decorator.rb +0 -2
  103. data/spec/support/dummy_app.rb +85 -0
  104. data/spec/support/matchers/have_text.rb +50 -0
  105. data/spec/support/{samples → models}/namespaced_product.rb +1 -3
  106. data/spec/support/{samples → models}/non_active_model_product.rb +1 -0
  107. data/spec/support/{samples → models}/product.rb +17 -2
  108. data/spec/support/models/some_thing.rb +5 -0
  109. data/spec/support/models/uninferrable_decorator_model.rb +3 -0
  110. metadata +241 -87
  111. data/CHANGELOG.markdown +0 -57
  112. data/Readme.markdown +0 -387
  113. data/lib/draper/active_model_support.rb +0 -27
  114. data/lib/draper/base.rb +0 -312
  115. data/lib/draper/decorated_enumerable_proxy.rb +0 -90
  116. data/lib/draper/model_support.rb +0 -25
  117. data/lib/draper/rspec_integration.rb +0 -2
  118. data/lib/draper/system.rb +0 -18
  119. data/spec/draper/base_spec.rb +0 -873
  120. data/spec/draper/decorated_enumerable_proxy_spec.rb +0 -45
  121. data/spec/draper/model_support_spec.rb +0 -48
  122. data/spec/minitest-rails/spec_type_spec.rb +0 -63
  123. data/spec/support/samples/active_record.rb +0 -17
  124. data/spec/support/samples/decorator.rb +0 -5
  125. data/spec/support/samples/decorator_with_allows.rb +0 -3
  126. data/spec/support/samples/decorator_with_denies.rb +0 -3
  127. data/spec/support/samples/decorator_with_denies_all.rb +0 -3
  128. data/spec/support/samples/decorator_with_multiple_allows.rb +0 -4
  129. data/spec/support/samples/decorator_with_special_methods.rb +0 -13
  130. data/spec/support/samples/enumerable_proxy.rb +0 -3
  131. data/spec/support/samples/namespaced_product_decorator.rb +0 -7
  132. data/spec/support/samples/product_decorator.rb +0 -7
  133. data/spec/support/samples/sequel_product.rb +0 -13
  134. data/spec/support/samples/some_thing.rb +0 -2
  135. data/spec/support/samples/some_thing_decorator.rb +0 -3
  136. /data/{performance → spec/performance}/active_record.rb +0 -0
  137. /data/{performance/bechmark.rb → spec/performance/benchmark.rb} +0 -0
  138. /data/{performance → spec/performance}/models.rb +0 -0
  139. /data/spec/support/{samples → decorators}/widget_decorator.rb +0 -0
  140. /data/spec/support/{samples → models}/widget.rb +0 -0
data/.gitignore CHANGED
@@ -6,8 +6,11 @@ pkg/*
6
6
  coverage.data
7
7
  coverage/*
8
8
  .yardoc
9
+ doc/*
9
10
  tmp
10
11
  vendor/bundle
11
12
  *.swp
12
13
  *.swo
13
14
  *.DS_Store
15
+ spec/dummy/log/*
16
+ spec/dummy/db/*.sqlite3
data/.travis.yml CHANGED
@@ -1,4 +1,15 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.9.3
3
4
  - rbx-19mode
4
5
  - jruby-19mode
6
+ - ruby-head
7
+ env:
8
+ - "RAILS_VERSION=3.2"
9
+ - "RAILS_VERSION=3.1"
10
+ - "RAILS_VERSION=3.0"
11
+ - "RAILS_VERSION=master"
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: ruby-head
15
+ - env: "RAILS_VERSION=master"
data/.yardopts CHANGED
@@ -1 +1 @@
1
- yardoc 'lib/draper/**/*.rb' -m markdown
1
+ yardoc 'lib/draper/**/*.rb' -m markdown --no-private
data/CHANGELOG.md ADDED
@@ -0,0 +1,161 @@
1
+ # Draper Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ * Infer collection decorators. [https://github.com/drapergem/draper/commit/e8253df7dc6c90a542444c0f4ef289909fce4f90](https://github.com/drapergem/draper/commit/e8253df7dc6c90a542444c0f4ef289909fce4f90)
6
+
7
+ * Prevent calls to `scoped` on decorated associations. [https://github.com/drapergem/draper/commit/5dcc6c31ecf408753158d15fed9fb23fbfdc3734](https://github.com/drapergem/draper/commit/5dcc6c31ecf408753158d15fed9fb23fbfdc3734)
8
+
9
+ * Add `helper` method to tests. [https://github.com/drapergem/draper/commit/551961e72ee92355bc9c848bedfcc573856d12b0](https://github.com/drapergem/draper/commit/551961e72ee92355bc9c848bedfcc573856d12b0)
10
+
11
+ * Inherit method security. [https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
12
+
13
+ * Test against all versions of Rails 3. [https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d](https://github.com/drapergem/draper/commit/1865ed3e3b2b34853689a60b59b8ce9145674d1d)
14
+
15
+ * Pretend to be `instance_of?(source.class)` [https://github.com/drapergem/draper/commit/30d209f990847e84b221ac798e84b976f5775cc0](https://github.com/drapergem/draper/commit/30d209f990847e84b221ac798e84b976f5775cc0)
16
+
17
+ * Remove security from `Decorator`. Do manual delegation with `:delegate`. [https://github.com/drapergem/draper/commit/c6f8aaa2b2bd4679738050aede2503aa8e9db130](https://github.com/drapergem/draper/commit/c6f8aaa2b2bd4679738050aede2503aa8e9db130)
18
+
19
+ * Add generators for MiniTest. [https://github.com/drapergem/draper/commit/1fac02b65b15e32f06e8292cb858c97cb1c1da2c](https://github.com/drapergem/draper/commit/1fac02b65b15e32f06e8292cb858c97cb1c1da2c)
20
+
21
+ * Test against edge rails. [https://github.com/drapergem/draper/commit/e9b71e3cf55a800b48c083ff257a7c1cbe1b601b](https://github.com/drapergem/draper/commit/e9b71e3cf55a800b48c083ff257a7c1cbe1b601b)
22
+
23
+ ## 1.0.0.beta6
24
+
25
+ * Fix up README to include changes made. [https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6](https://github.com/drapergem/draper/commit/5e6e4d11b1e0c07c12b6b1e87053bc3f50ef2ab6)
26
+
27
+ * `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)
28
+
29
+ * A fix for `Decoratable#==` [https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65](https://github.com/drapergem/draper/commit/e4fa239d84e8e9d6a490d785abb3953acc28fa65)
30
+
31
+ * Ensure we coerce to an array in the right place. [https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095](https://github.com/drapergem/draper/commit/9eb9fc909c372ea1c2392d05594fa75a5c08b095)
32
+
33
+ ## 1.0.0.beta5
34
+
35
+ * Change CollectionDecorator to freeze its collection [https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4](https://github.com/drapergem/draper/commit/04d779615c43580409083a71661489e1bbf91ad4)
36
+
37
+ * Bugfix on `CollectionDecorator#to_s` [https://github.com/drapergem/draper/commit/eefd7d09cac97d531b9235246378c3746d153f08](https://github.com/drapergem/draper/commit/eefd7d09cac97d531b9235246378c3746d153f08)
38
+
39
+ * Upgrade `request_store` dependency to take advantage of a bugfix [https://github.com/drapergem/draper/commit/9f17212fd1fb656ef1314327d60fe45e0acf60a2](https://github.com/drapergem/draper/commit/9f17212fd1fb656ef1314327d60fe45e0acf60a2)
40
+
41
+ ## 1.0.0.beta4
42
+
43
+ * Fixed a race condition with capybara integration. [https://github.com/drapergem/draper/commit/e79464931e7b98c85ed5d78ed9ca38d51f43006e](https://github.com/drapergem/draper/commit/e79464931e7b98c85ed5d78ed9ca38d51f43006e)
44
+
45
+ * `[]` can be decorated again. [https://github.com/drapergem/draper/commit/597fbdf0c80583f5ea6df9f7350fefeaa0cca989](https://github.com/drapergem/draper/commit/597fbdf0c80583f5ea6df9f7350fefeaa0cca989)
46
+
47
+ * `model == decorator` as well as `decorator == model`. [https://github.com/drapergem/draper/commit/46f8a6823c50c13e5c9ab3c07723f335c4e291bc](https://github.com/drapergem/draper/commit/46f8a6823c50c13e5c9ab3c07723f335c4e291bc)
48
+
49
+ * Preliminary Mongoid integration. [https://github.com/drapergem/draper/commit/892d1954202c61fd082a07213c8d4a23560687bc](https://github.com/drapergem/draper/commit/892d1954202c61fd082a07213c8d4a23560687bc)
50
+
51
+ * Add a helper method `sign_in` for devise in decorator specs. [https://github.com/drapergem/draper/commit/66a30093ed4207d02d8fa60bda4df2da091d85a3](https://github.com/drapergem/draper/commit/66a30093ed4207d02d8fa60bda4df2da091d85a3)
52
+
53
+ * Brought back `context`. [https://github.com/drapergem/draper/commit/9609156b997b3a469386eef3a5f043b24d8a2fba](https://github.com/drapergem/draper/commit/9609156b997b3a469386eef3a5f043b24d8a2fba)
54
+
55
+ * Fixed issue where classes were incorrectly being looked up. [https://github.com/drapergem/draper/commit/ee2a015514ff87dfd2158926457e988c2fc3fd79](https://github.com/drapergem/draper/commit/ee2a015514ff87dfd2158926457e988c2fc3fd79)
56
+
57
+ * Integrate RequestStore for per-request storage. [https://github.com/drapergem/draper/commit/fde1cde9adfb856750c1f616d8b62d221ef97fc6](https://github.com/drapergem/draper/commit/fde1cde9adfb856750c1f616d8b62d221ef97fc6)
58
+
59
+ ## 1.0.0.beta3
60
+
61
+ * Relaxed Rails version requirement to 3.0. Support for < 3.2 should be
62
+ considered experimental. Please file bug reports.
63
+
64
+ ## 1.0.0.beta2
65
+
66
+ * `has_finders` is now `decorates_finders`. [https://github.com/drapergem/draper/commit/33f18aa062e0d3848443dbd81047f20d5665579f](https://github.com/drapergem/draper/commit/33f18aa062e0d3848443dbd81047f20d5665579f)
67
+
68
+ * If a finder method is used, and the source class is not set and cannot be inferred, an `UninferrableSourceError` is raised. [https://github.com/drapergem/draper/commit/8ef5bf2f02f7033e3cd4f1f5de7397b02c984fe3](https://github.com/drapergem/draper/commit/8ef5bf2f02f7033e3cd4f1f5de7397b02c984fe3)
69
+
70
+ * Class methods are now properly delegated again. [https://github.com/drapergem/draper/commit/731995a5feac4cd06cf9328d2892c0eca9992db6](https://github.com/drapergem/draper/commit/731995a5feac4cd06cf9328d2892c0eca9992db6)
71
+
72
+ * We no longer `respond_to?` private methods on the source. [https://github.com/drapergem/draper/commit/18ebac81533a6413aa20a3c26f23e91d0b12b031](https://github.com/drapergem/draper/commit/18ebac81533a6413aa20a3c26f23e91d0b12b031)
73
+
74
+ * Rails versioning relaxed to support Rails 4 [https://github.com/drapergem/draper/commit/8bfd393b5baa7aa1488076a5e2cb88648efaa815](https://github.com/drapergem/draper/commit/8bfd393b5baa7aa1488076a5e2cb88648efaa815)
75
+
76
+ ## 1.0.0.beta1
77
+
78
+ * Renaming `Draper::Base` to `Draper::Decorator`. This is the most significant
79
+ change you'll need to upgrade your application. [https://github.com/drapergem/draper/commit/025742cb3b295d259cf0ecf3669c24817d6f2df1](https://github.com/drapergem/draper/commit/025742cb3b295d259cf0ecf3669c24817d6f2df1)
80
+ * Added an internal Rails application for integration tests. This won't affect
81
+ your application, but we're now running a set of Cucumber tests inside of a
82
+ Rails app in both development and production mode to help ensure that we
83
+ don't make changes that break Draper. [https://github.com/drapergem/draper/commit/90a4859085cab158658d23d77cd3108b6037e36f](https://github.com/drapergem/draper/commit/90a4859085cab158658d23d77cd3108b6037e36f)
84
+ * Add `#decorated?` method. This gives us a free RSpec matcher,
85
+ `be_decorated`. [https://github.com/drapergem/draper/commit/834a6fd1f24b5646c333a04a99fe9846a58965d6](https://github.com/drapergem/draper/commit/834a6fd1f24b5646c333a04a99fe9846a58965d6)
86
+ * `#decorates` is no longer needed inside your models, and should be removed.
87
+ Decorators automatically infer the class they decorate. [https://github.com/drapergem/draper/commit/e1214d97b62f2cab45227cc650029734160dcdfe](https://github.com/drapergem/draper/commit/e1214d97b62f2cab45227cc650029734160dcdfe)
88
+ * Decorators do not automatically come with 'finders' by default. If you'd like
89
+ to use `SomeDecorator.find(1)`, for example, simply add `#has_finders` to
90
+ the decorator to include them. [https://github.com/drapergem/draper/commit/42b6f78fda4f51845dab4d35da68880f1989d178](https://github.com/drapergem/draper/commit/42b6f78fda4f51845dab4d35da68880f1989d178)
91
+ * To refer to the object being decorated, `#source` is now the preferred
92
+ method. [https://github.com/drapergem/draper/commit/1e84fcb4a0eab0d12f5feda6886ce1caa239cb16](https://github.com/drapergem/draper/commit/1e84fcb4a0eab0d12f5feda6886ce1caa239cb16)
93
+ * `ActiveModel::Serialization` is included in Decorators if you've requred
94
+ `ActiveModel::Serializers`, so that decorators can be serialized. [https://github.com/drapergem/draper/commit/c4b352799067506849abcbf14963ea36abda301c](https://github.com/drapergem/draper/commit/c4b352799067506849abcbf14963ea36abda301c)
95
+ * Properly support Test::Unit [https://github.com/drapergem/draper/commit/087e134ed0885ec11325ffabe8ab2bebef77a33a](https://github.com/drapergem/draper/commit/087e134ed0885ec11325ffabe8ab2bebef77a33a)
96
+
97
+ And many small bug fixes and refactorings.
98
+
99
+ ## 0.18.0
100
+
101
+ * [Adds the ability to decorate an enumerable proxy](https://github.com/drapergem/draper/commit/67c7125192740a7586a3a635acd735ae01b97837)
102
+
103
+ * Many bug fixes.
104
+
105
+ * Last version of Draper in the 0.x series.
106
+
107
+ ## 0.17.0
108
+
109
+ * [Fix earlier fix of `view_context` priming](https://github.com/drapergem/draper/commit/5da44336)
110
+ * [Add `denies_all`](https://github.com/drapergem/draper/commit/148e732)
111
+ * [Properly proxy associations with regard to `find`](https://github.com/drapergem/draper/commit/d46d19205e)
112
+
113
+ ## 0.16.0
114
+
115
+ * [Automatically prime `view_context`](https://github.com/drapergem/draper/commit/057ab4e8)
116
+ * [Fixed bug where rspec eq matchers didn't work]((https://github.com/drapergem/draper/commit/57617b)
117
+ * [Sequel ORM support](https://github.com/drapergem/draper/commit/7d4942)
118
+ * Fixed issues with newer minitest
119
+ * [Changed the way the `view_context` gets set](https://github.com/drapergem/draper/commit/0b03d9c)
120
+
121
+ ## 0.15.0
122
+
123
+ * Proper minitest integration
124
+ * [We can properly decorate scoped associations](https://github.com/drapergem/draper/issues/223)
125
+ * [Fixed awkward eager loading](https://github.com/drapergem/draper/commit/7dc3510b)
126
+
127
+ ## 0.14.0
128
+
129
+ * [Properly prime the view context in Rails Console](https://github.com/drapergem/draper/commit/738074f)
130
+ * Make more gems development requirements only
131
+
132
+ ## 0.13.0
133
+
134
+ * Upgraded all dependencies
135
+ * Dropped support for Rubies < 1.9.3
136
+ * `#to_model` has been renamed to `#wrapped_object`
137
+ * Allow proper overriding of special `ActiveModel` methods
138
+
139
+ ## 0.12.3
140
+
141
+ * [Fix i18n issue](https://github.com/drapergem/draper/issues/202)
142
+
143
+ ## 0.12.2
144
+
145
+ * Fix bug with initializing ammeter
146
+ * Some gems are now development only in the gemspec
147
+ * Fix bug where generated models were still inheriting from `ApplicationDecorator`
148
+
149
+ ## 0.12.0
150
+
151
+ * Added Changelog
152
+ * [Prevented double decoration](https://github.com/drapergem/draper/issues/173)
153
+ * [`ActiveModel::Errors` support](https://github.com/drapergem/draper/commit/19496f0c)
154
+ * [Fixed autoloading issue](https://github.com/drapergem/draper/issues/188)
155
+ * [Re-did generators](https://github.com/drapergem/draper/commit/9155e58f)
156
+ * [Added capybara integration](https://github.com/drapergem/draper/commit/57c8678e)
157
+ * Fixed a few bugs with the `DecoratedEnumerableProxy`
158
+
159
+ ## 0.11.1
160
+
161
+ * [Fixed regression, we don't want to introduce a hard dependency on Rails](https://github.com/drapergem/draper/issues/107)
data/CONTRIBUTING.md ADDED
@@ -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/Gemfile CHANGED
@@ -1,3 +1,23 @@
1
1
  source :rubygems
2
2
 
3
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", "~> 1.2.2.1"
12
+ end
13
+
14
+ case ENV["RAILS_VERSION"]
15
+ when "master"
16
+ gem "rails", github: "rails/rails"
17
+ when "3.2", nil
18
+ gem "rails", "~> 3.2.0"
19
+ when "3.1"
20
+ gem "rails", "~> 3.1.0"
21
+ when "3.0"
22
+ gem "rails", "~> 3.0.0"
23
+ 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,324 @@
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
+ delegate_all
39
+
40
+ def publication_status
41
+ if published?
42
+ "Published at #{published_at}"
43
+ else
44
+ "Unpublished"
45
+ end
46
+ end
47
+
48
+ def published_at
49
+ source.published_at.strftime("%A, %B %e")
50
+ end
51
+ end
52
+ ```
53
+
54
+ Notice that the `published?` method can be called even though `ArticleDecorator` doesn't define it - thanks to `delegate_all`, the decorator delegates missing 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.
55
+
56
+ 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.
57
+
58
+ Decorators are the ideal place to:
59
+ * format dates and times using `strftime`,
60
+ * define commonly-used representations of an object, like a `name` method that combines `first_name` and `last_name` attributes,
61
+ * mark up attributes with a little semantic HTML, like turning a `url` field into a hyperlink.
62
+
63
+
64
+ ## Installation
65
+
66
+ Add Draper to your Gemfile:
67
+
68
+ ```ruby
69
+ gem 'draper', '~> 1.0.0.beta6'
70
+ ```
71
+
72
+ And run `bundle install` within your app's directory.
73
+
74
+
75
+ ## Writing decorators
76
+
77
+ Decorators inherit from `Draper::Decorator`, live in your `app/decorators` directory, and are named for the model that they decorate:
78
+
79
+ ```ruby
80
+ # app/decorators/article_decorator.rb
81
+ class ArticleDecorator < Draper::Decorator
82
+ # ...
83
+ end
84
+ ```
85
+
86
+ ### Generators
87
+
88
+ 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`.
89
+
90
+ ### Accessing helpers
91
+
92
+ 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:
93
+
94
+ ```ruby
95
+ class ArticleDecorator < Draper::Decorator
96
+ def emphatic
97
+ helpers.content_tag(:strong, "Awesome")
98
+ end
99
+ end
100
+ ```
101
+
102
+ 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).
103
+
104
+ ### Accessing the model
105
+
106
+ 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`):
107
+
108
+ ```ruby
109
+ class ArticleDecorator < Draper::Decorator
110
+ delegate_all
111
+
112
+ def published_at
113
+ source.published_at.strftime("%A, %B %e")
114
+ end
115
+ end
116
+ ```
117
+
118
+
119
+ ## Decorating
120
+
121
+ ### Single objects
122
+
123
+ 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:
124
+
125
+ ```ruby
126
+ @article = Article.first.decorate
127
+ ```
128
+
129
+ 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:
130
+
131
+ ```ruby
132
+ @widget = ProductDecorator.new(Widget.first)
133
+ # or, equivalently
134
+ @widget = ProductDecorator.decorate(Widget.first)
135
+ ```
136
+
137
+ ### Collections
138
+
139
+ If you have a whole bunch of objects, you can decorate them all in one fell swoop:
140
+
141
+ ```ruby
142
+ @articles = ArticleDecorator.decorate_collection(Article.all)
143
+ # or, for scopes (but not `all`)
144
+ @articles = Article.popular.decorate
145
+ ```
146
+
147
+ If you want to add methods to your decorated collection (for example, for pagination), you can subclass `Draper::CollectionDecorator`:
148
+
149
+ ```ruby
150
+ # app/decorators/articles_decorator.rb
151
+ class ArticlesDecorator < Draper::CollectionDecorator
152
+ def page_number
153
+ 42
154
+ end
155
+ end
156
+
157
+ # elsewhere...
158
+ @articles = ArticlesDecorator.new(Article.all)
159
+ # or, equivalently
160
+ @articles = ArticlesDecorator.decorate(Article.all)
161
+ ```
162
+
163
+ 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.
164
+
165
+ 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 the `source`:
166
+
167
+ ```ruby
168
+ class PaginatingDecorator < Draper::CollectionDecorator
169
+ delegate :current_page, :total_pages, :limit_value
170
+ end
171
+ ```
172
+
173
+ 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.
174
+
175
+ ### Handy shortcuts
176
+
177
+ You can automatically decorate associated models:
178
+
179
+ ```ruby
180
+ class ArticleDecorator < Draper::Decorator
181
+ decorates_association :author
182
+ end
183
+ ```
184
+
185
+ And, if you want, you can add decorated finder methods:
186
+
187
+ ```ruby
188
+ class ArticleDecorator < Draper::Decorator
189
+ decorates_finders
190
+ end
191
+ ```
192
+
193
+ so that you can do:
194
+
195
+ ```ruby
196
+ @article = ArticleDecorator.find(params[:id])
197
+ ```
198
+
199
+
200
+ ## Testing
201
+
202
+ Draper supports RSpec, MiniTest::Rails, and Test::Unit, and will add the appropriate tests when you generate a decorator.
203
+
204
+ ### RSpec
205
+
206
+ Your specs should live in `spec/decorators` (if not, you need to tag them with `type: :decorator`).
207
+
208
+ In controller specs, you might want to check whether your instance variables are being decorated properly. You can use the handy predicate matchers:
209
+
210
+ ```ruby
211
+ assigns(:article).should be_decorated
212
+ # or, if you want to be more specific
213
+ assigns(:article).should be_decorated_with ArticleDecorator
214
+ ```
215
+
216
+ Note that `model.decorate == model`, so your existing specs shouldn't break when you add the decoration.
217
+
218
+ Spork users should `require 'draper/test/rspec_integration'` in the `Spork.prefork` block.
219
+
220
+
221
+ ## Advanced usage
222
+
223
+ ### `ApplicationDecorator`
224
+
225
+ If you need common methods in your decorators, you can create an `ApplicationDecorator`:
226
+
227
+ ```ruby
228
+ # app/decorators/application_decorator.rb
229
+ class ApplicationDecorator < Draper::Decorator
230
+ # ...
231
+ end
232
+ ```
233
+
234
+ and inherit from it instead of directly from `Draper::Decorator`.
235
+
236
+ ### Enforcing an interface between controllers and views
237
+
238
+ The `delegate_all` call at the top of your decorator means that all missing methods will delegated to the source. If you want to strictly control which methods are called in your views, you can choose to only delegate certain methods.
239
+
240
+ ```ruby
241
+ class ArticleDecorator < Draper::Decorator
242
+ delegate :title, :author
243
+ end
244
+ ```
245
+
246
+ As mentioned above for `CollectionDecorator`, the `delegate` method defaults to using `:source` if the `:to` option is omitted.
247
+
248
+
249
+ ### Adding context
250
+
251
+ 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
252
+
253
+ ```ruby
254
+ Article.first.decorate(context: {role: :admin})
255
+ ```
256
+
257
+ The value passed to the `:context` option is then available in the decorator through the `context` method.
258
+
259
+ 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:
260
+
261
+ ```ruby
262
+ class ArticleDecorator < Draper::Decorator
263
+ decorates_association :author, context: {foo: "bar"}
264
+ end
265
+ ```
266
+
267
+ or, if you simply want to modify the parent's context, use a lambda that takes a hash and returns a new hash:
268
+
269
+ ```ruby
270
+ class ArticleDecorator < Draper::Decorator
271
+ decorates_association :author,
272
+ context: ->(parent_context){ parent_context.merge(foo: "bar") }
273
+ end
274
+ ```
275
+
276
+ ### Specifying decorators
277
+
278
+ 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:
279
+
280
+ ```ruby
281
+ class ArticleDecorator < Draper::Decorator
282
+ decorates_association :author, with: FancyPersonDecorator
283
+ end
284
+ ```
285
+
286
+ 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.
287
+
288
+ ### Scoping associations
289
+
290
+ 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:
291
+
292
+ ```ruby
293
+ class ArticleDecorator < Draper::Decorator
294
+ decorates_association :comments, scope: :recent
295
+ end
296
+ ```
297
+
298
+ ### Breaking with convention
299
+
300
+ 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:
301
+
302
+ ```ruby
303
+ class MySpecialArticleDecorator < Draper::Decorator
304
+ decorates :article
305
+ end
306
+ ```
307
+
308
+ You don't need to worry about this if you don't want to proxy class methods.
309
+
310
+ ### Making models decoratable
311
+
312
+ 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.
313
+
314
+
315
+ ## Contributors
316
+
317
+ 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).
318
+
319
+ ### Core Team
320
+
321
+ * Jeff Casimir (jeff@jumpstartlab.com)
322
+ * Steve Klabnik (steve@jumpstartlab.com)
323
+ * Vasiliy Ermolovich
324
+ * Andrew Haines
data/Rakefile CHANGED
@@ -1,47 +1,68 @@
1
- require 'bundler/gem_tasks'
2
1
  require 'rake'
2
+ require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
4
 
5
- RCOV = RUBY_VERSION.to_f == 1.8
5
+ def run_in_dummy_app(command)
6
+ success = system("cd spec/dummy && #{command}")
7
+ raise "#{command} failed" unless success
8
+ end
9
+
10
+ task "default" => "ci"
11
+
12
+ desc "Run all tests for CI"
13
+ task "ci" => "spec"
6
14
 
7
- namespace :spec do
15
+ desc "Run all specs"
16
+ task "spec" => "spec:all"
8
17
 
9
- RSpec::Core::RakeTask.new(:coverage) do |t|
10
- t.pattern = 'spec/**/*_spec.rb'
18
+ namespace "spec" do
19
+ task "all" => ["draper", "generators", "integration"]
11
20
 
12
- if RCOV
13
- t.rcov = true
14
- t.rcov_opts = '--exclude osx\/objc,spec,gems\/'
21
+ def spec_task(name)
22
+ desc "Run #{name} specs"
23
+ RSpec::Core::RakeTask.new(name) do |t|
24
+ t.pattern = "spec/#{name}/**/*_spec.rb"
15
25
  end
16
26
  end
17
27
 
18
- RSpec::Core::RakeTask.new(:normal) do |t|
19
- t.pattern ='spec/**/*_spec.rb'
20
- t.rcov = false
21
- end
28
+ spec_task "draper"
29
+ spec_task "generators"
22
30
 
23
- namespace :coverage do
24
- desc "Cleanup coverage data"
25
- task :cleanup do
26
- rm_rf 'coverage.data'
27
- rm_rf 'coverage'
28
- end
31
+ desc "Run integration specs"
32
+ task "integration" => ["db:setup", "integration:all"]
29
33
 
30
- desc "Browse the code coverage report."
31
- task :report => ["spec:coverage:cleanup", "spec:coverage"] do
32
- if RCOV
33
- require "launchy"
34
- Launchy.open("coverage/index.html")
35
- else
36
- require 'cover_me'
37
- CoverMe.complete!
34
+ namespace "integration" do
35
+ task "all" => ["development", "production", "test"]
36
+
37
+ ["development", "production"].each do |environment|
38
+ task environment do
39
+ Rake::Task["spec:integration:run"].execute environment
38
40
  end
39
41
  end
40
- end
41
42
 
42
- end
43
+ task "run" do |t, environment|
44
+ puts "Running integration specs in #{environment}"
45
+
46
+ ENV["RAILS_ENV"] = environment
47
+ success = system("rspec spec/integration")
43
48
 
44
- desc "RSpec tests"
45
- task "spec" => "spec:normal"
49
+ raise "Integration specs failed in #{environment}" unless success
50
+ end
46
51
 
47
- task "default" => "spec"
52
+ task "test" do
53
+ puts "Running rake in dummy app"
54
+ ENV["RAILS_ENV"] = "test"
55
+ run_in_dummy_app "rake"
56
+ end
57
+ end
58
+ end
59
+
60
+ namespace "db" do
61
+ desc "Set up databases for integration testing"
62
+ task "setup" do
63
+ run_in_dummy_app "rm -f db/*.sqlite3"
64
+ run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed"
65
+ run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed"
66
+ run_in_dummy_app "RAILS_ENV=test rake db:schema:load"
67
+ end
68
+ end