diff --git a/lib/will_paginate/data_mapper.rb b/lib/will_paginate/data_mapper.rb index 127998242..13f43646b 100644 --- a/lib/will_paginate/data_mapper.rb +++ b/lib/will_paginate/data_mapper.rb @@ -63,9 +63,13 @@ def total_entries end def to_a - ::WillPaginate::Collection.create(current_page, per_page) do |col| - col.replace super - col.total_entries ||= total_entries + if paginated? + ::WillPaginate::Collection.create(current_page, per_page) do |col| + col.replace super + col.total_entries ||= total_entries + end + else + super end end diff --git a/spec/finders/data_mapper_spec.rb b/spec/finders/data_mapper_spec.rb index 67929b6ec..8c536d20d 100644 --- a/spec/finders/data_mapper_spec.rb +++ b/spec/finders/data_mapper_spec.rb @@ -80,4 +80,24 @@ Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1 end + it "can iterate and then call WP methods" do + animals = Animal.all(:limit => 2).page(1) + animals.each { |a| } + animals.total_entries.should == 3 + end + + it "augments to_a to return a WP::Collection" do + animals = Animal.all(:limit => 2).page(1) + array = animals.to_a + array.size.should == 2 + array.is_a? WillPaginate::Collection + array.current_page.should == 1 + array.per_page.should == 2 + end + + it "doesn't have a problem assigning has-one-through relationship" do + human = Human.create :name => "Mislav" + human.pet = Animal.first + end + end if datamapper_loaded diff --git a/spec/finders/data_mapper_test_connector.rb b/spec/finders/data_mapper_test_connector.rb index 31b0bf4a5..9d7453f6c 100644 --- a/spec/finders/data_mapper_test_connector.rb +++ b/spec/finders/data_mapper_test_connector.rb @@ -19,9 +19,34 @@ def self.setup end end +class Ownership + include DataMapper::Resource + + belongs_to :animal, :key => true + belongs_to :human, :key => true + + def self.setup + end +end + +class Human + include DataMapper::Resource + + property :id, Serial + property :name, String + + has n, :ownerships + has 1, :pet, :model => 'Animal', :through => :ownerships, :via => :animal + + def self.setup + end +end + # Load fixtures -Animal.auto_migrate! -Animal.setup +[Animal, Ownership, Human].each do |klass| + klass.auto_migrate! + klass.setup +end if 'irb' == $0 DataMapper.logger.set_log($stdout, :debug)