Skip to content

Commit

Permalink
Merge pull request #1 from evolvingweb/redmine-5-compatibility
Browse files Browse the repository at this point in the history
Redmine 5 compatibility
  • Loading branch information
burak-ikiler-ew authored May 22, 2024
2 parents 58214cf + a227cdc commit 198d58f
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 279 deletions.
210 changes: 104 additions & 106 deletions app/controllers/concerns/issue_templates_common.rb
Original file line number Diff line number Diff line change
@@ -1,142 +1,140 @@
# frozen_string_literal: true

module Concerns
module IssueTemplatesCommon
extend ActiveSupport::Concern
module IssueTemplatesCommon
extend ActiveSupport::Concern

class InvalidTemplateFormatError < StandardError; end
class InvalidTemplateFormatError < StandardError; end

included do
before_action :log_action, only: [:destroy]
included do
before_action :log_action, only: [:destroy]

# logging action
def log_action
logger&.info "[#{self.class}] #{action_name} called by #{User.current.name}"
end

def plugin_setting
Setting.plugin_redmine_issue_templates
end

def apply_all_projects?
plugin_setting['apply_global_template_to_all_projects'].to_s == 'true'
end

def apply_template_when_edit_issue?
plugin_setting['apply_template_when_edit_issue'].to_s == 'true'
end

def builtin_fields_enabled?
plugin_setting['enable_builtin_fields'].to_s == 'true'
end
# logging action
def log_action
logger&.info "[#{self.class}] #{action_name} called by #{User.current.name}"
end

def load_selectable_fields
tracker_id = params[:tracker_id]
project_id = params[:project_id]
render plain: {} && return if tracker_id.blank?

custom_fields = core_fields_map_by_tracker_id(tracker_id: tracker_id, project_id: project_id).merge(custom_fields_map_by_tracker_id(tracker_id))
render plain: { custom_fields: custom_fields }.to_json
end

def orphaned_templates
render partial: 'common/orphaned', locals: { orphaned_templates: orphaned }
def plugin_setting
Setting.plugin_redmine_issue_templates
end

def apply_all_projects?
plugin_setting['apply_global_template_to_all_projects'].to_s == 'true'
end

def checklists
template_params[:checklists].presence || []
def apply_template_when_edit_issue?
plugin_setting['apply_template_when_edit_issue'].to_s == 'true'
end

def builtin_fields_json
value = template_params[:builtin_fields].blank? ? {} : JSON.parse(template_params[:builtin_fields])
return value if value.is_a?(Hash)

raise InvalidTemplateFormatError
def builtin_fields_enabled?
plugin_setting['enable_builtin_fields'].to_s == 'true'
end
end

def checklist_enabled?
Redmine::Plugin.registered_plugins.key? :redmine_checklists
rescue StandardError
false
end
def load_selectable_fields
tracker_id = params[:tracker_id]
project_id = params[:project_id]
render plain: {} && return if tracker_id.blank?

def valid_params
# convert attribute name and data for checklist plugin supporting
attributes = template_params.except(:checklists, :builtin_fields)
attributes[:builtin_fields_json] = builtin_fields_json if builtin_fields_enabled?
attributes[:checklist_json] = checklists.to_json if checklist_enabled?
attributes
end
custom_fields = core_fields_map_by_tracker_id(tracker_id: tracker_id, project_id: project_id).merge(custom_fields_map_by_tracker_id(tracker_id))
render plain: { custom_fields: custom_fields }.to_json
end

def destroy
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
def orphaned_templates
render partial: 'common/orphaned', locals: { orphaned_templates: orphaned }
end

#
# TODO: Code should be refactored
#
def core_fields_map_by_tracker_id(tracker_id: nil, project_id: nil)
return {} unless builtin_fields_enabled?
def apply_all_projects?
plugin_setting['apply_global_template_to_all_projects'].to_s == 'true'
end

fields = %w[status_id priority_id]
def checklists
template_params[:checklists].presence || []
end

