Extend a Issue Model Function

Added by Nico Landgraf over 5 years ago

Hello,

I created a Plugin to import some stuff in Redmine and now it is required to extend a Model function in the Issue-Model (the update_closed_on).
So I checked the Development Guide and found some Code exampels but my Plugin code was not called.
I write the code direct in the Model to check if the syntax was wrong but all works fine.

I performed the follwing Steps:
- Create a Plugin in Plugins folder
- Create a File issue_patch.rb in the lib folder from the Plugin
- Add the following code to the 'issue_patch.rb'

require_dependency 'issue'
module IssuePatch
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable
      alias_method_chain :update_closed_on, :update_closed_on_with_patch
    end
  end

  module InstanceMethods
    def update_closed_on_with_patch
      test = CustomValue.where("customized_id = #{self.id} AND customized_type = 'Issue' AND custom_field_id = 36").first
      if !test.nil?
        test.value = Date.today
        test.save
      end
    end
  end
end

Issue.send(:include, IssuePatch)

It seems to me that the code in my issue_patch was never called when the 'update_closed_on' was called.
Can anyone see a Problem un my function?

Replies (1)

RE: Extend a Issue Model Function - Added by Martin Dubé over 5 years ago

What's in your init.rb file?

I'm more familiar at modifying existing plugins than writing from scratch, but I'd say that something is missing from your init file. Probably like this:

require_dependency 'Plugin/issue_patch'

(1-1/1)