Skip to content

Commit

Permalink
Monkey patch active record relation query attribute hash method to be…
Browse files Browse the repository at this point in the history
…tter compare objects

This a important since arel and AR commonly use array - operator
  • Loading branch information
JesseChavez committed Jan 23, 2025
1 parent 44e7775 commit 10eb984
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/arjdbc/abstract/relation_query_attribute_monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "active_model/attribute"

module ActiveRecord
# NOTE: improved implementation for hash methods that is used to
# compare objects. AR and arel commonly use `[a, b] - [b]` operations and
# JRuby internally uses the hash method to implement that operation,
# on the other hand, CRuby does not use the hash method
# for small arrays (length <= 16).
class Relation
# monkey patch
module RelationQueryAttributeMonkeyPatch
private

def hash
# [self.class, name, value_for_database, type].hash
[self.class, name, value_before_type_cast, type].hash
end
end

class QueryAttribute
prepend RelationQueryAttributeMonkeyPatch
end
end
end
2 changes: 2 additions & 0 deletions lib/arjdbc/mysql/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require 'arjdbc/abstract/statement_cache'
require 'arjdbc/abstract/transaction_support'

require "arjdbc/abstract/relation_query_attribute_monkey_patch"

module ActiveRecord
module ConnectionAdapters
AbstractMysqlAdapter.class_eval do
Expand Down
2 changes: 2 additions & 0 deletions lib/arjdbc/postgresql/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

require 'active_model'

require "arjdbc/abstract/relation_query_attribute_monkey_patch"

module ArJdbc
# Strives to provide Rails built-in PostgreSQL adapter (API) compatibility.
module PostgreSQL
Expand Down
2 changes: 2 additions & 0 deletions lib/arjdbc/sqlite3/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
require "active_support/core_ext/class/attribute"
require "arjdbc/sqlite3/column"

require "arjdbc/abstract/relation_query_attribute_monkey_patch"

module SQLite3
module Constants
module Open
Expand Down

0 comments on commit 10eb984

Please sign in to comment.