Defect #33206

Unable to autoload constant Version.table_name if gems uses Version class

Added by Alexander Meindl about 1 year ago. Updated about 1 month ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:Gems support
Target version:4.1.2
Resolution:Fixed Affected version:

Description

Introduced with r18780 Version.table_name is not available, if a gem package (like attr_encrypted) is used with a class named "Version".

At the moment this problem does not exist with Gem packages from Redmine itself, but gems from plugins.

Existing tests are broken, e.g.

bin/rails test test/unit/project_test.rb -n test_move_a_root_project_to_a_project
# Running:

E

Error:
ProjectTest#test_move_a_root_project_to_a_project:
NoMethodError: undefined method `table_name' for AttrEncrypted::Version:Module
    app/models/issue.rb:1739:in `update_versions'
    app/models/issue.rb:1385:in `update_versions_from_hierarchy_change'
    app/models/project.rb:935:in `update_versions_from_hierarchy_change'
    app/models/project.rb:424:in `set_parent!'
    test/unit/project_test.rb:316:in `test_move_a_root_project_to_a_project'

Similar problem was #23269. A better description of the problem with examples are here: https://stackoverflow.com/questions/21115567/autoloading-classes-using-class-self-in-rails

I added a patch for a more solid solution to prevent the problem. It would be great to have this change in Redmine.

full_qualified_version_class.patch Magnifier (858 Bytes) Alexander Meindl, 2020-03-29 08:54

full_qualified_version_class_version2.patch Magnifier (1.46 KB) Alexander Meindl, 2020-03-29 09:33

Associated revisions

Revision 20760
Added by Go MAEDA about 1 month ago

Unable to autoload constant Version.table_name if gems use Version class (#33206).

Patch by Alexander Meindl.

Revision 20761
Added by Go MAEDA about 1 month ago

Merged r20760 from trunk to 4.1-stable (#33206).

History

#1 Updated by Alexander Meindl about 1 year ago

I added another version of patch, which uses Rails 5 where.not and Arel::Table. With this solution, no more table name specification is required.

Both solutions work, but second one is more cleaner - but more refactoring.

#2 Updated by Go MAEDA about 1 month ago

  • Tracker changed from Patch to Defect
  • Status changed from New to Confirmed
  • Target version set to Candidate for next minor release

Steps to reproduce the issue:

echo gem \'attr_encrypted\' >> Gemfile.local
bundle install
bin/rails test test/unit/project_test.rb -n test_move_a_root_project_to_a_project

#3 Updated by Pavel Rosick√Ĺ about 1 month ago

I think the first variant should be preferred, see https://github.com/rails/rails/issues/36761

#4 Updated by Go MAEDA about 1 month ago

  • Target version changed from Candidate for next minor release to 4.0.8

I will commit the former patch, full_qualified_version_class.patch soon.

#5 Updated by Go MAEDA about 1 month ago

  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch. Thank you for reporting and fixing the issue.

#6 Updated by Go MAEDA about 1 month ago

  • Status changed from Resolved to Closed
  • Target version changed from 4.0.8 to 4.1.2

Also available in: Atom PDF