From 53e1df5c3ee169144a2778c6d2ee13cc6af99429 Mon Sep 17 00:00:00 2001 From: Patrick McNeill Date: Fri, 17 May 2013 17:57:57 -0400 Subject: [PATCH] Adds 'decorator_class?' method Indicates whether a class can be decorated or not without resorting to a rescue in application-level code. --- lib/draper/decoratable.rb | 10 ++++++++++ spec/draper/decoratable_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index 70ae4f90..2c39dd66 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -23,6 +23,10 @@ def decorator_class self.class.decorator_class end + def decorator_class? + self.class.decorator_class? + end + # The list of decorators that have been applied to the object. # # @return [Array] `[]` @@ -56,6 +60,12 @@ def decorate(options = {}) decorator_class.decorate_collection(collection, options.reverse_merge(with: nil)) end + def decorator_class? + decorator_class + rescue Draper::UninferrableDecoratorError + false + end + # Infers the decorator class to be used by {Decoratable#decorate} (e.g. # `Product` maps to `ProductDecorator`). # diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index e880acbd..46ef1036 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -46,6 +46,26 @@ module Draper end end + describe "#decorator_class?" do + it "returns true for decoratable model" do + expect(Product.new.decorator_class?).to be_true + end + + it "returns false for non-decoratable model" do + expect(Model.new.decorator_class?).to be_false + end + end + + describe ".decorator_class?" do + it "returns true for decoratable model" do + expect(Product.decorator_class?).to be_true + end + + it "returns false for non-decoratable model" do + expect(Model.decorator_class?).to be_false + end + end + describe "#decorator_class" do it "delegates to .decorator_class" do product = Product.new