Skip to content

Commit

Permalink
fix failing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Apr 25, 2024
1 parent fe2b45a commit 741437b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
4 changes: 3 additions & 1 deletion lib/mongoid/extensions/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ def mongoize(object)
if object.is_a?(String)
# https://jira.mongodb.org/browse/MONGOID-4460
time = ::Time.parse(object)
else
elsif object.respond_to?(:__mongoize_time__)
time = object.__mongoize_time__
else
nil
end
rescue ArgumentError
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def demongoize(object)
def mongoize(object)
return if object.blank?
begin
time = object.__mongoize_time__
time = object.respond_to?(:__mongoize_time__) ? object.__mongoize_time__ : nil
rescue ArgumentError
return
end
Expand Down
20 changes: 9 additions & 11 deletions lib/mongoid/scopable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,15 @@ def check_scope_validity(value)
# @return [ Method ] The defined method.
def define_scope_method(name)
singleton_class.class_eval do
ruby2_keywords(
define_method(name) do |*args, **kwargs|
scoping = _declared_scopes[name]
scope = instance_exec(*args, **kwargs, &scoping[:scope])
extension = scoping[:extension]
to_merge = scope || queryable
criteria = to_merge.empty_and_chainable? ? to_merge : with_default_scope.merge(to_merge)
criteria.extend(extension)
criteria
end
)
define_method(name) do |*args, **kwargs|
scoping = _declared_scopes[name]
scope = instance_exec(*args, **kwargs, &scoping[:scope])
extension = scoping[:extension]
to_merge = scope || queryable
criteria = to_merge.empty_and_chainable? ? to_merge : with_default_scope.merge(to_merge)
criteria.extend(extension)
criteria
end
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/mongoid/criteria/queryable/selectable_logical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@
# Date instance is converted to a Time instance in local time,
# because we are querying on a Time field and dates are interpreted
# in local time when assigning to Time fields
{'published' => {'$gt' => Time.local(2020, 2, 3)}},
{'published' => {'$gt' => Time.zone.local(2020, 2, 3)}},
]}
end
end
Expand Down Expand Up @@ -2427,7 +2427,7 @@

context 'when a criterion has an aliased field' do
let(:selection) { query.none_of({ id: 1 }) }

it 'adds the $nor selector and aliases the field' do
expect(selection.selector).to eq('$nor' => [{ '_id' => 1 }])
end
Expand Down Expand Up @@ -2514,7 +2514,7 @@
# Date instance is converted to a Time instance in local time,
# because we are querying on a Time field and dates are interpreted
# in local time when assigning to Time fields
{'published' => {'$gt' => Time.local(2020, 2, 3) } },
{'published' => {'$gt' => Time.zone.local(2020, 2, 3) } },
] }
end
end
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/extensions/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
end
end

describe "#__mongoize_time__" do

it "returns self" do
expect(object.__mongoize_time__).to eq(object)
end
end

describe ".demongoize" do

let(:object) do
Expand Down
16 changes: 8 additions & 8 deletions spec/mongoid/extensions/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
describe ".demongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

context "when the time zone is not defined" do
Expand All @@ -22,7 +22,7 @@

it "returns the local time" do
expect(Time.demongoize(time).utc_offset).to eq(
Time.local(2010, 11, 19).utc_offset
Time.zone.local(2010, 11, 19).utc_offset
)
end
end
Expand All @@ -41,7 +41,7 @@
context "when we have a time close to midnight" do

let(:time) do
Time.local(2010, 11, 19, 0, 30).utc
Time.zone.local(2010, 11, 19, 0, 30).utc
end

it "changes it back to the equivalent local time" do
Expand Down Expand Up @@ -322,7 +322,7 @@
describe ".mongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

context "when given nil" do
Expand Down Expand Up @@ -608,7 +608,7 @@
end

it "converts to a utc time" do
expect(Time.mongoize(date)).to eq(Time.local(date.year, date.month, date.day))
expect(Time.mongoize(date)).to eq(Time.zone.local(date.year, date.month, date.day))
end

it "has a zero utc offset" do
Expand All @@ -635,7 +635,7 @@
end

it "returns a time" do
expect(Time.mongoize(array)).to eq(Time.local(*array))
expect(Time.mongoize(array)).to eq(Time.zone.local(*array))
end

context "when setting ActiveSupport time zone" do
Expand All @@ -653,11 +653,11 @@
describe "#mongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

let!(:eom_time) do
Time.local(2012, 11, 30, 23, 59, 59, 999999.999)
Time.zone.local(2012, 11, 30, 23, 59, 59, 999999.999)
end

let!(:eom_time_mongoized) do
Expand Down
16 changes: 8 additions & 8 deletions spec/mongoid/extensions/time_with_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
describe ".demongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

context "when the time zone is not defined" do
Expand All @@ -22,7 +22,7 @@

it "returns the local time" do
expect(ActiveSupport::TimeWithZone.demongoize(time).utc_offset).to eq(
Time.local(2010, 11, 19).utc_offset
Time.zone.local(2010, 11, 19).utc_offset
)
end
end
Expand All @@ -41,7 +41,7 @@
context "when we have a time close to midnight" do

let(:time) do
Time.local(2010, 11, 19, 0, 30).utc
Time.zone.local(2010, 11, 19, 0, 30).utc
end

it "changes it back to the equivalent local time" do
Expand Down Expand Up @@ -136,7 +136,7 @@
describe ".mongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

context "when given nil" do
Expand Down Expand Up @@ -171,7 +171,7 @@

it "returns a local date from the string" do
expect(ActiveSupport::TimeWithZone.mongoize(time.to_s)).to eq(
Time.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
Time.zone.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
)
end
end
Expand Down Expand Up @@ -275,7 +275,7 @@
end

it "converts to a utc time" do
expect(ActiveSupport::TimeWithZone.mongoize(date)).to eq(Time.local(date.year, date.month, date.day))
expect(ActiveSupport::TimeWithZone.mongoize(date)).to eq(Time.zone.local(date.year, date.month, date.day))
end

it "has a zero utc offset" do
Expand All @@ -302,7 +302,7 @@
end

it "returns a time" do
expect(ActiveSupport::TimeWithZone.mongoize(array)).to eq(Time.local(*array))
expect(ActiveSupport::TimeWithZone.mongoize(array)).to eq(Time.zone.local(*array))
end

context "when setting ActiveSupport time zone" do
Expand All @@ -320,7 +320,7 @@
describe "#mongoize" do

let!(:time) do
Time.local(2010, 11, 19)
Time.zone.local(2010, 11, 19)
end

it "converts to a utc time" do
Expand Down
9 changes: 8 additions & 1 deletion spec/mongoid/monkey_patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
__intersect_from_array__
__intersect_from_object__
__mongoize_object_id__
__mongoize_time__
__union__
__union_from_object__
ivar
Expand All @@ -58,29 +57,34 @@
Array => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
__sort_option__
__sort_pair__
delete_one
],
Date => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
],
DateTime => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
],
FalseClass => %i[is_a?],
Float => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
],
Hash => %i[
__sort_option__
],
Integer => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
],
Module => %i[
re_define_method
Expand All @@ -102,6 +106,7 @@
__evolve_time__
__expr_part__
__mongo_expression__
__mongoize_time__
__sort_option__
before_type_cast?
collectionize
Expand Down Expand Up @@ -150,10 +155,12 @@
Time => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
],
ActiveSupport::TimeWithZone => %i[
__evolve_date__
__evolve_time__
__mongoize_time__
_bson_to_i
],
BSON::Decimal128 => %i[
Expand Down

0 comments on commit 741437b

Please sign in to comment.