Remove vestigial version conditionals
This commit is contained in:
parent
e5c913859f
commit
de1dda4f79
|
@ -27,6 +27,9 @@ Bundler/OrderedGems:
|
|||
Exclude:
|
||||
- gemfiles/* # generated by Appraisal
|
||||
|
||||
Layout/ArgumentAlignment:
|
||||
EnforcedStyle: with_fixed_indentation
|
||||
|
||||
Layout/DotPosition:
|
||||
EnforcedStyle: trailing
|
||||
|
||||
|
|
|
@ -8,9 +8,6 @@ module PaperTrail
|
|||
# The `CastAttributeSerializer` (de)serializes model attribute values. For
|
||||
# example, the string "1.99" serializes into the integer `1` when assigned
|
||||
# to an attribute of type `ActiveRecord::Type::Integer`.
|
||||
#
|
||||
# This implementation depends on the `type_for_attribute` method, which was
|
||||
# introduced in rails 4.2. As of PT 8, we no longer support rails < 4.2.
|
||||
class CastAttributeSerializer
|
||||
def initialize(klass)
|
||||
@klass = klass
|
||||
|
@ -30,13 +27,6 @@ module PaperTrail
|
|||
def defined_enums
|
||||
@defined_enums ||= (@klass.respond_to?(:defined_enums) ? @klass.defined_enums : {})
|
||||
end
|
||||
end
|
||||
|
||||
# Uses AR 5's `serialize` and `deserialize`.
|
||||
class CastAttributeSerializer
|
||||
def serialize(attr, val)
|
||||
AttributeSerializerFactory.for(@klass, attr).serialize(val)
|
||||
end
|
||||
|
||||
def deserialize(attr, val)
|
||||
if defined_enums[attr] && val.is_a?(::String)
|
||||
|
@ -46,6 +36,10 @@ module PaperTrail
|
|||
AttributeSerializerFactory.for(@klass, attr).deserialize(val)
|
||||
end
|
||||
end
|
||||
|
||||
def serialize(attr, val)
|
||||
AttributeSerializerFactory.for(@klass, attr).serialize(val)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,8 +22,6 @@ module PaperTrail
|
|||
#
|
||||
# @api private
|
||||
class Base
|
||||
RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1")
|
||||
|
||||
# @api private
|
||||
def initialize(record, in_after_callback)
|
||||
@record = record
|
||||
|
@ -51,7 +49,7 @@ module PaperTrail
|
|||
#
|
||||
# @api private
|
||||
def attribute_changed_in_latest_version?(attr_name)
|
||||
if @in_after_callback && RAILS_GTE_5_1
|
||||
if @in_after_callback
|
||||
@record.saved_change_to_attribute?(attr_name.to_s)
|
||||
else
|
||||
@record.attribute_changed?(attr_name.to_s)
|
||||
|
@ -60,30 +58,14 @@ module PaperTrail
|
|||
|
||||
# @api private
|
||||
def nonskipped_attributes_before_change(is_touch)
|
||||
cache_changed_attributes do
|
||||
record_attributes = @record.attributes.except(*@record.paper_trail_options[:skip])
|
||||
|
||||
record_attributes.each_key do |k|
|
||||
if @record.class.column_names.include?(k)
|
||||
record_attributes[k] = attribute_in_previous_version(k, is_touch)
|
||||
end
|
||||
record_attributes = @record.attributes.except(*@record.paper_trail_options[:skip])
|
||||
record_attributes.each_key do |k|
|
||||
if @record.class.column_names.include?(k)
|
||||
record_attributes[k] = attribute_in_previous_version(k, is_touch)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Rails 5.1 changed the API of `ActiveRecord::Dirty`.
|
||||
# @api private
|
||||
def cache_changed_attributes(&block)
|
||||
if RAILS_GTE_5_1
|
||||
# Everything works fine as it is
|
||||
yield
|
||||
else
|
||||
# Any particular call to `changed_attributes` produces the huge memory allocation.
|
||||
# Lets use the generic AR workaround for that.
|
||||
@record.send(:cache_changed_attributes, &block)
|
||||
end
|
||||
end
|
||||
|
||||
# Rails 5.1 changed the API of `ActiveRecord::Dirty`. See
|
||||
# https://github.com/paper-trail-gem/paper_trail/pull/899
|
||||
#
|
||||
|
@ -91,18 +73,14 @@ module PaperTrail
|
|||
#
|
||||
# @api private
|
||||
def attribute_in_previous_version(attr_name, is_touch)
|
||||
if RAILS_GTE_5_1
|
||||
if @in_after_callback && !is_touch
|
||||
# For most events, we want the original value of the attribute, before
|
||||
# the last save.
|
||||
@record.attribute_before_last_save(attr_name.to_s)
|
||||
else
|
||||
# We are either performing a `record_destroy` or a
|
||||
# `record_update(is_touch: true)`.
|
||||
@record.attribute_in_database(attr_name.to_s)
|
||||
end
|
||||
if @in_after_callback && !is_touch
|
||||
# For most events, we want the original value of the attribute, before
|
||||
# the last save.
|
||||
@record.attribute_before_last_save(attr_name.to_s)
|
||||
else
|
||||
@record.attribute_was(attr_name.to_s)
|
||||
# We are either performing a `record_destroy` or a
|
||||
# `record_update(is_touch: true)`.
|
||||
@record.attribute_in_database(attr_name.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -138,7 +116,7 @@ module PaperTrail
|
|||
def changes_in_latest_version
|
||||
# Memoized to reduce memory usage
|
||||
@changes_in_latest_version ||= begin
|
||||
if @in_after_callback && RAILS_GTE_5_1
|
||||
if @in_after_callback
|
||||
@record.saved_changes
|
||||
else
|
||||
@record.changes
|
||||
|
|
|
@ -7,8 +7,6 @@ require "paper_trail/events/update"
|
|||
module PaperTrail
|
||||
# Represents the "paper trail" for a single record.
|
||||
class RecordTrail
|
||||
RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1")
|
||||
|
||||
def initialize(record)
|
||||
@record = record
|
||||
end
|
||||
|
|
|
@ -11,15 +11,12 @@ module PaperTrail
|
|||
end
|
||||
|
||||
def serialize(array)
|
||||
return serialize_with_ar(array) if active_record_pre_502?
|
||||
array
|
||||
end
|
||||
|
||||
def deserialize(array)
|
||||
return deserialize_with_ar(array) if active_record_pre_502?
|
||||
|
||||
case array
|
||||
# Needed for legacy reasons. If serialized array is a string
|
||||
# Needed for legacy data. If serialized array is a string
|
||||
# then it was serialized with Rails < 5.0.2.
|
||||
when ::String then deserialize_with_ar(array)
|
||||
else array
|
||||
|
@ -28,16 +25,6 @@ module PaperTrail
|
|||
|
||||
private
|
||||
|
||||
def active_record_pre_502?
|
||||
::ActiveRecord.gem_version < Gem::Version.new("5.0.2")
|
||||
end
|
||||
|
||||
def serialize_with_ar(array)
|
||||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.
|
||||
new(@subtype, @delimiter).
|
||||
serialize(array)
|
||||
end
|
||||
|
||||
def deserialize_with_ar(array)
|
||||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.
|
||||
new(@subtype, @delimiter).
|
||||
|
|
|
@ -14,12 +14,7 @@ module PaperTrail
|
|||
extend ::ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
if ::ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :item, polymorphic: true, optional: true
|
||||
else
|
||||
belongs_to :item, polymorphic: true
|
||||
end
|
||||
|
||||
belongs_to :item, polymorphic: true, optional: true
|
||||
validates_presence_of :event
|
||||
after_create :enforce_version_limit!
|
||||
end
|
||||
|
|
|
@ -22,14 +22,11 @@ class BeforeDestroyModifier < CallbackModifier
|
|||
paper_trail.on_destroy :before
|
||||
end
|
||||
|
||||
if ActiveRecord.gem_version < Gem::Version.new("5") ||
|
||||
!ActiveRecord::Base.belongs_to_required_by_default
|
||||
|
||||
unless ActiveRecord::Base.belongs_to_required_by_default
|
||||
class AfterDestroyModifier < CallbackModifier
|
||||
has_paper_trail on: []
|
||||
paper_trail.on_destroy :after
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class NoArgDestroyModifier < CallbackModifier
|
||||
|
|
|
@ -8,16 +8,8 @@ module Family
|
|||
has_many :children, class_name: "::Family::Family", foreign_key: :parent_id
|
||||
has_many :grandsons, through: :familie_lines
|
||||
has_one :mentee, class_name: "::Family::Family", foreign_key: :partner_id
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :parent, class_name: "::Family::Family", foreign_key: :parent_id, optional: true
|
||||
else
|
||||
belongs_to :parent, class_name: "::Family::Family", foreign_key: :parent_id
|
||||
end
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :mentor, class_name: "::Family::Family", foreign_key: :partner_id, optional: true
|
||||
else
|
||||
belongs_to :mentor, class_name: "::Family::Family", foreign_key: :partner_id
|
||||
end
|
||||
belongs_to :parent, class_name: "::Family::Family", foreign_key: :parent_id, optional: true
|
||||
belongs_to :mentor, class_name: "::Family::Family", foreign_key: :partner_id, optional: true
|
||||
|
||||
accepts_nested_attributes_for :mentee
|
||||
accepts_nested_attributes_for :children
|
||||
|
|
|
@ -3,19 +3,13 @@
|
|||
module Family
|
||||
class FamilyLine < ActiveRecord::Base
|
||||
has_paper_trail
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :parent, class_name: "::Family::Family", foreign_key: :parent_id, optional: true
|
||||
else
|
||||
belongs_to :parent, class_name: "::Family::Family", foreign_key: :parent_id
|
||||
end
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :grandson, class_name: "::Family::Family",
|
||||
foreign_key: :grandson_id,
|
||||
optional: true
|
||||
else
|
||||
belongs_to :grandson, class_name: "::Family::Family",
|
||||
foreign_key: :grandson_id
|
||||
end
|
||||
belongs_to :parent,
|
||||
class_name: "::Family::Family",
|
||||
foreign_key: :parent_id,
|
||||
optional: true
|
||||
belongs_to :grandson,
|
||||
class_name: "::Family::Family",
|
||||
foreign_key: :grandson_id,
|
||||
optional: true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Fluxor < ActiveRecord::Base
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :widget, optional: true
|
||||
else
|
||||
belongs_to :widget
|
||||
end
|
||||
belongs_to :widget, optional: true
|
||||
end
|
||||
|
|
|
@ -14,11 +14,7 @@ class Person < ActiveRecord::Base
|
|||
|
||||
has_one :thing
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id, optional: true
|
||||
else
|
||||
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id
|
||||
end
|
||||
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id, optional: true
|
||||
|
||||
has_paper_trail
|
||||
|
||||
|
|
|
@ -1,16 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Pet < ActiveRecord::Base
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :owner, class_name: "Person", optional: true
|
||||
else
|
||||
belongs_to :owner, class_name: "Person"
|
||||
end
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :animal, optional: true
|
||||
else
|
||||
belongs_to :animal
|
||||
end
|
||||
|
||||
belongs_to :owner, class_name: "Person", optional: true
|
||||
belongs_to :animal, optional: true
|
||||
has_paper_trail
|
||||
end
|
||||
|
|
|
@ -1,25 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module OverrideSongAttributesTheRails4Way
|
||||
def attributes
|
||||
if name
|
||||
super.merge(name: name)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def changed_attributes
|
||||
if name
|
||||
super.merge(name: name)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Song < ActiveRecord::Base
|
||||
has_paper_trail
|
||||
attribute :name, :string
|
||||
|
||||
# Uses an integer of seconds to hold the length of the song
|
||||
def length=(minutes)
|
||||
|
@ -29,12 +12,4 @@ class Song < ActiveRecord::Base
|
|||
def length
|
||||
read_attribute(:length) / 60
|
||||
end
|
||||
|
||||
if ActiveRecord::VERSION::MAJOR >= 5
|
||||
attribute :name, :string
|
||||
else
|
||||
attr_accessor :name
|
||||
|
||||
prepend OverrideSongAttributesTheRails4Way
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,10 +5,5 @@ class Thing < ActiveRecord::Base
|
|||
scope: -> { order("id desc") },
|
||||
extend: PrefixVersionsInspectWithCount
|
||||
}
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :person, optional: true
|
||||
else
|
||||
belongs_to :person
|
||||
end
|
||||
belongs_to :person, optional: true
|
||||
end
|
||||
|
|
|
@ -3,10 +3,5 @@
|
|||
class Vehicle < ActiveRecord::Base
|
||||
# This STI parent class specifically does not call `has_paper_trail`.
|
||||
# Of its sub-classes, only `Car` and `Bicycle` are versioned.
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :owner, class_name: "Person", optional: true
|
||||
else
|
||||
belongs_to :owner, class_name: "Person"
|
||||
end
|
||||
belongs_to :owner, class_name: "Person", optional: true
|
||||
end
|
||||
|
|
|
@ -2,10 +2,5 @@
|
|||
|
||||
class Whatchamajigger < ActiveRecord::Base
|
||||
has_paper_trail
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :owner, polymorphic: true, optional: true
|
||||
else
|
||||
belongs_to :owner, polymorphic: true
|
||||
end
|
||||
belongs_to :owner, polymorphic: true, optional: true
|
||||
end
|
||||
|
|
|
@ -3,11 +3,7 @@
|
|||
class Wotsit < ActiveRecord::Base
|
||||
has_paper_trail
|
||||
|
||||
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
|
||||
belongs_to :widget, optional: true
|
||||
else
|
||||
belongs_to :widget
|
||||
end
|
||||
belongs_to :widget, optional: true
|
||||
|
||||
def created_on
|
||||
created_at.to_date
|
||||
|
|
|
@ -22,11 +22,7 @@ RSpec.describe PaperTrail::InstallGenerator, type: :generator do
|
|||
expected_parent_class = lambda {
|
||||
old_school = "ActiveRecord::Migration"
|
||||
ar_version = ActiveRecord::VERSION
|
||||
if ar_version::MAJOR >= 5
|
||||
format("%s[%d.%d]", old_school, ar_version::MAJOR, ar_version::MINOR)
|
||||
else
|
||||
old_school
|
||||
end
|
||||
format("%s[%d.%d]", old_school, ar_version::MAJOR, ar_version::MINOR)
|
||||
}.call
|
||||
expected_create_table_options = lambda {
|
||||
if described_class::MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
|
||||
|
|
|
@ -17,9 +17,7 @@ RSpec.describe CallbackModifier, type: :model, versioning: true do
|
|||
end
|
||||
end
|
||||
|
||||
if ActiveRecord.gem_version < Gem::Version.new("5") ||
|
||||
!ActiveRecord::Base.belongs_to_required_by_default
|
||||
|
||||
unless ActiveRecord::Base.belongs_to_required_by_default
|
||||
context "when :after" do
|
||||
it "creates the version after destroy" do
|
||||
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
|
||||
|
@ -27,7 +25,6 @@ RSpec.describe CallbackModifier, type: :model, versioning: true do
|
|||
expect(modifier.versions.last.reify).to be_flagged_deleted
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when no argument" do
|
||||
|
|
|
@ -74,9 +74,8 @@ RSpec.describe Person, type: :model, versioning: true do
|
|||
person.save!
|
||||
person.assign_attributes(time_zone: "Pacific Time (US & Canada)")
|
||||
person.save!
|
||||
max_len = ActiveRecord::VERSION::MAJOR < 4 ? 105 : 118
|
||||
len = person.versions.last.object_changes.length
|
||||
expect((len < max_len)).to(be_truthy)
|
||||
expect(len < 118).to eq(true)
|
||||
end
|
||||
|
||||
it "version.object attribute should have stored value from serializer" do
|
||||
|
|
|
@ -110,14 +110,12 @@ module PaperTrail
|
|||
|
||||
context "changing the data type of database columns on the fly" do
|
||||
# TODO: Changing the data type of these database columns in the middle
|
||||
# of the test suite adds a fair amount of complication. Is there a better
|
||||
# of the test suite adds a fair amount of complexity. Is there a better
|
||||
# way? We already have a `json_versions` table in our tests, maybe we
|
||||
# could use that and add a `jsonb_versions` table?
|
||||
column_overrides = [false]
|
||||
if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 4
|
||||
column_overrides << "json"
|
||||
# 'jsonb' column types are only supported for ActiveRecord 4.2+
|
||||
column_overrides << "jsonb" if ::ActiveRecord::VERSION::STRING >= "4.2"
|
||||
if ENV["DB"] == "postgres"
|
||||
column_overrides += %w[json jsonb]
|
||||
end
|
||||
|
||||
column_overrides.shuffle.each do |column_datatype_override|
|
||||
|
|
|
@ -5,7 +5,7 @@ require "spec_helper"
|
|||
module PaperTrail
|
||||
module AttributeSerializers
|
||||
::RSpec.describe ObjectAttribute do
|
||||
if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 5
|
||||
if ENV["DB"] == "postgres"
|
||||
describe "postgres-specific column types" do
|
||||
describe "#serialize" do
|
||||
it "serializes a postgres array into a plain array" do
|
||||
|
|
|
@ -477,9 +477,6 @@ RSpec.describe(::PaperTrail, versioning: true) do
|
|||
end
|
||||
|
||||
it "reify with the correct type" do
|
||||
if ActiveRecord::VERSION::MAJOR < 4
|
||||
assert_kind_of(FooWidget, foo.versions.last.reify)
|
||||
end
|
||||
expect(PaperTrail::Version.last.previous).to(eq(foo.versions.first))
|
||||
expect(PaperTrail::Version.last.next).to(be_nil)
|
||||
end
|
||||
|
|
|
@ -22,17 +22,17 @@ class PaperTrailSpecMigrator
|
|||
# in rails 5.2. This is an undocumented change, AFAICT. Then again,
|
||||
# how many people use the programmatic interface? Most people probably
|
||||
# just use rake. Maybe we're doing it wrong.
|
||||
#
|
||||
# See also discussion in https://github.com/rails/rails/pull/40806, when
|
||||
# MigrationContext#migrate became public.
|
||||
def migrate
|
||||
v = ::ActiveRecord.gem_version
|
||||
if v >= ::Gem::Version.new("6.0.0.rc2")
|
||||
if ::ActiveRecord.gem_version >= ::Gem::Version.new("6.0.0.rc2")
|
||||
::ActiveRecord::MigrationContext.new(
|
||||
@migrations_path,
|
||||
::ActiveRecord::Base.connection.schema_migration
|
||||
).migrate
|
||||
elsif ::Gem::Requirement.new([">= 5.2.0.rc1", "< 6.0.0.rc2"]).satisfied_by?(v)
|
||||
::ActiveRecord::MigrationContext.new(@migrations_path).migrate
|
||||
else
|
||||
::ActiveRecord::Migrator.migrate(@migrations_path)
|
||||
::ActiveRecord::MigrationContext.new(@migrations_path).migrate
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue