draper 3.0.1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +16 -4
  3. data/CHANGELOG.md +30 -0
  4. data/Gemfile +10 -2
  5. data/README.md +34 -5
  6. data/draper.gemspec +7 -6
  7. data/lib/draper.rb +1 -0
  8. data/lib/draper/automatic_delegation.rb +2 -0
  9. data/lib/draper/collection_decorator.rb +4 -2
  10. data/lib/draper/configuration.rb +8 -0
  11. data/lib/draper/decoratable.rb +0 -1
  12. data/lib/draper/decorated_association.rb +0 -2
  13. data/lib/draper/decorator.rb +8 -6
  14. data/lib/draper/delegation.rb +1 -1
  15. data/lib/draper/finders.rb +0 -1
  16. data/lib/draper/helper_proxy.rb +0 -1
  17. data/lib/draper/lazy_helpers.rb +0 -2
  18. data/lib/draper/query_methods.rb +23 -0
  19. data/lib/draper/query_methods/load_strategy.rb +21 -0
  20. data/lib/draper/test_case.rb +3 -3
  21. data/lib/draper/undecorate.rb +1 -1
  22. data/lib/draper/version.rb +1 -1
  23. data/lib/draper/view_context/build_strategy.rb +1 -3
  24. data/lib/draper/view_helpers.rb +4 -4
  25. data/spec/draper/collection_decorator_spec.rb +0 -2
  26. data/spec/draper/configuration_spec.rb +32 -8
  27. data/spec/draper/decoratable_spec.rb +1 -3
  28. data/spec/draper/decorated_association_spec.rb +0 -2
  29. data/spec/draper/decorator_spec.rb +32 -0
  30. data/spec/draper/draper_spec.rb +1 -0
  31. data/spec/draper/factory_spec.rb +0 -4
  32. data/spec/draper/query_methods/load_strategy_spec.rb +26 -0
  33. data/spec/draper/query_methods_spec.rb +70 -0
  34. data/spec/dummy/app/assets/config/manifest.js +3 -0
  35. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  36. data/spec/generators/controller/controller_generator_spec.rb +1 -0
  37. data/spec/integration/integration_spec.rb +1 -0
  38. data/spec/spec_helper.rb +7 -0
  39. metadata +39 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cc23ed0f81227aa958510ecb67676453d97b40cc
4
- data.tar.gz: ed8a159772bafb7df87cc5c767757e8b697f55b3
2
+ SHA256:
3
+ metadata.gz: a90d57e1df50336b735a6f16cc119a558ee8472b70bd9748c59f25b70afc319a
4
+ data.tar.gz: a4510f40a8afac7976e7afea4659d8e1c3eb981c005bc4046660b4d110874df7
5
5
  SHA512:
6
- metadata.gz: 466c609e53461c1e1f260f9e2bf6a0eb522238e5bdae8a2f37a88c1cd22d22665bf18293ba497f9f64d27faa0e12f0dbb9e3d906bae569f324c6124c02ad6b7e
7
- data.tar.gz: db229fe0cf5daf0acc2e52834c599a21e9ba85782458a8fd3a0520574e77f1857ec30c57c81dceb75561016170c941f8da3593d3a58fd395137c01ce57322cf0
6
+ metadata.gz: bee71715525fdc2de5336c399b1dfe4a774d9fd68071bb01468c8f31712504cf6af8beecf1f4facf4ea25ef5bcad7a14b2e2f12bec12f7b6f1e55c04318c4f7a
7
+ data.tar.gz: 8f013f4b36efa767ada401a979e5bf10e28f068997d24fd8035e4025254300709eb7c2e80c5f0498cc49c176b7561fbff48e2cc03f39b418f368c3266bf30359
@@ -1,16 +1,28 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=b7ba588af2a540fa96c267b3655a2afe31ea29976dc25905a668dd28d5e88915
4
+
1
5
  language: ruby
2
- sudo: false
3
6
  cache: bundler
4
7
 
5
8
  services:
6
9
  - mongodb
7
10
 
8
11
  rvm:
9
- - 2.2.7
10
- - 2.3.4
11
- - 2.4.1
12
+ - 2.4.9
13
+ - 2.5.7
14
+ - 2.6.5
15
+ - 2.7.0
12
16
  - ruby-head
13
17
 
14
18
  matrix:
15
19
  allow_failures:
16
20
  - rvm: ruby-head
21
+
22
+ before_script:
23
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
24
+ - chmod +x ./cc-test-reporter
25
+ - ./cc-test-reporter before-build
26
+
27
+ after_script:
28
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -1,5 +1,35 @@
1
1
  # Draper Changelog
2
2
 
