Commit Graph

28 Commits

Author SHA1 Message Date
Shu Fujita 157eb95507 Clean and remove empty lines (#844)
## Description
Clean and remove empty lines by Rubocop.
2019-02-25 12:22:28 -06:00
Pascal Betz 6f76c64528 When decoration fails, show original class (#821) 2017-10-12 11:55:00 -05:00
Yen-Nan Lin cc23669889 Let decorator_class infer anonymous class' decorator from superclass (#820)
* Let decorator_class infer anonymous class' decorator from superclass

* Replace constantize with safe_constantize on decorator's private methods

* Don't raise NameError on decorator private methods.
To prevent rescuing and checking if NameError is from internal decorator
private methods, we change to return nil on these private methods.

* Revise decorator inferred_object_class and collection_decorator_name
based on code review comments
2017-09-14 07:48:01 -07:00
Joe Rafaniello e79ae14518 Only Draper::Decorator types should delegate === to object. (#720)
Draper::Decorater provides the object method and we should delegate === to this
method only for these types.

Other classes could respond_to?(:object) so using is_a? clarifies not just any
class implementing object but only classes that are a Draper::Decorator.

Besides this, another major reason is is_a? is much faster than respond_to?
in ActiveRecord models because of the number of methods AR provides and the
layers of delegation. There is a cache specifically for respond_to? [1] because
it's so slow.

Even with this cache, some of our pages are 10% faster by using the is_a? change
from this patch.

The original implementation [2] used the method source, which was later renamed
to object.  The tests seem to indicate the intention was to always have
this === delegation to source/object for decorated objects... which is what
Drapper::Decorator types are.

Test changes:
* verify decorated instances of Product and instances derived from
decorated instances of Product are === Product.
* verify unrelated decorated instances and non-decorators that
happen to implement object are not === Product.

[1] https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/attribute_methods.rb#L343-L351
[2] ca116154f3
2017-03-31 14:51:10 -05:00
eugen0329 de2ae4a7d3 Fix wrong UninferrableDecoratorError error raising 2016-11-07 04:36:53 +03:00
Sean Linsley 5d9e6919c4 remove old compatibility code 2016-07-04 16:07:06 -05:00
Michael Schuerig 10eac99b44 Inherit decorator from superclass.
If the class itself does not have a decorator, try the superclass.
2013-11-10 18:02:34 +01:00
Patrick McNeill 53e1df5c3e Adds 'decorator_class?' method
Indicates whether a class can be decorated or not without resorting to a
rescue in application-level code.
2013-05-19 19:58:36 -04:00
Andrew Haines 4b933ef39d Rename `source` to `object`
Closes #501
2013-04-30 14:10:52 +01:00
Andrew Haines 2444c09bdc Revert "Merge pull request #480 from urbanautomaton/decorator-namespace-option"
This reverts commit f93542ca7c, reversing
changes made to f4dd9e0206.
2013-03-10 09:27:12 +01:00
Andrew Haines 09ad84fb71 Rescue specific NameErrors when inferring classes
The previous behavior swallowed genuine NameErrors (as opposed to
failure to find the inferred class), for example when the user has
not updated the decorator base class from Draper::Base.
2013-03-05 22:28:03 +00:00
Andrew Haines 47ee29c3b7 Prevent deprecation warning in Rails 4.0
Use `all` instead of `scoped` if possible. Closes #484
2013-03-03 11:14:18 +00:00
Simon Coffey b1d66ca15c Add option to specify namespace for decorator lookup
To use different decorators in different contexts (e.g. HTML rendering
vs. API representation), the ability to specify a decorator namespace is
added. For example:

    > Product.new.decorate
    => #<ProductDecorator:0x0000010b6e47e8>
    > Product.new.decorate(:namespace => HTML)
    => #<HTML::ProductDecorator:0x0000010b6e47e9>
2013-02-19 20:00:23 +00:00
Andrew Haines b156635b1e Support STI in Decoratable.decorate
Closes #468
2013-02-12 08:45:55 +00:00
Andrew Haines 686f1395da Check for `decorated?` before using `source` in #==
Closes #426
2013-01-18 10:05:13 +00:00
Andrew Haines 0888133aa6 Documentation 2013-01-07 16:37:36 +00:00
Andrew Haines b83a95f0f0 Use `self == other.source` in Decoratable#==
Apparently AR::Base does something tricky with #== which causes
`super(other.source)` to break

Closes #391
2012-12-28 12:52:45 +00:00
Andrew Haines 46f8a6823c Implement Decoratable#==
Now model == decorator as well as decorator == model
2012-12-11 19:56:50 +00:00
Andrew Haines ca116154f3 Add === support
Closes #341
2012-11-15 23:41:00 +00:00
Andrew Haines 52daa3125b Handle options properly in Decoratable#decorate
In 30104b0 I had accidentally nuked the options but didn't realise
as there was no spec coverage.

I then discovered that, due to memoization, `#decorate` did not
use options after the first call:

    product.decorate(role: :admin)
    product.decorate(role: :user)  # still has admin role!

I ditched the `#decorator` alias since `#decorate` seems far more
canonical, and dropped the block passing syntax, since this seems
orthogonal to the main purpose of the method.
2012-11-08 01:25:28 +00:00
Andrew Haines 7748ce5fcb Split up decorate and decorate_collection 2012-11-08 00:50:35 +00:00
Andrew Haines b5fc398bca Fix Decoratable#decorator_class
It was possible that `Model.decorator_class` and
`Model.new.decorator_class` might be two different things.
2012-11-08 00:50:35 +00:00
Andrew Haines 30104b0fc0 Move :infer option to CollectionDecorator 2012-11-07 23:19:05 +00:00
Andrew Haines dc53646376 Add UninferrableDecoratorError 2012-11-01 02:06:03 +00:00
Andrew Haines d017518b33 Use alias_method throughout for consistency 2012-10-31 22:27:40 +00:00
Andrew Haines 834a6fd1f2 Add decorated? method 2012-10-30 22:47:13 +00:00
Andrew Haines 7a32c79f1a Add similar methods to Decoratable 2012-10-30 22:37:40 +00:00
Andrew Haines d06afda764 Rename ModelSupport to Decoratable 2012-10-09 10:11:57 +01:00