Skip to content
Browse files

Data Mapper: fix to_a and association bug

Closes #197, #218, #216, #204
  • Loading branch information...
1 parent 0bb3c8d commit c88edf16a8a8d5df554f89009d015561c396d1f0 @mislav committed Jan 31, 2012
Showing with 54 additions and 5 deletions.
  1. +7 −3 lib/will_paginate/data_mapper.rb
  2. +20 −0 spec/finders/data_mapper_spec.rb
  3. +27 −2 spec/finders/data_mapper_test_connector.rb
View
10 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
View
20 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
View
29 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)

0 comments on commit c88edf1

Please sign in to comment.
Something went wrong with that request. Please try again.