Skip to content
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
9 changes: 8 additions & 1 deletion lib/activerecord-bitemporal/bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,18 @@ def find_at_time(datetime, *ids)
if ActiveRecord.version >= Gem::Version.new("8.0.0")
use_bitemporal_id_as_primary_key :ids

# Ensure that AR::Batches (https://github.com/rails/rails/blob/v8.0.4/activerecord/lib/active_record/relation/batches.rb)
# Ensure that AR::Batches
# (https://github.com/rails/rails/blob/v8.0.5/activerecord/lib/active_record/relation/batches.rb)
# works with `bitemporal_id` as the primary key in Rails 8+.
use_bitemporal_id_as_primary_key :find_each
use_bitemporal_id_as_primary_key :find_in_batches
use_bitemporal_id_as_primary_key :in_batches

# Ensure that `#excluding` and `#without`
# (https://github.com/rails/rails/blob/v8.0.5/activerecord/lib/active_record/relation/query_methods.rb#L1548-L1591)
# work with `bitemporal_id` as the primary key in Rails 8+.
use_bitemporal_id_as_primary_key :excluding
use_bitemporal_id_as_primary_key :without
end

def build_arel(*)
Expand Down
63 changes: 63 additions & 0 deletions spec/activerecord-bitemporal/relation/excluding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe "#excluding (#without)" do
def create_employees_with_history
Employee
.create!([{ name: "Jane" }, { name: "Tom" }, { name: "Bob" }])
.each { |e| e.update!(updated_at: Time.current) }
end

describe "#excluding" do
context "with record objects" do
let!(:employees) { create_employees_with_history }

it "excludes the specified record by bitemporal_id" do
excluded = employees[0]
result = Employee.excluding(excluded)
expect(result).to match_array [employees[1], employees[2]]
expect(result).not_to include(excluded)
end

it "excludes multiple records by bitemporal_id" do
result = Employee.excluding(employees[0], employees[1])
expect(result).to match_array [employees[2]]
end
end

context "with a Relation" do
let!(:employees) { create_employees_with_history }

it "excludes records from the relation" do
excluded_relation = Employee.where(name: "Jane")
result = Employee.excluding(excluded_relation)
expect(result).to match_array [employees[1], employees[2]]
end
end

context "on association" do
let!(:company) {
Company
.create!(name: "Company")
.tap { |c| c.employees << create_employees_with_history }
}

it "excludes the specified record" do
employees = company.employees.to_a
result = company.employees.excluding(employees[0])
expect(result).to match_array [employees[1], employees[2]]
end
end
end

describe "#without" do
let!(:employees) { create_employees_with_history }

it "is an alias for excluding" do
excluded = employees[0]
result = Employee.without(excluded)
expect(result).to match_array [employees[1], employees[2]]
end
end
end