0

I want to override a method from Redmine Core using a Patch in a plugin but I'm not able to make it working :-(.

I want to change the behavior of the method try_to_login from the Redmine core.

Here is my Init.rb:

require 'redmine'
require 'user_patch'

ActionDispatch::Callbacks.to_prepare do
  require_dependency 'project'
  require_dependency 'principal'
  require_dependency 'user'
  unless User.included_modules.include? UserPatch
    User.send :include, UserPatch
  end
end

Redmine::Plugin.register :security do
  name 'Security 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

and my patch file:

module UserPatch

    def self.included(base)
    base.send(:include, InstanceMethods)
        base.class_eval do
            alias_method :try_to_login, :try_to_login_with_patch        
        end
    end 

    module InstanceMethods
      # Returns the user that matches provided login and password, or nil
      def try_to_login_with_patch(login, password, active_only=true)
            <do somthing ...>
      end


    end
end

Any idea what is wrong in this ?

Thanks you in avance.

Regards,

Aniss

1 Answers1

0

i've done this already in my plugin: https://github.com/berti92/mega_calendar

init.rb - within Redmin::Plugin.register...

Rails.configuration.to_prepare do 
  UsersController.send(:include, UsersControllerPatch)
end

in the lib folder create a file "issues_controller_patch.rb"

module IssuesControllerPatch
  def self.included(base)
    base.class_eval do
      # Insert overrides here, for example:
      def create_with_plugin
        create_without_plugin
        ## your code ##
      end
      def update_with_plugin
        update_without_plugin
        ## your code ##
      end
      alias_method_chain :update, :plugin
      alias_method_chain :create, :plugin
    end
  end
end

create_without_plugin means the original code will be executed. If you dont want it, then delete the create_without_plugin and update_without_plugin

Berti92
  • 441
  • 1
  • 6
  • 12