Skip to content

Commit b2fec3e

Browse files
danhealyjamis
andauthored
MONGOID-5789 database_field_name given nil or empty string should raise UnknownAttribute exception (#5836)
* database_field_name given nil or empty string should raise UnknownAttribute exception * fix spec syntax * database_field_name return empty string instead of exception * `field` might be an empty string, not merely nil * fix specs to expect empty string instead of nil This is okay, because the database_field_name method is a private API. We can change the contract here without regard for who else might be using it. --------- Co-authored-by: Jamis Buck <jamis.buck@mongodb.com>
1 parent b083e2c commit b2fec3e

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

lib/mongoid/fields.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ def traverse_association_tree(key, fields, associations, aliased_associations)
413413
#
414414
# @api private
415415
def database_field_name(name, relations, aliased_fields, aliased_associations)
416-
return nil unless name.present?
416+
return "" unless name.present?
417+
417418
key = name.to_s
418419
segment, remaining = key.split('.', 2)
419420

lib/mongoid/touchable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _gather_touch_updates(now, field = nil)
7878
field = database_field_name(field)
7979

8080
write_attribute(:updated_at, now) if respond_to?("updated_at=")
81-
write_attribute(field, now) if field
81+
write_attribute(field, now) if field.present?
8282

8383
touches = _extract_touches_from_atomic_sets(field) || {}
8484
touches.merge!(_parent._gather_touch_updates(now) || {}) if _touchable_parent?

spec/mongoid/attributes_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@
289289
end
290290
end
291291

292+
context "when given nil" do
293+
294+
it "returns nil" do
295+
expect(person[nil]).to be nil
296+
end
297+
298+
end
299+
300+
context "when given an empty string" do
301+
302+
it "returns nil" do
303+
expect(person[""]).to be nil
304+
end
305+
306+
end
307+
292308
context "when the field was not explicitly defined" do
293309

294310
context "when excluding with only and the field was not excluded" do

spec/mongoid/fields_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,12 +1871,12 @@ class DiscriminatorChild2 < DiscriminatorParent
18711871

18721872
context 'given nil' do
18731873
subject { Person.database_field_name(nil) }
1874-
it { is_expected.to eq nil }
1874+
it { is_expected.to eq '' }
18751875
end
18761876

18771877
context 'given an empty String' do
18781878
subject { Person.database_field_name('') }
1879-
it { is_expected.to eq nil }
1879+
it { is_expected.to eq '' }
18801880
end
18811881

18821882
context 'given a String' do

0 commit comments

Comments
 (0)