Skip to content

Commit

Permalink
Data Mapper: fix to_a and association bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jan 31, 2012
1 parent 0bb3c8d commit c88edf1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/will_paginate/data_mapper.rb
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions spec/finders/data_mapper_spec.rb
Expand Up @@ -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
29 changes: 27 additions & 2 deletions spec/finders/data_mapper_test_connector.rb
Expand Up @@ -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)
Expand Down

0 comments on commit c88edf1

Please sign in to comment.