Skip to content

MONGOID-5098 Bug fix: Range.mongoize should mongoize its members #5108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/mongoid/extensions/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,30 @@ def demongoize(object)
#
# @return [ Hash ] The object mongoized.
def mongoize(object)
return nil if object.nil?
return object if object.is_a?(::Hash)
return object if object.is_a?(String)
hash = { "min" => object.first, "max" => object.last }
case object
when NilClass then nil
when String then object
when Hash then __mongoize_hash__(object)
else __mongoize_range__(object)
end
end

private

def __mongoize_hash__(object)
hash = object.stringify_keys
hash.slice!('min', 'max', 'exclude_end')
hash.compact!
hash.transform_values!(&:mongoize)
hash
end

def __mongoize_range__(object)
hash = {}
hash['min'] = object.first&.mongoize
hash['max'] = object.last&.mongoize
if object.respond_to?(:exclude_end?) && object.exclude_end?
hash.merge!("exclude_end" => true)
hash['exclude_end'] = true
end
hash
end
Expand Down
302 changes: 302 additions & 0 deletions spec/integration/persistence/range_field_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Range field persistence' do
let!(:person) { Person.create!(field => value).reload }
subject { person.send(field) }
let(:now_utc) { Time.now }
let(:later_utc) { now_utc + 10.minutes }
let(:now_in_zone) { now_utc.in_time_zone('Asia/Tokyo') }
let(:later_in_zone) { later_utc.in_time_zone('Asia/Tokyo') }

context 'static field' do
let(:field) { :range }

context 'when Integer' do
let(:value) { 1..3 }
it do
expect(subject).to eq(1..3)
end
end

context 'when Integer exclude_end' do
let(:value) { 1...3 }
it { expect(subject).to eq(1...3) }
end

context 'when Hash<String, Integer>' do
let(:value) { { 'min' => 1, 'max' => 3 } }
it { expect(subject).to eq(1..3) }
end

context 'when Hash<String, Integer> exclude_end' do
let(:value) { { 'min' => 1, 'max' => 3, 'exclude_end' => true } }
it { expect(subject).to eq(1...3) }
end

context 'when Hash<Symbol, Integer>' do
let(:value) { { min: 1, max: 3 } }
it { expect(subject).to eq(1..3) }
end

context 'when Hash<Symbol, Integer> exclude_end' do
let(:value) { { min: 1, max: 3, exclude_end: true } }
it { expect(subject).to eq(1...3) }
end

context 'when Time' do
let(:value) { now_utc..later_utc }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq false
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when Time exclude_end' do
let(:value) { now_utc...later_utc }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq true
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when Hash<String, Time>' do
let(:value) { { 'min' => now_utc, 'max' => later_utc } }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq false
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when Hash<String, Time> exclude_end' do
let(:value) { { 'min' => now_utc, 'max' => later_utc, 'exclude_end' => true } }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq true
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when ActiveSupport::TimeWithZone' do
let(:value) { now_in_zone..later_in_zone }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq false
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when ActiveSupport::TimeWithZone exclude_end' do
let(:value) { now_in_zone...later_in_zone }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq true
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when Hash<String, ActiveSupport::TimeWithZone>' do
let(:value) { { 'min' => now_in_zone, 'max' => later_in_zone } }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq false
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end

context 'when Hash<String, ActiveSupport::TimeWithZone> exclude_end' do
let(:value) { { 'min' => now_in_zone, 'max' => later_in_zone, 'exclude_end' => true } }

it do
expect(subject).to be_a Range
expect(subject.exclude_end?).to eq true
expect(subject.first).to be_within(0.01.second).of(now_utc)
expect(subject.last).to be_within(0.01.second).of(later_utc)
expect(subject.first.class).to eq Time
expect(subject.last.class).to eq Time
end
end
end

context 'dynamic field' do
let(:field) { :dynamic }

context 'when Integer' do
let(:value) { 1..3 }
it do
expect(subject).to eq('max' => 3, 'min' => 1)
end
end

context 'when Integer exclude_end' do
let(:value) { 1...3 }
it { expect(subject).to eq('max' => 3, 'min' => 1, 'exclude_end' => true) }
end

context 'when descending' do
let(:value) { 3..1 }
it { expect(subject).to eq('max' => 1, 'min' => 3) }
end

context 'when descending exclude_end' do
let(:value) { 3...1 }
it { expect(subject).to eq('max' => 1, 'min' => 3, 'exclude_end' => true) }
end

context 'when Hash<String, Integer>' do
let(:value) { { 'min' => 1, 'max' => 3 } }
it { expect(subject).to eq('max' => 3, 'min' => 1) }
end

context 'when Hash<String, Integer> exclude_end' do
let(:value) { { 'min' => 1, 'max' => 3, 'exclude_end' => true } }
it { expect(subject).to eq('max' => 3, 'min' => 1, 'exclude_end' => true) }
end

context 'when Hash<Symbol, Integer>' do
let(:value) { { min: 1, max: 3 } }
it { expect(subject).to eq('max' => 3, 'min' => 1) }
end

context 'when Hash<Symbol, Integer> exclude_end' do
let(:value) { { min: 1, max: 3, exclude_end: true } }
it { expect(subject).to eq('max' => 3, 'min' => 1, 'exclude_end' => true) }
end

context 'when Time' do
let(:value) { now_utc..later_utc }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq nil
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when Time exclude_end' do
let(:value) { now_utc...later_utc }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq true
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when Hash<String, Time>' do
let(:value) { { 'min' => now_utc, 'max' => later_utc } }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq nil
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when Hash<String, Time> exclude_end' do
let(:value) { { 'min' => now_utc, 'max' => later_utc, 'exclude_end' => true } }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq true
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when ActiveSupport::TimeWithZone' do
let(:value) { now_in_zone..later_in_zone }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq nil
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when ActiveSupport::TimeWithZone exclude_end' do
let(:value) { now_in_zone...later_in_zone }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq true
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when Hash<String, ActiveSupport::TimeWithZone>' do
let(:value) { { 'min' => now_in_zone, 'max' => later_in_zone } }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq nil
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end

context 'when Hash<String, ActiveSupport::TimeWithZone> exclude_end' do
let(:value) { { 'min' => now_in_zone, 'max' => later_in_zone, 'exclude_end' => true } }

it do
expect(subject).to be_a Hash
expect(subject['exclude_end']).to eq true
expect(subject['min']).to be_within(0.01.second).of(now_utc)
expect(subject['max']).to be_within(0.01.second).of(later_utc)
expect(subject['min'].class).to eq Time
expect(subject['max'].class).to eq Time
end
end
end
end
Loading