Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
fix Lazy loading breaks STI collection
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Kubb <dan.kubb@autopilotmarketing.com>
  • Loading branch information
contagion authored and Dan Kubb committed Jan 21, 2009
1 parent 8591493 commit 71b6117
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/dm-core/collection.rb
Expand Up @@ -35,7 +35,9 @@ def load(values)
# @api public
def reload(query = {})
@query = scoped_query(query)
@query.update(:fields => @query.fields | @key_properties)
inheritance_property = model.base_model.inheritance_property
fields = (@key_properties | [inheritance_property]).compact
@query.update(:fields => @query.fields | fields)
replace(all(:reload => true))
end

Expand Down
4 changes: 4 additions & 0 deletions lib/dm-core/model.rb
Expand Up @@ -234,6 +234,10 @@ def key(repository_name = default_repository_name)
properties(repository_name).key
end

def inheritance_property(repository_name = default_repository_name)
@properties[repository_name].detect { |property| property.type == DataMapper::Types::Discriminator }
end

def default_order(repository_name = default_repository_name)
@default_order ||= {}
@default_order[repository_name] ||= key(repository_name).map { |property| Query::Direction.new(property) }
Expand Down
42 changes: 42 additions & 0 deletions spec/integration/sti_spec.rb
Expand Up @@ -14,6 +14,7 @@ class ::Book
property :id, Serial
property :title, String, :nullable => false
property :isbn, Integer, :nullable => false
property :content, Text
property :class_type, Discriminator
end

Expand Down Expand Up @@ -226,5 +227,46 @@ class ::SpaceWestern < ScienceFiction
end
end
end

describe "with lazy property" do
before :all do
Book.auto_migrate!(:sqlite3)
repository(:sqlite3) do
ShortStory.create(
:title => "The Science of Happiness",
:isbn => "129038",
:content => <<-HERE,
This doesn’t feel like a normal academic conference. True, the three-day Positive Psychology Summit i
HERE
:moral => "Bullshit might get you to the top, but it won't keep you there.")
ShortStory.create(
:title => "Blank as white paper",
:isbn => "129039",
:content => '',
:moral => "none.")
end
end

it "should be able to access the properties from the parent collection" do
repository(:sqlite3) do
books = Book.all

books.each do |book|
book.title.should_not be_nil
book.isbn.should_not be_nil
book.moral.should_not be_nil
end

books[0].content

books.each do |book|
book.title.should_not be_nil
book.isbn.should_not be_nil
book.moral.should_not be_nil
end

end
end
end
end
end

0 comments on commit 71b6117

Please sign in to comment.