Skip to content

Commit

Permalink
don't set default scope for discriminator's root model
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Marcin Grzesiak committed Mar 29, 2010
1 parent 58017d0 commit 6af8cab
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/model.rb
Expand Up @@ -548,7 +548,7 @@ def load(records, query)
record = record.dup
field_map.each { |property, field| record[property] = record.delete(field) if record.key?(field) }

model = discriminator && (klass = record[discriminator]) && discriminator.typecast(klass) || self
model = discriminator && discriminator.typecast(record[discriminator]) || self
model_key = model.key(repository_name)

resource = if model_key.valid?(key_values = record.values_at(*model_key))
Expand Down
2 changes: 0 additions & 2 deletions lib/dm-core/types/discriminator.rb
Expand Up @@ -13,8 +13,6 @@ def self.bind(property)
model = property.model
property_name = property.name

model.default_scope(repository_name).update(property_name => model.descendants)

model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
extend Chainable
Expand Down
12 changes: 3 additions & 9 deletions spec/public/types/discriminator_spec.rb
Expand Up @@ -3,18 +3,12 @@
describe DataMapper::Types::Discriminator do
before :all do
module ::Blog
class NewDiscriminator < DataMapper::Types::Discriminator
primitive Class
default lambda { |resource, property| resource.model }
required true
end

class Article
include DataMapper::Resource

property :id, Serial
property :title, String, :required => true
property :type, NewDiscriminator
property :type, Discriminator
end

class Announcement < Article; end
Expand Down Expand Up @@ -89,8 +83,8 @@ class Release < Announcement; end
end

describe 'Model#default_scope' do
it 'should set the default scope for the grandparent model' do
@article_model.default_scope[:type].should equal(@article_model.descendants)
it "shouldn't set the default scope for root model if you want all descendants anyway" do
@article_model.default_scope[:type].should be_nil
end

it 'should set the default scope for the parent model' do
Expand Down

0 comments on commit 6af8cab

Please sign in to comment.