From 84552f35e31cbe9bf3cd0b241aec1ef465fca965 Mon Sep 17 00:00:00 2001 From: Anton Argirov Date: Sat, 19 Dec 2015 17:08:02 +0600 Subject: [PATCH] New observable objects added (Version) --- README.rdoc | 2 +- app/models/custom_workflow.rb | 6 +-- config/locales/en.yml | 2 + config/locales/ru.yml | 2 + init.rb | 3 ++ lib/redmine_custom_workflows/version_patch.rb | 38 +++++++++++++++++++ 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 lib/redmine_custom_workflows/version_patch.rb diff --git a/README.rdoc b/README.rdoc index d2f6e7e..53f276b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -118,7 +118,7 @@ This plug-in is compatible with Redmine 2.x.x, 3.x.x == Changelog -[0.1.6] * New observable objects added (TimeEntry) +[0.1.6] * New observable objects added (TimeEntry, Version) * Bug fixes [0.1.5] * New observable objects added (Project, Wiki Content, Attachment, Issue Attachments, Project Attachments, Wiki Page Attachments) * Ability to hook before_destroy and after_destroy events diff --git a/app/models/custom_workflow.rb b/app/models/custom_workflow.rb index 4a78e53..52c1586 100644 --- a/app/models/custom_workflow.rb +++ b/app/models/custom_workflow.rb @@ -9,10 +9,10 @@ end class CustomWorkflow < ActiveRecord::Base OBSERVABLES = [:issue, :issue_attachments, :user, :attachment, :group, :group_users, :project, :project_attachments, - :wiki_content, :wiki_page_attachments, :time_entry, :shared] - PROJECT_OBSERVABLES = [:issue, :issue_attachments, :project, :project_attachments, :wiki_content, :wiki_page_attachments, :time_entry] + :wiki_content, :wiki_page_attachments, :time_entry, :version, :shared] + PROJECT_OBSERVABLES = [:issue, :issue_attachments, :project, :project_attachments, :wiki_content, :wiki_page_attachments, :time_entry, :version] COLLECTION_OBSERVABLES = [:group_users, :issue_attachments, :project_attachments, :wiki_page_attachments] - SINGLE_OBSERVABLES = [:issue, :user, :group, :attachment, :project, :wiki_content, :time_entry] + SINGLE_OBSERVABLES = [:issue, :user, :group, :attachment, :project, :wiki_content, :time_entry, :version] attr_protected :id has_and_belongs_to_many :projects diff --git a/config/locales/en.yml b/config/locales/en.yml index 4de264f..f64cdbb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -63,6 +63,7 @@ en: text_custom_workflow_wiki_content_code_note: Scripts are executed in the context of Wiki Content object when project object changes (destroys). Use methods and properties of the project directly (or through "self") text_custom_workflow_wiki_page_attachments_code_note: These scripts are executed when a file being added to wiki page/removed from wiki page. Use variables @page and @attachment to access appropriate objects in your scripts. text_custom_workflow_time_entry_code_note: Scripts are executed in the context of TimeEntry object when time enty object changes (destroys). Use methods and properties of the time entry directly (or through "self") + text_custom_workflow_version_code_note: Scripts are executed in the context of Version object when version object changes (destroys). Use methods and properties of the version directly (or through "self") text_no_enabled_projects: No projects text_custom_workflow_author: Will be included in exported XML @@ -81,3 +82,4 @@ en: custom_workflow_observable_wiki_page_attachments: "Wiki Page Attachments" custom_workflow_observable_group_users: "Group Users" custom_workflow_observable_time_entry: "Time Entry" + custom_workflow_observable_version: "Version" diff --git a/config/locales/ru.yml b/config/locales/ru.yml index b556863..9e3bddd 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -63,6 +63,7 @@ ru: text_custom_workflow_wiki_content_code_note: Эти сценарии исполняются в контексте объекта Wiki содержания когда объект Wiki содержания изменяется (удаляется). Используйте методы и свойства объекта содержания Wiki (WikiContent) напрямую или через ключевое слово self. text_custom_workflow_wiki_page_attachments_code_note: Эти сценарии выполняются когда файл загружается на Wiki страницу/удаляется с Wiki страницы. Используйте переменные @page и @attachment для доступа к соответствующим объектам из Ваших сценариев. text_custom_workflow_time_entry_code_note: Эти сценарии исполняются в контексте объекта затраченного времени когда объект изменяется (удаляется). Используйте методы и свойства объекта затраченного времени (TimeEntry) напрямую или через ключевое слово self. + text_custom_workflow_version_code_note: Эти сценарии исполняются в контексте объекта версии когда объект изменяется (удаляется). Используйте методы и свойства объекта версии (Version) напрямую или через ключевое слово self. text_no_enabled_projects: Нет проектов text_custom_workflow_author: Будет использован в XML файле при экспорте @@ -81,3 +82,4 @@ ru: custom_workflow_observable_wiki_page_attachments: "Вложения страниц Wiki" custom_workflow_observable_group_users: "Пользователи группы" custom_workflow_observable_time_entry: "Затраченное время" + custom_workflow_observable_version: "Версия" diff --git a/init.rb b/init.rb index 839f08a..0fb41e6 100644 --- a/init.rb +++ b/init.rb @@ -23,6 +23,9 @@ Rails.application.config.to_prepare do unless TimeEntry.include?(RedmineCustomWorkflows::TimeEntryPatch) TimeEntry.send(:include, RedmineCustomWorkflows::TimeEntryPatch) end + unless Version.include?(RedmineCustomWorkflows::VersionPatch) + Version.send(:include, RedmineCustomWorkflows::VersionPatch) + end unless WikiContent.include?(RedmineCustomWorkflows::WikiContentPatch) WikiContent.send(:include, RedmineCustomWorkflows::WikiContentPatch) end diff --git a/lib/redmine_custom_workflows/version_patch.rb b/lib/redmine_custom_workflows/version_patch.rb new file mode 100644 index 0000000..cd125a1 --- /dev/null +++ b/lib/redmine_custom_workflows/version_patch.rb @@ -0,0 +1,38 @@ +module RedmineCustomWorkflows + module VersionPatch + + def self.included(base) + base.send(:include, InstanceMethods) + base.class_eval do + before_save :before_save_custom_workflows + after_save :after_save_custom_workflows + before_destroy :before_destroy_custom_workflows + after_destroy :after_destroy_custom_workflows + end + end + + module InstanceMethods + def before_save_custom_workflows + @version = self + @saved_attributes = attributes.dup + CustomWorkflow.run_shared_code(self) + CustomWorkflow.run_custom_workflows(:version, self, :before_save) + errors.empty? && (@saved_attributes == attributes || valid?) + ensure + @saved_attributes = nil + end + + def after_save_custom_workflows + CustomWorkflow.run_custom_workflows(:version, self, :after_save) + end + + def before_destroy_custom_workflows + CustomWorkflow.run_custom_workflows(:version, self, :before_destroy) + end + + def after_destroy_custom_workflows + CustomWorkflow.run_custom_workflows(:version, self, :after_destroy) + end + end + end +end