Skip to content

Commit

Permalink
Merge pull request #4296 from jonhyman/feature/fix-regression
Browse files Browse the repository at this point in the history
MONGOID-4296, fixes regression introduced in 5.1.2 with #only and…
  • Loading branch information
estolfo authored Jul 13, 2016
2 parents 9fce02f + 8436329 commit 2e2e0d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongoid/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def selection_included?(name, selection, field)
if field && field.localized?
selection.key?("#{name}.#{::I18n.locale}")
else
selection.keys.collect { |k| k.partition('.').first }.include?(name)
selection.key?(name) || selection.keys.collect { |k| k.partition('.').first }.include?(name)
end
end

Expand Down
45 changes: 45 additions & 0 deletions spec/mongoid/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@
it "does not raise an error" do
expect(from_db.desc).to eq("test")
end

context "accessing via []" do

it "does not raise an error" do
expect(from_db["desc"]).to eq("en" => "test")
end
end

context "when calling only on a sub-document" do

let(:title) {"Executive"}
let(:city) {"NYC"}
let!(:agent) do
agent = Agent.new(:title => title)
agent.build_address(:city => city)
agent.save()
agent
end
let(:from_db) do
Agent.only(:title, "address.city").first
end

context "when the field is in the only" do

it "does not raise an error" do
expect(from_db.address.city).to eq(city)
end
end

context "accessing via []" do

it "does not raise an error" do
expect(from_db["address.city"]).to eq(city)
end
end
end
end

context 'when the attribute is a hash field' do
Expand Down Expand Up @@ -86,6 +122,15 @@
from_db.title
}.to raise_error(ActiveModel::MissingAttributeError)
end

context "accessing via []" do

it "raises an error" do
expect {
from_db["title"]
}.to raise_error(ActiveModel::MissingAttributeError)
end
end
end

context "when excluding with without" do
Expand Down

0 comments on commit 2e2e0d0

Please sign in to comment.