Skip to content

Commit

Permalink
Merge pull request #1404 from paper-trail-gem/fix_1401_early_ar_refer…
Browse files Browse the repository at this point in the history
…ence

Delay referencing ActiveRecord until after Railtie is loaded
  • Loading branch information
jaredbeck authored Nov 12, 2022
2 parents ccf0183 + 34c8312 commit 7cdd212
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- [#1404](/~https://github.com/paper-trail-gem/paper_trail/pull/1404) -
Delay referencing ActiveRecord until after Railtie is loaded

## 13.0.0 (2022-08-15)

Expand Down
8 changes: 6 additions & 2 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
# can revisit this decision.
require "active_support/all"

# We used to `require "active_record"` here, but that was [replaced with a
# Railtie](/~https://github.com/paper-trail-gem/paper_trail/pull/1281) in PT 12.
# As a result, we cannot reference `ActiveRecord` in this file (ie. until our
# Railtie has loaded). If we did, it would cause [problems with non-Rails
# projects](/~https://github.com/paper-trail-gem/paper_trail/pull/1401).

require "paper_trail/errors"
require "paper_trail/cleaner"
require "paper_trail/compatibility"
Expand All @@ -26,8 +32,6 @@ module PaperTrail
named created_at.
EOS

RAILS_GTE_7_0 = ::ActiveRecord.gem_version >= ::Gem::Version.new("7.0.0")

extend PaperTrail::Cleaner

class << self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def deserialize(attr, val)
if defined_enums[attr] && val.is_a?(::String)
# Because PT 4 used to save the string version of enums to `object_changes`
val
elsif PaperTrail::RAILS_GTE_7_0 && val.is_a?(ActiveRecord::Type::Time::Value)
elsif rails_gte_7_0? && val.is_a?(ActiveRecord::Type::Time::Value)
# Because Rails 7 time attribute throws a delegation error when you deserialize
# it with the factory.
# See ActiveRecord::Type::Time::Value crashes when loaded from YAML on rails 7.0
Expand All @@ -43,6 +43,10 @@ def deserialize(attr, val)
end
end

def rails_gte_7_0?
::ActiveRecord.gem_version >= ::Gem::Version.new("7.0.0")
end

def serialize(attr, val)
AttributeSerializerFactory.for(@klass, attr).serialize(val)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/queries/versions/where_object_changes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(version_model_class, attributes)
@version_model_class = version_model_class

# Currently, this `deep_dup` is necessary because the `jsonb` branch
# modifies `@attributes`, and that would be a nasty suprise for
# modifies `@attributes`, and that would be a nasty surprise for
# consumers of this class.
# TODO: Stop modifying `@attributes`, then remove `deep_dup`.
@attributes = attributes.deep_dup
Expand Down
6 changes: 3 additions & 3 deletions spec/support/shared_examples/queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
adapter = instance_spy("CustomObjectChangesAdapter")
bicycle = model.create!(name: "abc")
allow(adapter).to(
receive(:where_object_changes).with(model.paper_trail.version_class, name: "abc")
receive(:where_object_changes).with(model.paper_trail.version_class, { name: "abc" })
).and_return(bicycle.versions[0..1])
PaperTrail.config.object_changes_adapter = adapter
expect(
Expand Down Expand Up @@ -238,7 +238,7 @@
bicycle.update!(name: "xyz")

allow(adapter).to(
receive(:where_object_changes_from).with(model.paper_trail.version_class, name: "abc")
receive(:where_object_changes_from).with(model.paper_trail.version_class, { name: "abc" })
).and_return([bicycle.versions[1]])

PaperTrail.config.object_changes_adapter = adapter
Expand Down Expand Up @@ -321,7 +321,7 @@
bicycle.update!(name: "xyz")

allow(adapter).to(
receive(:where_object_changes_to).with(model.paper_trail.version_class, name: "xyz")
receive(:where_object_changes_to).with(model.paper_trail.version_class, { name: "xyz" })
).and_return([bicycle.versions[1]])

PaperTrail.config.object_changes_adapter = adapter
Expand Down

0 comments on commit 7cdd212

Please sign in to comment.