1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Implement automatic finding using .find with integer or string

This commit is contained in:
Jeff Casimir 2011-07-23 09:16:52 -07:00
parent fc8199cdfc
commit 605332ce76
3 changed files with 24 additions and 9 deletions

View file

@ -9,15 +9,16 @@ module Draper
self.denied = DEFAULT_DENIED self.denied = DEFAULT_DENIED
def initialize(input) def initialize(input)
if input.instance_of?(Fixnum)
input = model_class.find(Fixnum)
end
input.inspect input.inspect
self.class.source_class = input.class self.class.source_class = input.class
@model = input @model = input
build_methods build_methods
end end
def self.find(input)
self.new(model_class.find(input))
end
def self.decorates(input) def self.decorates(input)
self.model_class = input.to_s.classify.constantize self.model_class = input.to_s.classify.constantize
end end

View file

@ -3,7 +3,7 @@ require 'draper'
describe Draper::Base do describe Draper::Base do
subject{ Draper::Base.new(source) } subject{ Draper::Base.new(source) }
let(:source){ "Sample String" } let(:source){ Product.new }
context(".decorates") do context(".decorates") do
it "sets the model class for the decorator" do it "sets the model class for the decorator" do
@ -42,12 +42,18 @@ describe Draper::Base do
end end
it "should wrap source methods so they still accept blocks" do 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 end
context ".new" do context ".find" do
it "should lookup the associated model when passed an integer" 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.should be_instance_of(ProductDecorator)
pd.model.should be_instance_of(Product) pd.model.should be_instance_of(Product)
end end
@ -55,7 +61,7 @@ describe Draper::Base do
context ".decorate" do context ".decorate" do
it "should return a collection of wrapped objects when given a collection of source objects" 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 = Draper::Base.decorate(sources)
output.should respond_to(:each) output.should respond_to(:each)
output.size.should == sources.size output.size.should == sources.size
@ -79,7 +85,7 @@ describe Draper::Base do
end end
it "should not clobber other decorators' methods" do it "should not clobber other decorators' methods" do
subject.should respond_to(:upcase) subject.should respond_to(:hello_world)
end end
end end

View file

@ -2,6 +2,14 @@ class Product
def self.find(id) def self.find(id)
return Product.new return Product.new
end end
def hello_world
"Hello, World"
end
def block
yield
end
end end
class ProductDecorator < Draper::Base class ProductDecorator < Draper::Base