refactor entity concerns

This commit is contained in:
Alexander Meindl 2021-09-10 23:29:31 +02:00
parent bc9054b615
commit a08f59fd76
3 changed files with 85 additions and 62 deletions

View File

@ -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

View File

@ -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

View File

@ -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