Implementing #first and #last methods on decorators

This commit is contained in:
Jeff Casimir 2011-10-28 21:02:36 -04:00
parent 3670ef76ee
commit 0893f1f813
3 changed files with 34 additions and 0 deletions

View File

@ -103,6 +103,14 @@ module Draper
def self.all(context = {})
Draper::DecoratedEnumerableProxy.new(model_class.all, self, context)
end
def self.first(context = {})
decorate(model_class.first, context)
end
def self.last(context = {})
decorate(model_class.last, context)
end
# Access the helpers proxy to call built-in and user-defined
# Rails helpers. Aliased to `.h` for convinience.

View File

@ -175,6 +175,24 @@ describe Draper::Base do
subject.should == other
end
end
context 'position accessors' do
[:first, :last].each do |method|
context "##{method}" do
it "should return a decorated instance" do
ProductDecorator.send(method).should be_instance_of ProductDecorator
end
it "should return the #{method} instance of the wrapped class" do
ProductDecorator.send(method).model.should == Product.send(method)
end
it "should accept an optional context" do
ProductDecorator.send(method, :admin).context.should == :admin
end
end
end
end
describe "collection decoration" do

View File

@ -1,5 +1,13 @@
class Product < ActiveRecord::Base
include Draper::ModelSupport
def self.first
@@first ||= Product.new
end
def self.last
@@last ||= Product.new
end
def self.all
[Product.new, Product.new]