# exclude "description"
tracker = Tracker.find_by(id: tracker_id)
fields += tracker.core_fields.reject { |field| field == 'description' } if tracker.present?
fields.reject! { |field| %w[category_id fixed_version_id].include?(field) } if project_id.blank?
def builtin_fields_json
value = template_params[:builtin_fields].blank? ? {} : JSON.parse(template_params[:builtin_fields])
return value if value.is_a?(Hash)

map = {}
raise InvalidTemplateFormatError
end

fields.each do |field|
id = "issue_#{field}"
name = I18n.t('field_' + field.gsub(/_id$/, ''))
value = { name: name, core_field_id: id }
if field == 'priority_id'
value[:possible_values] = IssuePriority.active.pluck(:name)
value[:field_format] = 'list'
end
def checklist_enabled?
Redmine::Plugin.registered_plugins.key? :redmine_checklists
rescue StandardError
false
end

def valid_params
# convert attribute name and data for checklist plugin supporting
attributes = template_params.except(:checklists, :builtin_fields)
attributes[:builtin_fields_json] = builtin_fields_json if builtin_fields_enabled?
attributes[:checklist_json] = checklists.to_json if checklist_enabled?
attributes
end

def destroy
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

if field == 'status_id' && tracker.present?
value[:possible_values] = tracker.issue_statuses.pluck(:name)
value[:field_format] = 'list'
end
#
# TODO: Code should be refactored
#
def core_fields_map_by_tracker_id(tracker_id: nil, project_id: nil)
return {} unless builtin_fields_enabled?

value[:field_format] = 'date' if %(start_date due_date).include?(field)
fields = %w[status_id priority_id]

value[:field_format] = 'ratio' if field == 'done_ratio'
# exclude "description"
tracker = Tracker.find_by(id: tracker_id)
fields += tracker.core_fields.reject { |field| field == 'description' } if tracker.present?
fields.reject! { |field| %w[category_id fixed_version_id].include?(field) } if project_id.blank?

map[id] = value
map = {}

fields.each do |field|
id = "issue_#{field}"
name = I18n.t('field_' + field.gsub(/_id$/, ''))
value = { name: name, core_field_id: id }
if field == 'priority_id'
value[:possible_values] = IssuePriority.active.pluck(:name)
value[:field_format] = 'list'
end

if field == 'status_id' && tracker.present?
value[:possible_values] = tracker.issue_statuses.pluck(:name)
value[:field_format] = 'list'
end
map
rescue StandardError => e
logger&.info "core_fields_map_by_tracker_id failed due to this error: #{e.message}"
{}

value[:field_format] = 'date' if %(start_date due_date).include?(field)

value[:field_format] = 'ratio' if field == 'done_ratio'

map[id] = value
end
map
rescue StandardError => e
logger&.info "core_fields_map_by_tracker_id failed due to this error: #{e.message}"
{}
end

def custom_fields_map_by_tracker_id(tracker_id = nil)
return {} unless builtin_fields_enabled?
return {} if tracker_id.blank?
def custom_fields_map_by_tracker_id(tracker_id = nil)
return {} unless builtin_fields_enabled?
return {} if tracker_id.blank?

tracker = Tracker.find_by(id: tracker_id)
ids = tracker&.custom_field_ids || []
fields = IssueCustomField.where(id: ids)
map = {}
fields.each do |field|
id = "issue_custom_field_values_#{field.id}"
attributes = field.attributes
tracker = Tracker.find_by(id: tracker_id)
ids = tracker&.custom_field_ids || []
fields = IssueCustomField.where(id: ids)
map = {}
fields.each do |field|
id = "issue_custom_field_values_#{field.id}"
attributes = field.attributes

