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

Conversation

jwoodrow
Copy link

@jwoodrow jwoodrow commented Oct 19, 2023

There was a warning that kept popping up because of how globalize was using serialize and the changes that are in effect in Rails 7.1 and will be permanently changed in Rails 7.2

DEPRECATION WARNING: Passing the class as positional argument is deprecated and will be removed in Rails 7.2.

Please pass the class as a keyword argument:

serialize :metadata, type: Object

This PR uses the ActiveRecord conditions used to display the deprecation message and uses it to handle the detection between coder/type

Also uses the build_column_serializer private method to build the coder assigned to globalize_serialized_attributes

Comment on lines 5 to 9
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
Copy link
Author

Choose a reason for hiding this comment

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

This is what ActiveRecord does to display the deprecation message & make the function still work


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

@jwoodrow
Copy link
Author

Capture d’écran 2023-11-12 à 17 46 35 tests are working on the fork

@jpawlyn
Copy link

jpawlyn commented Nov 14, 2023

Thanks for working on this @jwoodrow 👍🏼 . For the last commit fix tests we are seeing all tests fail in our app with the message:

Psych::DisallowedClass:
       Tried to dump unspecified class: ActiveSupport::HashWithIndifferentAccess

Not sure why this is but will add further details if we discover the reason. Without that commit, everything looks to be working fine.

@jwoodrow
Copy link
Author

That's odd especially because I haven't changed anything special and I haven't used any with_indifferent_access either 🤔

@jpawlyn
Copy link

jpawlyn commented Nov 15, 2023

Investigating the error above, it seems that when the ActiveStorage::Blob class is being loaded, the coder set in ActiveRecord::Store:

https://github.com/rails/rails/blob/6b93fff8af32ef5e91f4ec3cfffb081d0553faf0/activerecord/lib/active_record/store.rb#L108

is being overwritten by the class ActiveRecord::Coders::YAMLColumn with the call to options.merge(coder: ... before the call to super but this doesn't happen in the previous commit.

My tests fail when an instance of ActiveStorage::Blob is created. Not sure what the solution is though 🤷🏼 .

This is undoubtedly not the solution but when I added the following the tests did pass:

        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)

@jwoodrow
Copy link
Author

This is undoubtedly not the solution but when I added the following the tests did pass:

Kind of makes sense, if coder was provided by keyword chances are that there shouldn't be a need to determine a default value for coder or type 🤔

@jwoodrow
Copy link
Author

jwoodrow commented Jan 5, 2024

After trying to update one of our codebases that use globalize I finally encountered the same error with ActiveStorage::Blob. After adding the if options[:coder].blank? the specs still pass and the error no longer seems to happen so I've updated the code

@arikarim
Copy link

any Update on merging this?

@thomasdarde
Copy link

Hi Any update on this issue ? It's not clear what is required for checks to pass ?

@jpawlyn
Copy link

jpawlyn commented Mar 19, 2024

Hi Any update on this issue ? It's not clear what is required for checks to pass ?

We switched over to Mobility following this recommendation and that worked out pretty well and was fairly seemless.

@ilvez
Copy link

ilvez commented Mar 20, 2024

We switched over to Mobility following this recommendation and that worked out pretty well and was fairly seemless.

Thanks, how did I not notice this suggestion?! There is also migration guide: https://github.com/shioyama/mobility/wiki/Migrating-from-Globalize

EDIT: One thing that this migration should mention is that with_translations is missing feature. Some hints I picked up from here: shioyama/mobility#390 (comment) . Some of the queries, which were using with_translations(locale), I just split into regular queries like:

      .where(model_translations: { locale: })
      .where.not(model_translations: { name: nil })

.. or something similar.

javierm added a commit to javierm/consul that referenced this pull request Apr 14, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to javierm/consul that referenced this pull request Apr 14, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to javierm/consul that referenced this pull request Apr 14, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to javierm/consul that referenced this pull request Apr 15, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to javierm/consul that referenced this pull request Apr 15, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to consuldemocracy/consuldemocracy that referenced this pull request Apr 15, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to javierm/consul that referenced this pull request Apr 15, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
javierm added a commit to consuldemocracy/consuldemocracy that referenced this pull request Apr 15, 2024
```
Rendered shared/_social_media_meta_tags.html.erb
DEPRECATION WARNING: Passing the class as positional argument is
deprecated and will be removed in Rails 7.2.
Please pass the class as a keyword argument:
  serialize :metadata, type: Object
 (called from require at <internal:/rbenv/versions/3.2.3/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38)
```
@Splines
Copy link

Splines commented Apr 22, 2024

We switched over to Mobility following this recommendation and that worked out pretty well and was fairly seemless.

That's one option, thanks for pointing it out. It'd be awesome if this PR could be finalized/merged such that those who want to upgrade to Rails 7.1 and only after that switch to mobility in a separate step could do so without all these deprecation warnings.

Edit: At MaMpf, we switched from globalize to mobility recently and the transition was really easy and it works like a charm. Can recommend to do so as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants