 Doesn't patch "User" model in a plugin (Redmine 2.0)
Doesn't patch "User" model in a plugin (Redmine 2.0)
Added by Vladimir Pitin over 13 years ago
Sorry for my English :)
I have a plugin that works in "Redmine 1.3.0.stable.24228 (MySQL)" 
This plugin extend a model "User". And it worked fine.
Now we have installed Redmine 2.0.0
I try rewrite my plugin, but extending model "User" call error "Expected /usr/share/srv-redmine/redmine-2.0/app/models/user.rb to define User"
This init.rb code:
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
#require 'user'
ActionDispatch::Callbacks.to_prepare do
    User.send(:include, AdvancedRoadmap::UserPatch)
end
	If I add 
require 'user'
I have error:
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___374889370_87506190'
  app/controllers/projects_controller.rb:168:in `show'
	This's content of file "lib/advanced_roadmap/user_patch.rb"
require_dependency "user" 
module AdvancedRoadmap
  module UserPatch
    def self.included(base)
        base.extend(ClassMethods)
        base.send(:include, InstanceMethods)        
      base.class_eval do 
      end
    end
    module ClassMethods   
    end
    module InstanceMethods
    end        
  end
end
	It's my settings
root@redmine:/usr/share/srv-redmine/redmine-2.0/log# RAILS_ENV=development rake about (in /usr/share/srv-redmine/redmine-2.0) About your application's environment Ruby version 1.9.3 (i686-linux) RubyGems version 1.8.24 Rack version 1.4 Rails version 3.2.3 Active Record version 3.2.3 Action Pack version 3.2.3 Active Resource version 3.2.3 Action Mailer version 3.2.3 Active Support version 3.2.3 Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x9b232d0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport, OpenIdAuthentication Application root /usr/share/srv-redmine/redmine-2.0 Environment development Database adapter mysql2 Database schema version 20120422150750
Also I use: RVM (version 3.*), OS - Ubuntu 10.10
Any help is appreciated.
Replies (5)
     RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Kirill Bezrukov (RedmineUP) over 13 years ago
    RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Kirill Bezrukov (RedmineUP) over 13 years ago
  
  +1 
I have the same issues with my plugins
     RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Daniel Munn over 13 years ago
    RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Daniel Munn over 13 years ago
  
  I've been placing it within:
Rails.application.config.to_prepare do end
Haven't noted any troubles
     RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Vladimir Pitin over 13 years ago
    RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Vladimir Pitin over 13 years ago
  
  I tried this:
Rails.application.config.to_prepare do end
But I've had same troubles.
Expected /usr/share/srv-redmine/redmine-2.0/app/models/user.rb to define User
Also, I created the deffect http://www.redmine.org/issues/11035
Other models is extending fine.
     RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Daniel Munn over 13 years ago
    RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Daniel Munn over 13 years ago
  
  As commented on the defect:
This works:
lib/redmine_test/patches/user_patch.rb
require_dependency 'project'
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
After which the User.echoTest method exists - you need to add
require_dependency 'principal' before require_dependency 'user'
     RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Vivek Jain over 12 years ago
    RE: Doesn't patch "User" model in a plugin (Redmine 2.0)
    -
    Added by Vivek Jain over 12 years ago
  
  Hi
I tried patching the User with a method all_business_unitdef all_business_units
  ...
  end
the patch is defined as :
require  'project'
require  'principal'
require 'user'
def self.included(base)
        require 'net/ldap'
        puts "loaded UserPatch" 
        base.send(:extend, ClassMethods)
        base.send(:include, InstanceMethods)
        base.class_eval do
          unloadable
        end
end
module ClassMethods
      def all_business_units
        #logic
      end
end
ActionDispatch::Callbacks.to_prepare do
    User.send(:include, RedminePlugin::Patches::UserPatch)
end
	When the class gets loaded it displays the log statements however when I tried refer it from my controller like
User.all_business_units
it gives me the following error
NoMethodError (undefined method `all_business_units' for #<Class:0x560bf08>):
  activerecord (3.2.13) lib/active_record/dynamic_matchers.rb:55:in `method_missing'
I am using redmine 2.3.2
Please suggest