Skip to content

Commit

Permalink
Merge pull request #322 from haines/ditch-utils
Browse files Browse the repository at this point in the history
Remove Utils module
  • Loading branch information
steveklabnik committed Oct 30, 2012
2 parents 22695a8 + 61033a2 commit f1c7226
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 47 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
14 changes: 13 additions & 1 deletion lib/draper/decoratable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ def decorator_class

alias :decorate :decorator

def applied_decorators
[]
end

def decorated_with?(decorator_class)
false
end

def decorated?
false
end

module ClassMethods
def decorate(options = {})
decorator_proxy = decorator_class.decorate(self.scoped, options)
Expand All @@ -22,4 +34,4 @@ def decorator_class
"#{model_name}Decorator".constantize
end
end
end
end
48 changes: 34 additions & 14 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,13 +172,30 @@ def self.last(options = {})
decorate(model_class.last, options)
end

# Fetch the original wrapped model.
# 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.
#
# @return [Object] original_model
def wrapped_object
@model
# @param [Class] decorator_class
def decorated_with?(decorator_class)
applied_decorators.include?(decorator_class)
end

def decorated?
true
end

def decorator
self
end
alias :decorate :decorator

# Delegates == to the decorated models
#
# @return [Boolean] true if other's model == self's model
Expand Down Expand Up @@ -245,10 +258,9 @@ def context=(input)
options[:context] = input
end

def source
model
end
alias_method :to_source, :model
alias :wrapped_object :model
alias :source :model
alias :to_source :model

private

Expand All @@ -260,6 +272,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.

18 changes: 18 additions & 0 deletions spec/draper/decoratable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
end
end

describe "#applied_decorators" do
it "returns an empty list" do
subject.applied_decorators.should be_empty
end
end

describe "#decorated_with?" do
it "returns false" do
subject.should_not be_decorated_with(ProductDecorator)
end
end

describe "#decorated?" do
it "returns false" do
subject.should_not be_decorated
end
end

describe Draper::Decoratable::ClassMethods do
shared_examples_for "a call to Draper::Decoratable::ClassMethods#decorate" do
subject { klass.limit }
Expand Down
32 changes: 32 additions & 0 deletions spec/draper/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,38 @@ 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

describe "#decorated?" do
it "returns true" do
subject.should be_decorated
end
end

describe "#decorator" do
it "returns the decorator itself" do
subject.decorator.should be subject
end

it "is aliased to #decorate" do
subject.decorate.should be subject
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 f1c7226

Please sign in to comment.