3
+ ## 4.0.1 - 2020-03-25
4
+
5
+ ### Fixes
6
+ * Check only object's private methods when preventing delegation [#875](https://github.com/drapergem/draper/pull/875)
7
+
8
+ ### Other Changes
9
+ * Use `alias` over `alias_method` [#877](https://github.com/drapergem/draper/pull/877)
10
+
11
+ ## 4.0.0 - 2020-02-05
12
+
13
+ ### Breaking Changes
14
+ * Drop support for Ruby < 2.4 [#852](https://github.com/drapergem/draper/pull/852), [#872](https://github.com/drapergem/draper/pull/872)
15
+ * Don't delegate public methods overridden by a private method in the decorator [#849](https://github.com/drapergem/draper/pull/849)
16
+
17
+ ### Fixes
18
+ * Add preservation of decorator options on QueryMethods [#868](https://github.com/drapergem/draper/pull/868)
19
+ * Add `#respond_to_missing?` to `CollectionDecorator` so it correctly responds to ORM methods [#850](https://github.com/drapergem/draper/pull/850)
20
+ * Fix deprecation warning with the new Rails 6 `ActionView::Base` constructor [#866](https://github.com/drapergem/draper/pull/866)
21
+ * Fix deprecation warning with Ruby 2.7 [#870](https://github.com/drapergem/draper/pull/870)
22
+
23
+ ### Other Changes
24
+ * Add SimpleCov for code coverage analysis [#851](https://github.com/drapergem/draper/pull/851)
25
+ * Update RSpec syntax in the README [#855](https://github.com/drapergem/draper/pull/855)
26
+ * Update continuous integration configuration [#861](https://github.com/drapergem/draper/pull/861), [#862](https://github.com/drapergem/draper/pull/862), [#863](https://github.com/drapergem/draper/pull/863), [#872](https://github.com/drapergem/draper/pull/872)
27
+
28
+ ## 3.1.0
29
+ * Rails 6 support [#841](https://github.com/drapergem/draper/pull/841)
30
+ * Include ORM query methods in `CollectionDecorator` (e.g. `includes`) [#845](https://github.com/drapergem/draper/pull/845)
31
+ * Document the fix for view context leaking in specs [#847](https://github.com/drapergem/draper/pull/847)
32
+
3
33
  ## 3.0.1
4
34
  * Let `decorator_class` infer anonymous class decorator from superclass [#820](https://github.com/drapergem/draper/pull/820)
5
35
  * When inferring decorator fails, show original class instead of `ActiveRecord::Base` [#821](https://github.com/drapergem/draper/pull/821)
data/Gemfile CHANGED
@@ -3,7 +3,11 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  platforms :ruby do
6
- gem "sqlite3"
6
+ if RUBY_VERSION >= "2.5.0"
7
+ gem 'sqlite3'
8
+ else
9
+ gem 'sqlite3', '~> 1.3.6'
10
+ end
7
11
  end
8
12
 
9
13
  platforms :jruby do
@@ -11,5 +15,9 @@ platforms :jruby do
11
15
  gem "activerecord-jdbcsqlite3-adapter"
12
16
  end
13
17
 
14
- gem "rails", "~> 5.0"
18
+ if RUBY_VERSION >= "2.5.0"
19
+ gem "rails", "~> 6.0"
20
+ else
21
+ gem "rails", "~> 5.0"
22
+ end
15
23
  gem "mongoid", github: "mongodb/mongoid"
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![TravisCI Build Status](https://travis-ci.org/drapergem/draper.svg?branch=master)](http://travis-ci.org/drapergem/draper)
4
4
  [![Code Climate](https://codeclimate.com/github/drapergem/draper.svg)](https://codeclimate.com/github/drapergem/draper)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/0d40c43951d516bf6985/test_coverage)](https://codeclimate.com/github/drapergem/draper/test_coverage)
5
6
  [![Inline docs](http://inch-ci.org/github/drapergem/draper.svg?branch=master)](http://inch-ci.org/github/drapergem/draper)
6
7
 
7
8
  Draper adds an object-oriented layer of presentation logic to your Rails
@@ -107,7 +108,7 @@ Decorators are the ideal place to:
107
108
 
108
109
  ## Installation
109
110
 
110
- As of version 3.0.0, Draper is only compatible with Rails 5 / Ruby 2.2 and later. Add Draper to your Gemfile.
111
+ As of version 4.0.0, Draper only officially supports Rails 5.2 / Ruby 2.4 and later. Add Draper to your Gemfile.
111
112
 
112
113
  ```ruby
113
114
  gem 'draper'
@@ -326,6 +327,17 @@ your `ArticleDecorator` and they'll return decorated objects:
326
327
  @article = ArticleDecorator.find(params[:id])
327
328
  ```
328
329
 
330
+ ### Decorated Query Methods
331
+ By default, Draper will decorate all [QueryMethods](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html)
332
+ of ActiveRecord.
333
+ If you're using another ORM, in order to support it, you can tell Draper to use a custom strategy:
334
+
335
+ ```ruby
336
+ Draper.configure do |config|
337
+ config.default_query_methods_strategy = :mongoid
338
+ end
339
+ ```
340
+
329
341
  ### When to Decorate Objects
330
342
 
331
343
  Decorators are supposed to behave very much like the models they decorate, and
@@ -368,7 +380,7 @@ can continue to use the `@article` instance variable to manipulate the model -
368
380
  for example, `@article.comments.build` to add a new blank comment for a form.
369
381
 
370
382
  ## Configuration
371
- Draper works out the box well, but also provides a hook for you to configure its
383
+ Draper works out the box well, but also provides a hook for you to configure its
372
384
  default functionality. For example, Draper assumes you have a base `ApplicationController`.
373
385
  If your base controller is named something different (e.g. `BaseController`),
374
386
  you can tell Draper to use it by adding the following to an initializer:
@@ -457,12 +469,29 @@ end
457
469
  ```
458
470
 
459
471
  Then you can stub the specific route helper functions you need using your
460
- preferred stubbing technique (this example uses RSpec's `stub` method):
472
+ preferred stubbing technique. This examples uses Rspec currently recommended API
473
+ available in RSpec 3.6+
461
474
 
462
475
  ```ruby
463
- helpers.stub(users_path: '/users')
476
+ without_partial_double_verification do
477
+ allow(helpers).to receive(:users_path).and_return('/users')
478
+ end
479
+ ```
480
+
481
+ ### View context leakage
482
+ As mentioned before, Draper needs to build a view context to access helper methods. In MiniTest, the view context is
483
+ cleared during `before_setup` preventing any view context leakage. In RSpec, the view context is cleared before each
484
+ `decorator`, `controller`, and `mailer` spec. However, if you use decorators in other types of specs
485
+ (e.g. `job`), you may still experience the view context leaking from the previous spec. To solve this, add the
486
+ following to your `spec_helper` for each type of spec you are experiencing the leakage:
487
+
488
+ ```ruby
489
+ config.before(:each, type: :type) { Draper::ViewContext.clear! }
464
490
  ```
465
491
 
492
+ _Note_: The `:type` above is just a placeholder. Replace `:type` with the type of spec you are experiencing
493
+ the leakage from.
494
+
466
495
  ## Advanced usage
467
496
 
468
497
  ### Shared Decorator Methods
@@ -620,7 +649,7 @@ you can include this module manually.
620
649
 
621
650
  ### Active Job Integration
622
651
 
623
- [Active Job](http://edgeguides.rubyonrails.org/active_job_basics.html) allows you to pass ActiveRecord
652
+ [Active Job](http://edgeguides.rubyonrails.org/active_job_basics.html) allows you to pass ActiveRecord
624
653
  objects to background tasks directly and performs the necessary serialization and deserialization. In
625
654
  order to do this, arguments to a background job must implement [Global ID](https://github.com/rails/globalid).
626
655
  Decorated objects implement Global ID by delegating to the object they are decorating. This means
@@ -17,17 +17,18 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.required_ruby_version = '>= 2.2.2'
19
19
 
20
- s.add_dependency 'activesupport', '~> 5.0'
21
- s.add_dependency 'actionpack', '~> 5.0'
22
- s.add_dependency 'request_store', '~> 1.0'
23
- s.add_dependency 'activemodel', '~> 5.0'
24
- s.add_dependency 'activemodel-serializers-xml', '~> 1.0'
20
+ s.add_dependency 'activesupport', '>= 5.0'
21
+ s.add_dependency 'actionpack', '>= 5.0'
22
+ s.add_dependency 'request_store', '>= 1.0'
23
+ s.add_dependency 'activemodel', '>= 5.0'
24
+ s.add_dependency 'activemodel-serializers-xml', '>= 1.0'
25
25
 
26
26
  s.add_development_dependency 'ammeter'
27
27
  s.add_development_dependency 'rake'
28
28
  s.add_development_dependency 'rspec-rails'
29
29
  s.add_development_dependency 'minitest-rails'
30
30
  s.add_development_dependency 'capybara'
31
- s.add_development_dependency 'active_model_serializers', '~> 0.10'
31
+ s.add_development_dependency 'active_model_serializers', '>= 0.10'
32
32
  s.add_development_dependency 'rubocop'
33
+ s.add_development_dependency 'simplecov'
33
34
  end
@@ -23,6 +23,7 @@ require 'draper/factory'
23
23
  require 'draper/decorated_association'
24
24
  require 'draper/helper_support'
25
25
  require 'draper/view_context'
26
+ require 'draper/query_methods'
26
27
  require 'draper/collection_decorator'
27
28
  require 'draper/undecorate'
28
29
  require 'draper/decorates_assigned'
@@ -20,6 +20,8 @@ module Draper
20
20
 
21
21
  # @private
22
22
  def delegatable?(method)
23
+ return if private_methods(false).include?(method)
24
+
23
25
  object.respond_to?(method)
24
26
  end
25
27
 
@@ -2,6 +2,7 @@ module Draper
2
2
  class CollectionDecorator
3
3
  include Enumerable
4
4
  include Draper::ViewHelpers
5
+ include Draper::QueryMethods
5
6
  extend Draper::Delegation
6
7
 
7
8
  # @return the collection being decorated.
@@ -34,7 +35,7 @@ module Draper
34
35
  end
35
36
 
36
37
  class << self
37
- alias_method :decorate, :new
38
+ alias :decorate :new
38
39
  end
39
40
 
40
41
  # @return [Array] the decorated items.
@@ -58,11 +59,12 @@ module Draper
58
59
  true
59
60
  end
60
61
 
61
- alias_method :decorated_with?, :instance_of?
62
+ alias :decorated_with? :instance_of?
62
63
 
63
64
  def kind_of?(klass)
64
65
  decorated_collection.kind_of?(klass) || super
65
66
  end
67
+
66
68
  alias_method :is_a?, :kind_of?
67
69
 
68
70
  def replace(other)
@@ -11,5 +11,13 @@ module Draper
11
11
  def default_controller=(controller)
12
12
  @@default_controller = controller
13
13
  end
14
+
15
+ def default_query_methods_strategy
16
+ @@default_query_methods_strategy ||= :active_record
17
+ end
18
+
19
+ def default_query_methods_strategy=(strategy)
20
+ @@default_query_methods_strategy = strategy
21
+ end
14
22
  end
15
23
  end
@@ -48,7 +48,6 @@ module Draper
48
48
  end
49
49
 
50
50
  module ClassMethods
51
-
52
51
  # Decorates a collection of objects. Used at the end of a scope chain.
53
52
  #
54
53
  # @example
@@ -1,7 +1,6 @@
1
1
  module Draper
2
2
  # @private
3
3
  class DecoratedAssociation
4
-
5
4
  def initialize(owner, association, options)
6
5
  options.assert_valid_keys(:with, :scope, :context)
7
6
 
@@ -30,6 +29,5 @@ module Draper
30
29
 
31
30
  @decorated = factory.decorate(associated, context_args: owner.context)
32
31
  end
33
-
34
32
  end
35
33
  end
@@ -12,7 +12,8 @@ module Draper
12
12
 
13
13
  # @return the object being decorated.
14
14
  attr_reader :object
15
- alias_method :model, :object
15
+
16
+ alias :model :object
16
17
 
17
18
  # @return [Hash] extra data to be used in user-defined methods.
18
19
  attr_accessor :context
@@ -36,7 +37,7 @@ module Draper
36
37
  end
37
38
 
38
39
  class << self
39
- alias_method :decorate, :new
40
+ alias :decorate :new
40
41
  end
41
42
 
42
43
  # Automatically delegates instance methods to the source object. Class
@@ -190,7 +191,8 @@ module Draper
190
191
  def kind_of?(klass)
191
192
  super || object.kind_of?(klass)
192
193
  end
193
- alias_method :is_a?, :kind_of?
194
+
195
+ alias :is_a? :kind_of?
194
196
 
195
197
  # Checks if `self.instance_of?(klass)` or `object.instance_of?(klass)`
196
198
  #
@@ -225,7 +227,7 @@ module Draper
225
227
  # @return [Class] the class created by {decorate_collection}.
226
228
  def self.collection_decorator_class
227
229
  name = collection_decorator_name
228
- name_constant = name && name.safe_constantize
230
+ name_constant = name&.safe_constantize
229
231
 
230
232
  name_constant || Draper::CollectionDecorator
231
233
  end
@@ -248,7 +250,7 @@ module Draper
248
250
 
249
251
  def self.inferred_object_class
250
252
  name = object_class_name
251
- name_constant = name && name.safe_constantize
253
+ name_constant = name&.safe_constantize
252
254
  return name_constant unless name_constant.nil?
253
255
 
254
256
  raise Draper::UninferrableObjectError.new(self)
@@ -256,7 +258,7 @@ module Draper
256
258
 
257
259
  def self.collection_decorator_name
258
260
  singular = object_class_name
259
- plural = singular && singular.pluralize
261
+ plural = singular&.pluralize
260
262
 
261
263
  "#{plural}Decorator" unless plural == singular
262
264
  end
@@ -7,7 +7,7 @@ module Draper
7
7
  # @return [void]
8
8
  def delegate(*methods)
9
9
  options = methods.extract_options!
10
- super *methods, options.reverse_merge(to: :object)
10
+ super(*methods, **options.reverse_merge(to: :object))
11
11
  end
12
12
  end
13
13
  end
@@ -3,7 +3,6 @@ module Draper
3
3
  # do not have to extend this module directly; it is extended by
4
4
  # {Decorator.decorates_finders}.
5
5
  module Finders
6
-
7
6
  def find(id, options = {})
8
7
  decorate(object_class.find(id), options)
9
8
  end
@@ -2,7 +2,6 @@ module Draper
2
2
  # Provides access to helper methods - both Rails built-in helpers, and those
3
3
  # defined in your application.
4
4
  class HelperProxy
5
-
6
5
  # @overload initialize(view_context)
7
6
  def initialize(view_context)
8
7
  @view_context = view_context
@@ -3,13 +3,11 @@ module Draper
3
3
  # so that you can stop typing `h.` everywhere, at the cost of mixing in a
4
4
  # bazillion methods.
5
5
  module LazyHelpers
6
-
7
6
  # Sends missing methods to the {HelperProxy}.
8
7
  def method_missing(method, *args, &block)
9
8
  helpers.send(method, *args, &block)
10
9
  rescue NoMethodError
11
10
  super
12
11
  end
13
-
14
12
  end
15
13
  end
@@ -0,0 +1,23 @@
1
+ require_relative 'query_methods/load_strategy'
2
+
3
+ module Draper
4
+ module QueryMethods
5
+ # Proxies missing query methods to the source class if the strategy allows.
6
+ def method_missing(method, *args, &block)
7
+ return super unless strategy.allowed? method
8
+
9
+ object.send(method, *args, &block).decorate(with: decorator_class, context: context)
10
+ end
11
+
12
+ def respond_to_missing?(method, include_private = false)
13
+ strategy.allowed?(method) || super
14
+ end
15
+
16
+ private
17
+
18
+ # Configures the strategy used to proxy the query methods, which defaults to `:active_record`.
19
+ def strategy
20
+ @strategy ||= LoadStrategy.new(Draper.default_query_methods_strategy)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Draper
2
+ module QueryMethods
3
+ module LoadStrategy
4
+ def self.new(name)
5
+ const_get(name.to_s.camelize).new
6
+ end
7
+
8
+ class ActiveRecord
9
+ def allowed?(method)
10
+ ::ActiveRecord::Relation::VALUE_METHODS.include? method
11
+ end
12
+ end
13
+
14
+ class Mongoid
15
+ def allowed?(method)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -3,9 +3,9 @@ module Draper
3
3
 
4
4
  class TestCase < ::ActiveSupport::TestCase
5
5
  module ViewContextTeardown
6
- def teardown
7
- super
6
+ def before_setup
8
7
  Draper::ViewContext.clear!
8
+ super
9
9
  end
10
10
  end
11
11
 
@@ -21,7 +21,7 @@ module Draper
21
21
  end
22
22
 
23
23
  include Draper::ViewHelpers::ClassMethods
24
- alias_method :helper, :helpers
24
+ alias :helper :helpers
25
25
  end
26
26
 
27
27
  include Behavior
@@ -6,7 +6,7 @@ module Draper
6
6
  object
7
7
  end
8
8
  end
9
-
9
+
10
10
  def self.undecorate_chain(object)
11
11
  if object.respond_to?(:decorated?) && object.decorated?
12
12
  undecorate_chain(object.object)
@@ -1,3 +1,3 @@
1
1
  module Draper
2
- VERSION = '3.0.1'
2
+ VERSION = '4.0.1'
3
3
  end
@@ -2,7 +2,6 @@ module Draper
2
2
  module ViewContext
3
3
  # @private
4
4
  module BuildStrategy
5
-
6
5
  def self.new(name, &block)
7
6
  const_get(name.to_s.camelize).new(&block)
8
7
  end
@@ -13,7 +12,7 @@ module Draper
13
12
  end
14
13
 
15
14
  def call
16
- view_context_class.new
15
+ view_context_class.respond_to?(:empty) ? view_context_class.empty : view_context_class.new
17
16
  end
18
17
 
19
18
  private
@@ -51,7 +50,6 @@ module Draper
51
50
  ActionController::TestRequest.method(:create).parameters.first == [:req, :controller_class]
52
51
  end
53
52
  end
54
-
55
53
  end
56
54
  end
57
55
  end
@@ -5,7 +5,6 @@ module Draper
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  module ClassMethods
8
-
9
8
  # Access the helpers proxy to call built-in and user-defined
10
9
  # Rails helpers from a class context.
11
10
  #
@@ -13,8 +12,8 @@ module Draper
13
12
  def helpers
14
13
  Draper::ViewContext.current
15
14
  end
16
- alias_method :h, :helpers
17
15
 
16
+ alias :h :helpers
18
17
  end
19
18
 
20
19
  # Access the helpers proxy to call built-in and user-defined
@@ -24,14 +23,15 @@ module Draper
24
23
  def helpers
25
24
  Draper::ViewContext.current
26
25
  end
27
- alias_method :h, :helpers
26
+
27
+ alias :h :helpers
28
28
 
29
29
  # Alias for `helpers.localize`, since localize is something that's used
30
30
  # quite often. Further aliased to `l` for convenience.
31
31
  def localize(*args)
32
32
  helpers.localize(*args)
33
33
  end
34
- alias_method :l, :localize
35
34
 
35
+ alias :l :localize
36
36
  end
37
37
  end
@@ -7,7 +7,6 @@ module Draper
7
7
 
8
8
  describe "#initialize" do
9
9
  describe "options validation" do
10
-
11
10
  it "does not raise error on valid options" do
12
11
  valid_options = {with: Decorator, context: {}}
13
12
  expect{CollectionDecorator.new([], valid_options)}.not_to raise_error
@@ -287,6 +286,5 @@ module Draper
287
286
  expect(decorator.replace([:foo, :bar])).to be decorator
288
287
  end
289
288
  end
290
-
291
289
  end
292
290
  end
@@ -6,20 +6,44 @@ module Draper
6
6
  Draper.configure { |config| expect(config).to be Draper }
7
7
  end
8
8
 
9
- it 'defaults default_controller to ApplicationController' do
10
- expect(Draper.default_controller).to be ApplicationController
9
+ describe '#default_controller' do
10
+ it 'defaults default_controller to ApplicationController' do
11
+ expect(Draper.default_controller).to be ApplicationController
12
+ end
13
+
14
+ it 'allows customizing default_controller through configure' do
15
+ default = Draper.default_controller
16
+
17
+ Draper.configure do |config|
18
+ config.default_controller = CustomController
19
+ end
20
+
21
+ expect(Draper.default_controller).to be CustomController
22
+
23
+ Draper.default_controller = default
24
+ end
11
25
  end
12
26
 
13
- it 'allows customizing default_controller through configure' do
14
- default = Draper.default_controller
27
+ describe '#default_query_methods_strategy' do
28
+ let!(:default) { Draper.default_query_methods_strategy }
29
+
30
+ subject { Draper.default_query_methods_strategy }
15
31
 
16
- Draper.configure do |config|
17
- config.default_controller = CustomController
32
+ context 'when there is no custom strategy' do
33
+ it { is_expected.to eq(:active_record) }
18
34
  end
19
35
 
20
- expect(Draper.default_controller).to be CustomController
36
+ context 'when using a custom strategy' do
37
+ before do
38
+ Draper.configure do |config|
39
+ config.default_query_methods_strategy = :mongoid
40
+ end
41
+ end
21
42
 
22
- Draper.default_controller = default
43
+ after { Draper.default_query_methods_strategy = default }
44
+
45
+ it { is_expected.to eq(:mongoid) }
46
+ end
23
47
  end
24
48
  end
25
49
  end
@@ -3,7 +3,6 @@ require 'support/shared_examples/decoratable_equality'
3
3
 
4
4
  module Draper
5
5
  describe Decoratable do
6
-
7
6
  describe "#decorate" do
8
7
  it "returns a decorator for self" do
9
8
  product = Product.new
@@ -11,7 +10,7 @@ module Draper
11
10
 
12
11
  expect(decorator).to be_a ProductDecorator
13
12
  expect(decorator.object).to be product
14
- end
13
+ end
15
14
 
16
15
  it "accepts context" do
17
16
  context = {some: "context"}
@@ -232,6 +231,5 @@ module Draper
232
231
  end
233
232
  end
234
233
  end
235
-
236
234
  end
237
235
  end
@@ -2,7 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  module Draper
4
4
  describe DecoratedAssociation do
5
-
6
5
  describe "#initialize" do
7
6
  it "accepts valid options" do
8
7
  valid_options = {with: Decorator, scope: :foo, context: {}}
@@ -79,6 +78,5 @@ module Draper
79
78
  end
80
79
  end
81
80
  end
82
-
83
81
  end
84
82
  end
@@ -664,6 +664,38 @@ module Draper
664
664
  expect{decorator.hello_world}.to raise_error NoMethodError
665
665
  expect(decorator.methods).not_to include :hello_world
666
666
  end
667
+
668
+ context 'when decorator overrides a public method defined on the object with a private' do
669
+ let(:decorator_class) do
670
+ Class.new(Decorator) do
671
+ private
672
+
673
+ def hello_world
674
+ 'hello world'
675
+ end
676
+ end
677
+ end
678
+
679
+ let(:object) { Class.new { def hello_world; end }.new }
680
+
681
+ it 'does not delegate the public method defined on the object' do
682
+ decorator = decorator_class.new(object)
683
+
684
+ expect{ decorator.hello_world }.to raise_error NoMethodError
685
+ end
686
+ end
687
+
688
+ context 'when delegated method has the same name as private method defined on another object' do
689
+ let(:decorator_class) { Class.new(Decorator) }
690
+ let(:object) { Class.new { def print; end }.new }
691
+
692
+ it 'delegates the public method defined on the object' do
693
+ decorator = decorator_class.new(object)
694
+
695
+ # `print` private method is defined on `Object`
696
+ expect{ decorator.print }.not_to raise_error NoMethodError
697
+ end
698
+ end
667
699
  end
668
700
 
669
701
  context ".method_missing" do
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'support/shared_examples/view_helpers'
3
+ SimpleCov.command_name 'test:unit'
3
4
 
4
5
  module Draper
5
6
  describe Draper do
@@ -2,7 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  module Draper
4
4
  describe Factory do
5
-
6
5
  describe "#initialize" do
7
6
  it "accepts valid options" do
8
7
  valid_options = {with: Decorator, context: {foo: "bar"}}
@@ -88,11 +87,9 @@ module Draper
88
87
  end
89
88
  end
90
89
  end
91
-
92
90
  end
93
91
 
94
92
  describe Factory::Worker do
95
-
96
93
  describe "#call" do
97
94
  it "calls the decorator method" do
98
95
  object = double
@@ -246,6 +243,5 @@ module Draper
246
243
  end
247
244
  end
248
245
  end
249
-
250
246
  end
251
247
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ module Draper
5
+ module QueryMethods
6
+ describe LoadStrategy do
7
+ describe '#new' do
8
+ subject { described_class.new(:active_record) }
9
+
10
+ it { is_expected.to be_an_instance_of(LoadStrategy::ActiveRecord) }
11
+ end
12
+ end
13
+
14
+ describe LoadStrategy::ActiveRecord do
15
+ describe '#allowed?' do
16
+ it 'checks whether or not ActiveRecord::Relation::VALUE_METHODS has the given method' do
17
+ allow(::ActiveRecord::Relation::VALUE_METHODS).to receive(:include?)
18
+
19
+ described_class.new.allowed? :foo
20
+
21
+ expect(::ActiveRecord::Relation::VALUE_METHODS).to have_received(:include?).with(:foo)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require_relative '../dummy/app/decorators/post_decorator'
3
+
4
+ Post = Struct.new(:id) { }
5
+
6
+ module Draper
7
+ describe QueryMethods do
8
+ let(:fake_strategy) { instance_double(QueryMethods::LoadStrategy::ActiveRecord) }
9
+
10
+ before { allow(QueryMethods::LoadStrategy).to receive(:new).and_return(fake_strategy) }
11
+
12
+ describe '#method_missing' do
13
+ let(:collection) { [ Post.new, Post.new ] }
14
+ let(:collection_context) { { user: 'foo' } }
15
+ let(:collection_decorator) { PostDecorator.decorate_collection(collection, context: collection_context) }
16
+
17
+ context 'when strategy allows collection to call the method' do
18
+ let(:results) { spy(:results) }
19
+
20
+ before do
21
+ allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(true)
22
+ allow(collection).to receive(:send).with(:some_query_method).and_return(results)
23
+ end
24
+
25
+ it 'calls the method on the collection and decorate it results' do
26
+ collection_decorator.some_query_method
27
+
28
+ expect(results).to have_received(:decorate)
29
+ end
30
+
31
+ it 'calls the method on the collection and keeps the decoration options' do
32
+ collection_decorator.some_query_method
33
+
34
+ expect(results).to have_received(:decorate).with({ context: collection_context, with: PostDecorator })
35
+ end
36
+ end
37
+
38
+ context 'when strategy does not allow collection to call the method' do
39
+ before { allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(false) }
40
+
41
+ it 'raises NoMethodError' do
42
+ expect { collection_decorator.some_query_method }.to raise_exception(NoMethodError)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#respond_to?" do
48
+ let(:collection) { [ Post.new, Post.new ] }
49
+ let(:collection_decorator) { PostDecorator.decorate_collection(collection) }
50
+
51
+ subject { collection_decorator.respond_to?(:some_query_method) }
52
+
53
+ context 'when strategy allows collection to call the method' do
54
+ before do
55
+ allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(true)
56
+ end
57
+
58
+ it { is_expected.to eq(true) }
59
+ end
60
+
61
+ context 'when strategy does not allow collection to call the method' do
62
+ before do
63
+ allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(false)
64
+ end
65
+
66
+ it { is_expected.to eq(false) }
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -3,6 +3,7 @@ require 'dummy/config/environment'
3
3
  require 'ammeter/init'
4
4
  require 'generators/controller_override'
5
5
  require 'generators/rails/decorator_generator'
6
+ SimpleCov.command_name 'test:generator'
6
7
 
7
8
  describe Rails::Generators::ControllerGenerator do
8
9
  destination File.expand_path("../tmp", __FILE__)
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'support/dummy_app'
3
3
  require 'support/matchers/have_text'
4
+ SimpleCov.command_name 'test:integration'
4
5
 
5
6
  app = DummyApp.new(ENV["RAILS_ENV"])
6
7
  spec_types = {
@@ -1,3 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'spec'
4
+ add_group 'Draper', 'lib/draper'
5
+ add_group 'Generators', 'lib/generators'
6
+ end
7
+
1
8
  require 'bundler/setup'
2
9
  require 'draper'
3
10
  require 'action_controller'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draper
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Casimir
@@ -9,76 +9,76 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-10-12 00:00:00.000000000 Z
12
+ date: 2020-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '5.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '5.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: actionpack
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '5.0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '5.0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: request_store
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ">="
47
47
  - !ruby/object:Gem::Version
48
48
  version: '1.0'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '1.0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: activemodel
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - "~>"
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '5.0'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - "~>"
67
+ - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '5.0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: activemodel-serializers-xml
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - "~>"
74
+ - - ">="
75
75
  - !ruby/object:Gem::Version
76
76
  version: '1.0'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - "~>"
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '1.0'
84
84
  - !ruby/object:Gem::Dependency
@@ -155,14 +155,14 @@ dependencies:
155
155
  name: active_model_serializers
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
- - - "~>"
158
+ - - ">="
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0.10'
161
161
  type: :development
162
162
  prerelease: false
163
163
  version_requirements: !ruby/object:Gem::Requirement
164
164
  requirements:
165
- - - "~>"
165
+ - - ">="
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0.10'
168
168
  - !ruby/object:Gem::Dependency
@@ -179,6 +179,20 @@ dependencies:
179
179
  - - ">="
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
+ - !ruby/object:Gem::Dependency
183
+ name: simplecov
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :development
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
182
196
  description: Draper adds an object-oriented layer of presentation logic to your Rails
183
197
  apps.
184
198
  email:
@@ -220,6 +234,8 @@ files:
220
234
  - lib/draper/helper_proxy.rb
221
235
  - lib/draper/helper_support.rb
222
236
  - lib/draper/lazy_helpers.rb
237
+ - lib/draper/query_methods.rb
238
+ - lib/draper/query_methods/load_strategy.rb
223
239
  - lib/draper/railtie.rb
224
240
  - lib/draper/tasks/test.rake
225
241
  - lib/draper/test/devise_helper.rb
@@ -255,6 +271,8 @@ files:
255
271
  - spec/draper/finders_spec.rb
256
272
  - spec/draper/helper_proxy_spec.rb
257
273
  - spec/draper/lazy_helpers_spec.rb
274
+ - spec/draper/query_methods/load_strategy_spec.rb
275
+ - spec/draper/query_methods_spec.rb
258
276
  - spec/draper/undecorate_chain_spec.rb
259
277
  - spec/draper/undecorate_spec.rb
260
278
  - spec/draper/view_context/build_strategy_spec.rb
@@ -262,6 +280,8 @@ files:
262
280
  - spec/draper/view_helpers_spec.rb
263
281
  - spec/dummy/.rspec
264
282
  - spec/dummy/Rakefile
283
+ - spec/dummy/app/assets/config/manifest.js
284
+ - spec/dummy/app/controllers/application_controller.rb
265
285
  - spec/dummy/app/controllers/base_controller.rb
266
286
  - spec/dummy/app/controllers/localized_urls.rb
267
287
  - spec/dummy/app/controllers/posts_controller.rb
@@ -364,8 +384,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
364
384
  - !ruby/object:Gem::Version
365
385
  version: '0'
366
386
  requirements: []
367
- rubyforge_project:
368
- rubygems_version: 2.6.11
387
+ rubygems_version: 3.0.8
369
388
  signing_key:
370
389
  specification_version: 4
371
390
  summary: View Models for Rails
@@ -382,6 +401,8 @@ test_files:
382
401
  - spec/draper/finders_spec.rb
383
402
  - spec/draper/helper_proxy_spec.rb
384
403
  - spec/draper/lazy_helpers_spec.rb
404
+ - spec/draper/query_methods/load_strategy_spec.rb
405
+ - spec/draper/query_methods_spec.rb
385
406
  - spec/draper/undecorate_chain_spec.rb
386
407
  - spec/draper/undecorate_spec.rb
387
408
  - spec/draper/view_context/build_strategy_spec.rb
@@ -389,6 +410,8 @@ test_files:
389
410
  - spec/draper/view_helpers_spec.rb
390
411
  - spec/dummy/.rspec
391
412
  - spec/dummy/Rakefile
413
+ - spec/dummy/app/assets/config/manifest.js
414
+ - spec/dummy/app/controllers/application_controller.rb
392
415
  - spec/dummy/app/controllers/base_controller.rb
393
416
  - spec/dummy/app/controllers/localized_urls.rb
394
417
  - spec/dummy/app/controllers/posts_controller.rb