From e78ab046ea4d245a6104e8e738cb521dd7265db7 Mon Sep 17 00:00:00 2001 From: Dmitry Yu Okunev Date: Mon, 6 Apr 2015 10:46:19 +0300 Subject: [PATCH] Replaced mail_handler hacky hook by issues "before_save" hook --- init.rb | 2 +- lib/issue_model_patch.rb | 25 +++++++++++++++++ lib/mail_handler_model_patch.rb | 48 --------------------------------- 3 files changed, 26 insertions(+), 49 deletions(-) create mode 100644 lib/issue_model_patch.rb delete mode 100644 lib/mail_handler_model_patch.rb diff --git a/init.rb b/init.rb index 55e0867..f323c3f 100644 --- a/init.rb +++ b/init.rb @@ -1,6 +1,6 @@ require 'redmine' -require_dependency 'mail_handler_model_patch' +require_dependency 'issue_model_patch' require_dependency 'auto_watch_hook' Redmine::Plugin.register :redmine_auto_watch do diff --git a/lib/issue_model_patch.rb b/lib/issue_model_patch.rb new file mode 100644 index 0000000..fe054ae --- /dev/null +++ b/lib/issue_model_patch.rb @@ -0,0 +1,25 @@ +module AutoWatch + module IssuePatch + def self.included(base) + base.class_eval do + unloadable + + def autowatch_add_watcher(watcher) + return if watcher.nil? || !watcher.is_a?(User) || watcher.anonymous? || !watcher.active? + self.add_watcher(watcher) unless self.watched_by?(watcher) + end + + def autowatch_beforesave_hook + Rails.logger.info(self.to_yaml) + autowatch_add_watcher(self.author) + autowatch_add_watcher(self.assigned_to) + end + + around_save :autowatch_beforesave_hook + end + end + end +end + +Issue.send(:include, AutoWatch::IssuePatch) + diff --git a/lib/mail_handler_model_patch.rb b/lib/mail_handler_model_patch.rb deleted file mode 100644 index 42b5638..0000000 --- a/lib/mail_handler_model_patch.rb +++ /dev/null @@ -1,48 +0,0 @@ -module AutoWatch - module MailHandlerPatch - def self.included(base) - base.send(:include, InstanceMethods) - - base.class_eval do - unloadable - - def receive_issue_with_auto_watch - # The code of this function is a copy of https://github.com/redmine/redmine/blob/master/app/models/mail_handler.rb#L187 - - project = target_project - # check permission - unless @@handler_options[:no_permission_check] - raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) - end - - issue = Issue.new(:author => user, :project => project) - - issue.safe_attributes = issue_attributes_from_keywords(issue) - issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} - issue.subject = cleaned_up_subject - if issue.subject.blank? - issue.subject = '(no subject)' - end - issue.description = cleaned_up_text_body - issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? - - # add To and Cc as watchers before saving so the watchers can reply to Redmine - issue.add_watcher(issue.author) - issue.add_watcher(issue.assigned_to) - add_watchers(issue) - issue.save! - add_attachments(issue) - logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger - issue - end - alias_method_chain :receive_issue, :auto_watch - end - end - - module InstanceMethods - end - end -end - -MailHandler.send(:include, AutoWatch::MailHandlerPatch) -