From a08f59fd760eb564715696c87bd0098ddbc2cbd6 Mon Sep 17 00:00:00 2001 From: Alexander Meindl Date: Fri, 10 Sep 2021 23:29:31 +0200 Subject: [PATCH] refactor entity concerns --- lib/additionals/entity_methods.rb | 120 ++++++++++++----------- lib/additionals/entity_methods_global.rb | 17 ++++ lib/additionals/patches/wiki_patch.rb | 10 +- 3 files changed, 85 insertions(+), 62 deletions(-) create mode 100644 lib/additionals/entity_methods_global.rb diff --git a/lib/additionals/entity_methods.rb b/lib/additionals/entity_methods.rb index 05b67867..2969d9ac 100644 --- a/lib/additionals/entity_methods.rb +++ b/lib/additionals/entity_methods.rb @@ -1,71 +1,81 @@ # frozen_string_literal: true module Additionals + # Only used for non default Redmine entities (not for issues, time_tracking, etc) module EntityMethods - attr_reader :current_journal + extend ActiveSupport::Concern - def assignable_users(prj = nil) - prj = project if project.present? - users = prj.assignable_users_and_groups.to_a - users << author if author&.active? - if assigned_to_id_was.present? - assignee = Principal.find_by id: assigned_to_id_was - users << assignee if assignee + included do + include Additionals::EntityMethodsGlobal + include InstanceMethods + + attr_reader :current_journal + end + + module InstanceMethods + def assignable_users(prj = nil) + prj = project if project.present? + users = prj.assignable_users_and_groups.to_a + users << author if author&.active? + if assigned_to_id_was.present? + assignee = Principal.find_by id: assigned_to_id_was + users << assignee if assignee + end + + users.uniq! + users.sort end - users.uniq! - users.sort - end - - def last_notes - @last_notes ||= journals.where.not(notes: '').reorder(id: :desc).first.try(:notes) - end - - def new_status - true if created_on == updated_on - end - - # Returns the id of the last journal or nil - def last_journal_id - if new_record? - nil - else - journals.maximum :id + def last_notes + @last_notes ||= journals.where.not(notes: '').reorder(id: :desc).first.try(:notes) end - end - # Saves the changes in a Journal - # Called after_save - def create_journal - current_journal&.save - end + def new_status + true if created_on == updated_on + end - # Returns the journals that are visible to user with their index - # Used to display the issue history - # ! this is a replacement of Redmine method - no not change signature - def visible_journals_with_index(_user = User.current) - result = journals.preload(:details) - .preload(user: :email_address) - .reorder(:created_on, :id).to_a + # Returns the id of the last journal or nil + def last_journal_id + if new_record? + nil + else + journals.maximum :id + end + end - result.each_with_index { |j, i| j.indice = i + 1 } - Journal.preload_journals_details_custom_fields result - result.select! { |journal| journal.notes? || journal.visible_details.any? } - result - end + # Saves the changes in a Journal + # Called after_save + def create_journal + current_journal&.save + end - # Callback on file attachment - def attachment_added(attachment) - init_journal User.current - current_journal.journalize_attachment attachment, :added - current_journal.save! - end + # Returns the journals that are visible to user with their index + # Used to display the issue history + # ! this is a replacement of Redmine method - no not change signature + def visible_journals_with_index(_user = User.current) + result = journals.preload(:details) + .preload(user: :email_address) + .reorder(:created_on, :id).to_a - # Callback on attachment deletion - def attachment_removed(attachment) - init_journal User.current - current_journal.journalize_attachment attachment, :removed - current_journal.save! + result.each_with_index { |j, i| j.indice = i + 1 } + Journal.preload_journals_details_custom_fields result + result.select! { |journal| journal.notes? || journal.visible_details.any? } + result + end + + # Callback on file attachment + def attachment_added(attachment) + init_journal User.current + current_journal.journalize_attachment attachment, :added + current_journal.save! + end + + # Callback on attachment deletion + def attachment_removed(attachment) + init_journal User.current + current_journal.journalize_attachment attachment, :removed + current_journal.save! + end end end end diff --git a/lib/additionals/entity_methods_global.rb b/lib/additionals/entity_methods_global.rb new file mode 100644 index 00000000..1f4eba71 --- /dev/null +++ b/lib/additionals/entity_methods_global.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Additionals + # Usage for all entities (including redmine entities like issue) + module EntityMethodsGlobal + extend ActiveSupport::Concern + + class_methods do + def join_enabled_module(module_name: self::ENTITY_MODULE_NAME) + raise 'Missing module' if module_name.nil? + + "JOIN #{::EnabledModule.table_name} ON #{::EnabledModule.table_name}.project_id=#{table_name}.project_id" \ + " AND #{::EnabledModule.table_name}.name='#{module_name}'" + end + end + end +end diff --git a/lib/additionals/patches/wiki_patch.rb b/lib/additionals/patches/wiki_patch.rb index fb33edbb..9f8e84cd 100644 --- a/lib/additionals/patches/wiki_patch.rb +++ b/lib/additionals/patches/wiki_patch.rb @@ -8,20 +8,16 @@ module Additionals module WikiPatch extend ActiveSupport::Concern + ENTITY_MODULE_NAME = 'wiki' + included do + include Additionals::EntityMethodsGlobal include InstanceMethods alias_method :sidebar_without_additionals, :sidebar alias_method :sidebar, :sidebar_with_additionals end - class_methods do - def join_enabled_module - "JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id=#{Wiki.table_name}.project_id" \ - " AND #{EnabledModule.table_name}.name='wiki'" - end - end - module InstanceMethods def sidebar_with_additionals @sidebar ||= find_page 'Sidebar', with_redirect: false