diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 876e646..95d24aa 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -9,15 +9,16 @@ module Draper self.denied = DEFAULT_DENIED def initialize(input) - if input.instance_of?(Fixnum) - input = model_class.find(Fixnum) - end input.inspect self.class.source_class = input.class @model = input build_methods end + def self.find(input) + self.new(model_class.find(input)) + end + def self.decorates(input) self.model_class = input.to_s.classify.constantize end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index 72350ef..7d1cb2a 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -3,7 +3,7 @@ require 'draper' describe Draper::Base do subject{ Draper::Base.new(source) } - let(:source){ "Sample String" } + let(:source){ Product.new } context(".decorates") do it "sets the model class for the decorator" do @@ -42,12 +42,18 @@ describe Draper::Base do end it "should wrap source methods so they still accept blocks" do - subject.gsub("Sample"){|match| "Super"}.should == "Super String" + subject.block{"marker"}.should == "marker" end - context ".new" do + context ".find" do it "should lookup the associated model when passed an integer" do - pd = ProductDecorator.new(1) + pd = ProductDecorator.find(1) + pd.should be_instance_of(ProductDecorator) + pd.model.should be_instance_of(Product) + end + + it "should lookup the associated model when passed a string" do + pd = ProductDecorator.find("1") pd.should be_instance_of(ProductDecorator) pd.model.should be_instance_of(Product) end @@ -55,7 +61,7 @@ describe Draper::Base do context ".decorate" do it "should return a collection of wrapped objects when given a collection of source objects" do - sources = ["one", "two", "three"] + sources = [Product.new, Product.new] output = Draper::Base.decorate(sources) output.should respond_to(:each) output.size.should == sources.size @@ -79,7 +85,7 @@ describe Draper::Base do end it "should not clobber other decorators' methods" do - subject.should respond_to(:upcase) + subject.should respond_to(:hello_world) end end diff --git a/spec/samples/product_decorator.rb b/spec/samples/product_decorator.rb index fc69334..66bbae6 100644 --- a/spec/samples/product_decorator.rb +++ b/spec/samples/product_decorator.rb @@ -2,6 +2,14 @@ class Product def self.find(id) return Product.new end + + def hello_world + "Hello, World" + end + + def block + yield + end end class ProductDecorator < Draper::Base