attributes = attributes.merge(possible_values: field.possible_values_options.map { |value| value[0] }) if field.format.name == 'bool'
map[id] = attributes
end
map
rescue StandardError => e
logger&.info "core_fields_map_by_tracker_id failed due to this error: #{e.message}"
{}
attributes = attributes.merge(possible_values: field.possible_values_options.map { |value| value[0] }) if field.format.name == 'bool'
map[id] = attributes
end
map
rescue StandardError => e
logger&.info "core_fields_map_by_tracker_id failed due to this error: #{e.message}"
{}
end
end
106 changes: 52 additions & 54 deletions app/controllers/concerns/project_templates_common.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,72 @@
# frozen_string_literal: true

module Concerns
module ProjectTemplatesCommon
extend ActiveSupport::Concern
included do
before_action :find_user, :find_project, :authorize, except: %i[preview load load_selectable_fields]
before_action :find_object, only: %i[show edit update destroy]
accept_api_auth :index, :list_templates, :load
end

def show
render render_form_params
end
module ProjectTemplatesCommon
extend ActiveSupport::Concern
included do
before_action :find_user, :find_project, :authorize, except: %i[preview load load_selectable_fields]
before_action :find_object, only: %i[show edit update destroy]
accept_api_auth :index, :list_templates, :load
end

def destroy
unless template.destroy
flash[:error] = l(:enabled_template_cannot_destroy)
redirect_to action: :show, project_id: @project, id: template
return
end
def show
render render_form_params
end

flash[:notice] = l(:notice_successful_delete)
redirect_to action: 'index', project_id: @project
def destroy
unless template.destroy
flash[:error] = l(:enabled_template_cannot_destroy)
redirect_to action: :show, project_id: @project, id: template
return
end

def save_and_flash(message, action_on_failure)
unless template.save
render render_form_params.merge(action: action_on_failure)
return
end
flash[:notice] = l(:notice_successful_delete)
redirect_to action: 'index', project_id: @project
end

respond_to do |format|
format.html do
flash[:notice] = l(message)
redirect_to action: 'show', id: template.id, project_id: @project
end
format.js { head 200 }
end
rescue NoteTemplate::NoteTemplateError => e
flash[:error] = e.message
def save_and_flash(message, action_on_failure)
unless template.save
render render_form_params.merge(action: action_on_failure)
nil
end

def plugin_setting
Setting.plugin_redmine_issue_templates
respond_to do |format|
format.html do
flash[:notice] = l(message)
redirect_to action: 'show', id: template.id, project_id: @project
end
format.js { head 200 }
end
rescue NoteTemplate::NoteTemplateError => e
flash[:error] = e.message
render render_form_params.merge(action: action_on_failure)
nil
end

def apply_all_projects?
plugin_setting['apply_global_template_to_all_projects'].to_s == 'true'
end
def plugin_setting
Setting.plugin_redmine_issue_templates
end

private
def apply_all_projects?
plugin_setting['apply_global_template_to_all_projects'].to_s == 'true'
end

def template
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
private

def find_user
@user = User.current
end
def template
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

def find_tracker
@tracker = Tracker.find(params[:issue_tracker_id])
end
def find_user
@user = User.current
end

def find_project
@project = Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound
render_404
end
def find_tracker
@tracker = Tracker.find(params[:issue_tracker_id])
end

def find_project
@project = Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound
render_404
end
end
2 changes: 1 addition & 1 deletion app/controllers/global_issue_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class GlobalIssueTemplatesController < ApplicationController
layout 'base'
helper :issues
include IssueTemplatesHelper
include Concerns::IssueTemplatesCommon
include IssueTemplatesCommon
menu_item :issues
before_action :find_object, only: %i[show edit update destroy]
before_action :find_project, only: %i[edit update]
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/issue_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class IssueTemplatesController < ApplicationController
layout 'base'
helper :issues
include Concerns::IssueTemplatesCommon
include Concerns::ProjectTemplatesCommon
include IssueTemplatesCommon
include ProjectTemplatesCommon
menu_item :issues
before_action :find_tracker, :find_templates, only: %i[set_pulldown list_templates]

Expand Down
Loading

0 comments on commit 198d58f

Please sign in to comment.