Skip to content

Commit

Permalink
Drop the Utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
haines committed Oct 30, 2012
1 parent 9a5e77c commit 0bbcd19
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
1 change: 0 additions & 1 deletion lib/draper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'action_view'

require 'draper/version'
require 'draper/utils'
require 'draper/system'
require 'draper/active_model_support'
require 'draper/view_helpers'
Expand Down
31 changes: 25 additions & 6 deletions lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ class Decorator
def initialize(input, options = {})
input.to_a if input.respond_to?(:to_a) # forces evaluation of a lazy query from AR
self.class.model_class = input.class if model_class.nil?
@model = input
if input.instance_of?(self.class)
@model = input.model
elsif Utils.decorators_of(input).include?(self.class)
warn "Reapplying #{self.class} decorator to target that is already decorated with it. Call stack:\n#{caller(1).join "\n"}"
end
self.model = input
self.options = options
handle_multiple_decoration if input.is_a?(Draper::Decorator)
end

# Proxies to the class specified by `decorates` to automatically
Expand Down Expand Up @@ -176,6 +172,21 @@ def self.last(options = {})
decorate(model_class.last, options)
end

# Get the chain of decorators applied to the object.
#
# @return [Array] list of decorator classes
def applied_decorators
chain = model.respond_to?(:applied_decorators) ? model.applied_decorators : []
chain << self.class
end

# Checks if a given decorator has been applied.
#
# @param [Class] decorator_class
def decorated_with?(decorator_class)
applied_decorators.include?(decorator_class)
end

# Delegates == to the decorated models
#
# @return [Boolean] true if other's model == self's model
Expand Down Expand Up @@ -252,6 +263,14 @@ def allow?(method)
self.class.security.allow?(method)
end

def handle_multiple_decoration
if model.instance_of?(self.class)
self.model = model.model
elsif model.decorated_with?(self.class)
warn "Reapplying #{self.class} decorator to target that is already decorated with it. Call stack:\n#{caller(1).join("\n")}"
end
end

def find_association_reflection(association)
if model.class.respond_to?(:reflect_on_association)
model.class.reflect_on_association(association)
Expand Down
17 changes: 0 additions & 17 deletions lib/draper/utils.rb

This file was deleted.

16 changes: 16 additions & 0 deletions spec/draper/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ class CustomDecorator < Draper::Decorator
end
end

describe "#applied_decorators" do
it "returns a list of decorators applied to a model" do
decorator = ProductDecorator.new(SpecificProductDecorator.new(Product.new))
decorator.applied_decorators.should == [SpecificProductDecorator, ProductDecorator]
end
end

describe "#decorated_with?" do
it "checks if a decorator has been applied to a model" do
decorator = ProductDecorator.new(SpecificProductDecorator.new(Product.new))
decorator.should be_decorated_with ProductDecorator
decorator.should be_decorated_with SpecificProductDecorator
decorator.should_not be_decorated_with WidgetDecorator
end
end

context(".wrapped_object") do
it "return the wrapped object" do
subject.wrapped_object.should == source
Expand Down
14 changes: 0 additions & 14 deletions spec/utils_spec.rb

This file was deleted.

0 comments on commit 0bbcd19

Please sign in to comment.