https://www.redmine.org/https://www.redmine.org/favicon.ico?16793021292012-05-29T04:03:45ZRedmineRedmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385182012-05-29T04:03:45ZAlex Shulgin
<ul></ul><blockquote>
<p>#require 'user'</p>
<p>ActionDispatch::Callbacks.to_prepare do<br />User.send(:include, AdvancedRoadmap::UserPatch)<br />end</p>
</blockquote>
<p>Hm, did you try this instead:</p>
<pre>
require 'dispatcher'
Dispatcher.to_prepare do
require_dependency 'user'
User.send(:include, AdvancedRoadmap::UserPatch)
end
</pre>
<p>That seems pretty idiomatic to me.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385192012-05-29T04:11:05ZVladimir Pitin
<ul></ul><p>If i do this</p>
<pre>
require 'dispatcher'
Dispatcher.to_prepare do
require_dependency 'user'
User.send(:include, AdvancedRoadmap::UserPatch)
end
</pre>
<p>I cause error</p>
<pre>
cannot load such file -- dispatcher
</pre> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385222012-05-29T07:49:22ZEtienne Massip
<ul></ul><p>First code seemed OK, related to <a href="https://github.com/rails/rails/issues/3847" class="external">this rails issue</a> if it is an issue; try to clear your caches as suggested?</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385232012-05-29T07:49:34ZEtienne Massip
<ul><li><strong>Category</strong> changed from <i>Plugin Request</i> to <i>Plugin API</i></li><li><strong>Affected version (unused)</strong> set to <i>2.0.0</i></li><li><strong>Affected version</strong> set to <i>2.0.0</i></li></ul> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385242012-05-29T08:08:58ZFabrice ROBIN
<ul></ul><p>Vladimir Pitin wrote:</p>
<blockquote>
<p>If i do this</p>
<p>[...]</p>
<p>I cause error</p>
<p>[...]</p>
</blockquote>
<p>In fact, 'dispatcher' does not exist anymore in rails 3. <br />Redmine documentation for plugin development needs to be updated.<br />Try this instead:</p>
<pre>
Rails.configuration.to_prepare do
require_dependency 'user'
User.send(:include, AdvancedRoadmap::UserPatch)
end
</pre> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385252012-05-29T08:42:30ZEtienne Massip
<ul></ul><p>Fabrice ROBIN wrote:</p>
<blockquote>
<p>Redmine documentation for plugin development needs to be updated.</p>
</blockquote>
<p>I don't think that Redmine documentation actually refers to the use of <code>dispatcher</code>?</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385262012-05-29T09:22:59ZFabrice ROBIN
<ul></ul><p>Exact, I was convinced that was the case.</p>
<p>But it might be a good idea to add something in redmine wiki.<br />The to_prepare method is a must have when a plugin needs to patch redmine core,<br />mainly in development mode.</p>
<p>Here is the documentation about the to_prepare method<br /><pre>
Add a preparation callback. Preparation callbacks are run before every
request in development mode, and before the first request in production
mode
</pre></p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385272012-05-29T09:31:20ZEtienne Massip
<ul></ul><p>This is described in RoR guides, mainly in <a class="external" href="http://guides.rubyonrails.org/configuring.html#initializers">http://guides.rubyonrails.org/configuring.html#initializers</a>.</p>
<p>Vladimir is right when he replaces the <code>dispatcher</code> requirement with <code>ActionDispatch::Callbacks.to_prepare</code>, see <a class="external" href="http://guides.rubyonrails.org/configuring.html#configuring-action-dispatch">http://guides.rubyonrails.org/configuring.html#configuring-action-dispatch</a> , this is the most accurate match.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385432012-05-29T22:47:25ZVladimir Pitin
<ul></ul><p>If I try it</p>
<pre>
Rails.configuration.to_prepare do
require_dependency 'user'
User.send(:include, AdvancedRoadmap::UserPatch)
end
</pre>
<p>I have same error</p>
<pre>
Expected /usr/share/srv-redmine/redmine-2.0/app/models/user.rb to define User
</pre> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385442012-05-29T22:56:21ZVladimir Pitin
<ul></ul><blockquote>
<p>First code seemed OK, related to this rails issue if it is an issue; try to clear your caches as suggested?</p>
</blockquote>
<p>No.How can I clear my cashes?</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385452012-05-29T23:01:36ZVladimir Pitin
<ul></ul><p>My folder /tmp/cache is empty. If it's meant?</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385462012-05-29T23:37:59ZVladimir Pitin
<ul></ul><p>Upgrade to Redmine 2.0.1 had no affect<br />Commands:</p>
<pre>
rake tmp:cache:clear
rake tmp:sessions:clear
</pre>
<p>had no affect, too</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385472012-05-30T06:33:08ZDaniel Munn
<ul></ul><p>This works:<br />lib/redmine_test/patches/user_patch.rb<br /><pre>
require_dependency 'principal'
require_dependency 'user'
module RedmineTest
module Patches
module UserPatch
def self.included(base)
base.send(:extend, ClassMethods)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
end
end
module ClassMethods
def echoTest
STDOUT.print "echo Test\n"
STDOUT.flush
nil
end
end
module InstanceMethods
end
end
end
end
ActionDispatch::Callbacks.to_prepare do
User.send(:include, RedmineTest::Patches::UserPatch)
end
</pre></p>
<p>After which the User.echoTest method exists - you need to add <code>require_dependency 'principal'</code> before <code>require_dependency 'user'</code></p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385502012-05-30T07:29:03ZVladimir Pitin
<ul></ul><p>init.rb</p>
<pre>
require 'redmine'
require 'advanced_roadmap/user_patch'
Rails.logger.info 'Starting Advanced Roadmap plugin for Redmine'
Redmine::Plugin.register :advanced_roadmap do
name 'Advanced Roadmap plugin'
author 'Author name'
description 'This is a plugin for Redmine'
version '0.0.1'
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
end
</pre><br />user_patch.rb<br /><pre>
#require_dependency "user"
require_dependency 'principal'
require_dependency 'user'
module AdvancedRoadmap
module UserPatch
def self.included(base)
base.send(:extend, ClassMethods)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
end
end
module ClassMethods
def echoTest
STDOUT.print "echo Test\n"
STDOUT.flush
nil
end
end
module InstanceMethods
end
end
end
ActionDispatch::Callbacks.to_prepare do
User.send(:include, AdvancedRoadmap::UserPatch)
end
</pre>
<p>It's not work. error's is same</p>
<pre>
ActionView::Template::Error (undefined method `association_class' for nil:NilClass):
40: <%= hidden_field_tag(controller.default_search_scope, 1, :id => nil) if controller.default_search_scope %>
41: <label for='q'>
42: <%= link_to l(:label_search), {:controller => 'search', :action => 'index', :id => @project}, :accesskey => accesskey(:search) %>:
43: </label>
44: <%= text_field_tag 'q', @question, :size => 20, :class => 'small', :accesskey => accesskey(:quick_search) %>
45: <% end %>
46: <%= render_project_jump_box %>
app/helpers/application_helper.rb:240:in `render_project_jump_box'
app/views/layouts/base.html.erb:43:in `_app_views_layouts_base_html_erb__947349986_81730330'
</pre> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385632012-05-30T17:08:16ZDaniel Munn
<ul></ul><p>Hi, looks like an un-resolved dependency on the project model (via member model):</p>
<pre>
require_dependency 'project'
require_dependency 'principal'
require_dependency 'user'
</pre>
<p>Seems to allow standard output, please confirm.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385852012-05-30T22:17:31ZVladimir Pitin
<ul></ul><p>It seems that it works fine. I will test it in more detail. In any case, this variant doesn't cause error.<br />Thanks!</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=385952012-05-31T11:59:22ZOleg Kandaurov
<ul></ul><p>Daniel, Thanks a lot. You soulution works for me.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=388832012-06-10T19:49:49ZJean-Baptiste Barth
<ul></ul><p>I confirm it works too, but it feels like black magic to me. If anybody knows why we should declare explicitly those dependencies manually, it would be great.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=389652012-06-13T00:20:17ZVladimir Pitin
<ul></ul><p>Jean-Baptiste Barth wrote:</p>
<blockquote>
<p>I confirm it works too, but it feels like black magic to me. If anybody knows why we should declare explicitly those dependencies manually, it would be great.</p>
</blockquote>
<p>+1</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=400152012-07-27T21:58:12ZJohn Kubiatowicz
<ul></ul><p>Is there a way to have something work with both Rails 2 and 3? I tried the ActionDispatch::Callbacks idiom in a Rails 2.3.14 installation and it doesn't seem to work.</p>
<p>What does seem to work in 2.3.14 is to use</p>
<pre>
config.to_prepare do
Monkey Patch things
end
</pre>
<p>Will this work in 3.0+? I'm just wondering if this is an idiom that might be usable in general (sorry if this is a dumb question -- I am bit hazy on the startup internals of Rails and plugins).</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=400162012-07-27T23:34:58ZJohn Kubiatowicz
<ul></ul><p>Actually, in fact, what about avoiding the callback entirely? If you do a:</p>
<pre>
require_dependency 'my_patch_file'
</pre>
<p>in the init.rb file and then leave your User.send by itself at the end of the patch file, shouldn't this do the right thing (i.e. loaded once in production mode and every requires in development mode)? It certainly seems to work in production mode....</p>
<p>Forgive me if this sounds clueless, just trying to understand options here...</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=426952012-11-07T00:43:30ZVladimir Pitin
<ul></ul><p>For information</p>
<p>If I write <br /><pre>
require_dependency 'project'
require_dependency 'principal'
require_dependency 'user'
</pre><br />in my plugin, the plugin <br /><a class="external" href="http://www.redmine.org/plugins/extended_fields">http://www.redmine.org/plugins/extended_fields</a> <br />stops working, because validation <br /><pre>
validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
</pre><br />doesn't pass.</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=471672013-03-20T16:50:00ZAnonymous
<ul></ul><p>It seems to me that the issue reported here has been overcome already; the remainder of discussion seems to resolve about a debate how to implement Monkey Patching in a way that work on both Rails 2 and 3 (although I don't understand why you would bother, RoR 3 is the future, RoR 4 is coming soon), and how to do it most elegantly etc.</p>
<p>So it appears this issue can be closed, can't it?</p> Redmine - Defect #11035: Doesn't patch "User" model in a pluginhttps://www.redmine.org/issues/11035?journal_id=490122013-05-01T22:58:03ZJean-Baptiste Barth
<ul></ul><p>Max: not really, the underlying issue (why we need to declare dependencies explicitly) isn't resolved</p>
<p>John: yes, you should use <code>config.to_prepare { <block> }</code> in Redmine 1.x/Rails 2.x, and <code>ActionDispatch::Callbacks.to_prepare { <block> }</code> above. Note that since we started this thread, support for Redmine 1.x has been removed, and only Redmine 2.3.x is supported at the moment (hence Rails 3.2+).</p>
<p>Vladimir: that's unexpected, it should work</p>