Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warning for serialize in rails 7.1+ #811

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- 'main'
pull_request:
workflow_dispatch:
jobs:
test:
strategy:
Expand Down
5 changes: 5 additions & 0 deletions lib/globalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Globalize
ACTIVE_RECORD_52 = Gem::Version.new('5.2.0')
ACTIVE_RECORD_60 = Gem::Version.new('6.0.0')
ACTIVE_RECORD_61 = Gem::Version.new('6.1.0')
ACTIVE_RECORD_71 = Gem::Version.new('7.1.0')

CURRENT_RUBY = Gem::Version.new(RUBY_VERSION)
RUBY_VERSION_27 = Gem::Version.new('2.7.0')
Expand Down Expand Up @@ -96,6 +97,10 @@ def rails_61?
::ActiveRecord.version >= ACTIVE_RECORD_61
end

def rails_71?
::ActiveRecord.version >= ACTIVE_RECORD_71
end

protected

def read_locale
Expand Down
24 changes: 24 additions & 0 deletions lib/patches/active_record/rails7_1/serialization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Globalize
module AttributeMethods
module Serialization
def serialize(attr_name, class_name_or_coder = Object, **options)
if options[:coder].blank?
if class_name_or_coder == ::JSON || [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
options = options.merge(coder: class_name_or_coder, type: Object)
else
options = options.merge(coder: default_column_serializer, type: class_name_or_coder)
end
end

super(attr_name, **options)

coder = build_column_serializer(attr_name, options[:coder], options[:type], options[:yaml])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided to use the build_column_serializer private method on the class


self.globalize_serialized_attributes = globalize_serialized_attributes.dup
self.globalize_serialized_attributes[attr_name] = coder
end
end
end
end

ActiveRecord::AttributeMethods::Serialization::ClassMethods.send(:prepend, Globalize::AttributeMethods::Serialization)
45 changes: 45 additions & 0 deletions lib/patches/active_record/rails7_1/uniqueness_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Globalize
module Validations
module UniquenessValidator
def validate_each(record, attribute, value)
klass = record.class
if klass.translates? && klass.translated?(attribute)
finder_class = klass.translation_class
relation = build_relation(finder_class, attribute, value).where(locale: Globalize.locale)
relation = relation.where.not(klass.reflect_on_association(:translations).foreign_key => record.send(:id)) if record.persisted?


translated_scopes = Array(options[:scope]) & klass.translated_attribute_names
untranslated_scopes = Array(options[:scope]) - translated_scopes

relation = relation.joins(:globalized_model) if untranslated_scopes.present?
untranslated_scopes.each do |scope_item|
scope_value = record.send(scope_item)
reflection = klass.reflect_on_association(scope_item)
if reflection
scope_value = record.send(reflection.foreign_key)
scope_item = reflection.foreign_key
end
relation = relation.where(find_finder_class_for(record).table_name => { scope_item => scope_value })
end

translated_scopes.each do |scope_item|
scope_value = record.send(scope_item)
relation = relation.where(scope_item => scope_value)
end
relation = relation.merge(options[:conditions]) if options[:conditions]

if relation.exists?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
record.errors.add(attribute, :taken, **error_options)
end
else
super(record, attribute, value)
end
end
end
end
end

ActiveRecord::Validations::UniquenessValidator.prepend Globalize::Validations::UniquenessValidator
4 changes: 3 additions & 1 deletion lib/patches/active_record/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require_relative 'rails4/serialization'
elsif ::ActiveRecord.version < Gem::Version.new("6.1.0")
require_relative 'rails5_1/serialization'
else
elsif ::ActiveRecord.version < Gem::Version.new("7.1.0")
require_relative 'rails6_1/serialization'
else
require_relative 'rails7_1/serialization'
end