From c2c0f4e5262c9bfedea42bf47e5025f927ce86f1 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Sun, 21 Mar 2021 00:21:02 -0400 Subject: [PATCH] Style/ClassEqualityComparison --- .rubocop_todo.yml | 4 --- .../attribute_serializer_factory.rb | 34 +++++++++++++------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4cf4f2d9..07b2335f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -153,7 +153,3 @@ RSpec/FilePath: Exclude: - 'spec/paper_trail/model_spec.rb' - 'spec/paper_trail/serializer_spec.rb' - -Style/ClassEqualityComparison: - Exclude: - - 'lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb' diff --git a/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb b/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb index eb665876..9c73c65e 100644 --- a/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb +++ b/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb @@ -8,18 +8,32 @@ module PaperTrail # not suited for writing JSON to a text column. This factory # replaces certain default Active Record serializers # with custom PaperTrail ones. + # + # @api private module AttributeSerializerFactory - AR_PG_ARRAY_CLASS = "ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array" + class << self + # @api private + def for(klass, attr) + active_record_serializer = klass.type_for_attribute(attr) + if ar_pg_array?(active_record_serializer) + TypeSerializers::PostgresArraySerializer.new( + active_record_serializer.subtype, + active_record_serializer.delimiter + ) + else + active_record_serializer + end + end - def self.for(klass, attr) - active_record_serializer = klass.type_for_attribute(attr) - if active_record_serializer.class.name == AR_PG_ARRAY_CLASS - TypeSerializers::PostgresArraySerializer.new( - active_record_serializer.subtype, - active_record_serializer.delimiter - ) - else - active_record_serializer + private + + # @api private + def ar_pg_array?(obj) + if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array) + obj.instance_of?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array) + else + false + end end end end