ualbertalib / acts_as_rdfable

MIT License
0 stars 1 forks source link

Use `.class` instead of `.singleton_class` due to issue with Rails 7 #14

Closed murny closed 1 year ago

murny commented 1 year ago

From the Rails 7 upgrade in Jupiter, this gem is causing issues due to a change in how .singleton_class works:

https://github.com/ualbertalib/jupiter/pull/3250#issuecomment-1755980677

Tests are failing due to ActsAsRDF gem. We are seeing errors like this:

/Users/murny/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.0.8/lib/active_record/model_schema.rb:633:in `compute_table_name': undefined method `table_name' for nil:NilClass (NoMethodError)

            base_class.table_name
                      ^^^^^^^^^^^
  from /Users/murny/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.0.8/lib/active_record/model_schema.rb:284:in `reset_table_name'
  from /Users/murny/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.0.8/lib/active_record/model_schema.rb:248:in `table_name'
  from /Users/murny/jupiter/app/services/rdf_graph_creation_service.rb:5:in `get_prefixed_predicates'

I believe the issue from troubleshooting is due to the way we were relying on singleton_class in this gem to get the "class" of the model. It appears something broke in a newer version in Rails where the singleton_class no longer returns the correct data back.

I believe this is related to this issue which was closed with no action: https://github.com/rails/rails/issues/38293. Which is where the singleton_class no longer responds to .inspect with a similar error.

A simple solution is just use self.class instead of self.singleton_class for getting the models class. Will have to investigate if theirs any side effects from making this change.

Also feel like this gem could be improved. We have multiple ways to "register" the gem into models, not sure why we have many ways? (Activerecord base include and add_annotation_bindings! etc)

So as mentioned, we can just use .class instead of .singleton_class to fix this issue with how .singleton_class is now working in Rails 7.

This is the minimal amount of changes required to fix this issue for Rails 7 in Jupiter. I manually tested every method that could have been affected with this change inside acts_as_rdfable_core.rb and acts_as_rdfable.rb. Everything is working as intended.

This gem needs love though:

murny commented 1 year ago

Rails 7 Upgrade branch is now passing when using the fix in this pull request: https://github.com/ualbertalib/jupiter/pull/3250