Project

General

Profile

test if a custom field value has been changed in a after_save hook

Added by Florent Fievez over 12 years ago

Hello,

I have developed a plugin internal for my company. The plugin compute the value of a custom field for the whole issue tree. I want to optimize this process by testing if the custom field value has been changed before launching the recursive update.

Currently I have a plugin with a after_save hook which call a method on the parent issue if it exists. The method simply sum values of each leaf issue custom field. This is very similar to what redmine does for "estimated_time". But with large issue tree it can take some time, so i wan't to apply the change only if the custom field value has been changed.

My code:

module CompanyComputeRemaining
  module IssuePatch
    def self.included(base)
      base.extend(ClassMethods)

      base.send(:include, InstanceMethods)

      base.class_eval do
        after_save :compute_parent_remaining
        after_destroy :compute_parent_remaining
      end
    end
  end

  module ClassMethods
  end

  module InstanceMethods
    def compute_parent_remaining
        if self.project.module_enabled?(:company_compute_remaining)
                compute_remaining_for(parent_id) if parent_id
        end
    end
    def compute_remaining_for(issue_id)
        if issue_id && p = Issue.find_by_id(issue_id)
                sum = 0.0
                p.leaves.each do |leaf|
                        custom_value_f = 0
                        custom_value_f = leaf.custom_value_for(23).value.to_f if leaf.custom_value_for(23)
                        sum += custom_value_f
                end
                p.custom_value_for(23).value = sum.round(2)
                p.save(false)
        end
    end
  end
end

So I want to know how I can test if custom_value_for(23) has been changed before calling compute_remaining_for.

Thanks in advance,
Best regards,