Content
[Solved] Patching User: uninitialized constant Project::User
Added by Oliver Günther almost 11 years ago
While working on a new plugin with a patch for User, I’m experiencing an issue I have no explanation for and am looking for a solution to a blatant mistake, as I can’t find it.
The steps I’ve taken:
- Clone opf/openproject at dev(02a79032)
- generate a plugin
bundle exec rails generate open_project:plugin test_plugin ../plugins/ - Added
patches [:User]to engine.rb - Added
lib/open_project/test_plugin/patches/user_patch.rb, which adds a simple instance methodfoo - Add the plugin to
Gemfile.plugins - Start the server (foreman start -f Profile.dev)
The first request is returned correctly with the patch included to User. At all times, the second request fails with the following exception :
uninitialized constant Project::User
Trace:
Application Trace | Framework Trace | Full Trace app/models/project.rb:48:in `<class:Project>' app/models/project.rb:30:in `<top (required)>' app/models/principal.rb:36:in `<class:Principal>' app/models/principal.rb:30:in `<top (required)>' app/models/user.rb:32:in `<top (required)>' lib/open_project/plugins/acts_as_op_engine.rb:66:in `block (3 levels) in included' lib/open_project/plugins/acts_as_op_engine.rb:63:in `each' lib/open_project/plugins/acts_as_op_engine.rb:63:in `block (2 levels) in included'
The error occurs in the lib/open_project/plugins/acts_as_op_engine.rb in the constantize of User in the following line:
klass = klass_name.to_s.constantize
Things I’ve tested:
- Moved back to stable —> same error
- Tested a new stack/vm with a clean clone —> same error
- Loading only my other plugins (e.g., openproject-revisions_git) with a user patch —> works.
I can’t explain that one. The revisions_git plugin does not use the patch registry, butrequire_dependencyand manualUser.send. However, if I use that method in the TestPlugin, it still fails.
-
Loading the test plugin WITH my other plugins (i.e., multiple User patches) —> works fine
I definitely can’t explain that one..
I’ve added the plugin as an attachment.
Attachments
lib/open_project/test_plugin/engine.rb
# Prevent load-order problems in case openproject-plugins is listed after a plugin in the Gemfile
# or not at all
require 'open_project/plugins'
module OpenProject::TestPlugin
class Engine < ::Rails::Engine
engine_name :openproject_test_plugin
include OpenProject::Plugins::ActsAsOpEngine
register 'openproject-test_plugin',
:author_url => 'http://finn.de',
:requires_openproject => '>= 3.0.0pre13'
patches [:User]
end
end
lib/open_project/test_plugin/patches/user_patch.rb
module OpenProject::TestPlugin
module Patches
module UserPatch
def self.included(base)
base.class_eval do
unloadable
include InstanceMethods
end
end
module InstanceMethods
def foo
"bar from #{login}"
end
end
end
end
end
Replies (2)
Hi Oliver,
I think the problem has to do with loading the constants.
There is some weird dependency between Principal, User and Project and depending on when everything is loaded, you can run into problems. At least that happened in the old days when we worked on some plugins changing the User model.
We tried to remove the problem by having some require statements somewhere I can’t remember right now. But apparently, this does not solve your problem.
You should be able to add some require statements to the User patch or to the engine (the later probably). I think it was:
but the order of those might be different.
Hope to at least get you started. Otherwise, ask again.
Hi Jens,
thanks, that does indeed fix the problem!
I’ve tried
require_dependency 'user'already, but was unaware that the other two dependencies would solve it.Should others experience the same issues, If you use
require, loading the app displays all sorts of redefined warnings.Using
require_dependencyresolves that.