Skip to content

Commit

Permalink
Ensure field limitation is scoped to model. [ fix #2356 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Sep 8, 2012
1 parent e1c5573 commit 0c9ab0b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2356 When limiting fields returned in queries via `only` ensure that the
limitation is scoped to the model.

* \#2353 Allow `update_attribute` to properly handle aliased fields.

* \#2348 Conversion of strings to times should raise an arugment error if the
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/contextual/mongo.rb
Expand Up @@ -599,11 +599,11 @@ def apply_options
def selecting
begin
unless criteria.options[:fields].blank?
Threaded.selection = criteria.options[:fields]
Threaded.set_selection(klass, criteria.options[:fields])
end
yield
ensure
Threaded.selection = nil
Threaded.set_selection(klass, nil)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/fields/standard.rb
Expand Up @@ -50,7 +50,7 @@ def constraint
#
# @since 2.1.8
def eval_default(doc)
if fields = Threaded.selection
if fields = Threaded.selection(doc.class)
evaluated_default(doc) if included?(fields)
else
evaluated_default(doc)
Expand Down
13 changes: 8 additions & 5 deletions lib/mongoid/threaded.rb
Expand Up @@ -301,25 +301,28 @@ def set_persistence_options(klass, options)
# @example Get the field selection options.
# Threaded.selection
#
# @param [ Class ] klass The model class.
#
# @return [ Hash ] The field selection.
#
# @since 2.4.4
def selection
Thread.current["[mongoid]:selection"]
def selection(klass)
Thread.current["[mongoid][#{klass}]:selection"]
end

# Set the field selection on the current thread.
#
# @example Set the field selection.
# Threaded.selection = { field: 1 }
# Threaded.set_selection(Person, { field: 1 })
#
# @param [ Class ] klass The model Class.
# @param [ Hash ] value The current field selection.
#
# @return [ Hash ] The field selection.
#
# @since 2.4.4
def selection=(value)
Thread.current["[mongoid]:selection"] = value
def set_selection(klass, value)
Thread.current["[mongoid][#{klass}]:selection"] = value
end

# Get the global session override.
Expand Down
13 changes: 13 additions & 0 deletions spec/mongoid/criteria_spec.rb
Expand Up @@ -3248,6 +3248,19 @@ class C
criteria.options[:fields]["_type"].should be_nil
end
end

context "when instantiating a class of another type inside the iteration" do

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

it "only limits the fields on the correct model" do
criteria.each do |band|
Person.new.age.should eq(100)
end
end
end
end

context "when using inheritance" do
Expand Down

0 comments on commit 0c9ab0b

Please sign in to comment.