Skip to content

Commit

Permalink
Adds 'decorator_class?' method
Browse files Browse the repository at this point in the history
Indicates whether a class can be decorated or not without resorting to a
rescue in application-level code.
  • Loading branch information
pmcneill committed May 19, 2013
1 parent 1303deb commit 53e1df5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/draper/decoratable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class>] `[]`
Expand Down Expand Up @@ -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`).
#
Expand Down
20 changes: 20 additions & 0 deletions spec/draper/decoratable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53e1df5

Please sign in to comment.