Skip to content

Commit

Permalink
Only add _type for models using inheritance. Closes #2067.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jun 5, 2012
1 parent 8e6adb7 commit a575841
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/mongoid/criteria.rb
Expand Up @@ -222,7 +222,7 @@ def extras(extras)
# @since 2.0.0
def field_list
if options[:fields]
options[:fields].keys.reject!{ |key| key == "_type" }
options[:fields].keys.reject{ |key| key == "_type" }
else
[]
end
Expand Down Expand Up @@ -438,7 +438,11 @@ def merge!(other)
# @since 1.0.0
def only(*args)
return clone if args.empty?
super(*(args + [:_type]))
if klass.hereditary?
super(*args.push(:_type))
else
super(*args)
end
end

# Returns true if criteria responds to the given method.
Expand Down
26 changes: 22 additions & 4 deletions spec/mongoid/criteria_spec.rb
Expand Up @@ -2746,12 +2746,30 @@
Band.create(name: "Depeche Mode")
end

let(:criteria) do
Band.only(:_id)
context "when not using inheritance" do

let(:criteria) do
Band.only(:_id)
end

it "limits the returned fields" do
criteria.first.name.should be_nil
end

it "does not add _type to the fields" do
criteria.options[:fields]["_type"].should be_nil
end
end

it "limits the returned fields" do
criteria.first.name.should be_nil
context "when using inheritance" do

let(:criteria) do
Doctor.only(:_id)
end

it "adds _type to the fields" do
criteria.options[:fields]["_type"].should eq(1)
end
end
end

Expand Down

0 comments on commit a575841

Please sign in to comment.