Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #346 from akiko-pusu/develop
Browse files Browse the repository at this point in the history
Ready for release v1.0.2
  • Loading branch information
akiko-pusu authored May 14, 2020
2 parents f78a30d + 2cb1428 commit dd5b554
Show file tree
Hide file tree
Showing 39 changed files with 1,003 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
inherit_from: .rubocop_todo.yml
require:
- rubocop-rails
AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
Exclude:
- 'db/**/*'

15 changes: 15 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ EndOfLine:

Metrics/ModuleLength:
Max: 120

Rails/ApplicationRecord:
Enabled: false

Rails/UniqueValidationWithoutIndex:
Enabled: false

Rails/InverseOf:
Enabled: false

Rails/LexicallyScopedActionFilter:
Enabled: false

Rails/SkipsModelValidations:
Enabled: false
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ If you have any requests, bug reports, please use GitHub issues. <https://github

## Changelog

### 1.0.2

Release to implememted Global note templates feature.

NOTE: **Migration is required** to use global note template.

* Feature: Implement Global Note Template. (GitHub: #268, #336)
* Feature: Improve the input form for built-In / custom fields setting. (GitHub: #345)
* Bugfix: Selecting note template browser "jumps" to top of page. (GitHub: #338)
* Bugfix: Change to make the selector more specific. Thanks, @sandratatarevicova (GitHub: #332, #333)
* Apply Bulgarian translation. Thanks, @jwalkerbg (GitHub: #330)
* Update README: `--without` argument for `bundle` is no longer necessary. (GitHub: #335 / by @vividtone)
* Update German Translation (by Christian Friebel).

RESTRICTION: This version **is still not compatible with IE11**. (Related: #310)

### 1.0.1

This is bugfix release against v1.0.0.
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/concerns/project_templates_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def save_and_flash(message, action_on_failure)
nil
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

private

def template
Expand Down
112 changes: 112 additions & 0 deletions app/controllers/global_note_templates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

# noinspection RubocopInspection
class GlobalNoteTemplatesController < ApplicationController
layout 'base'
helper :issues
helper :issue_templates
menu_item :issues

before_action :find_object, only: %i[show update destroy]
before_action :find_project, only: %i[update]
before_action :require_admin, only: %i[index new show], excep: [:preview]

#
# Action for global template : Admin right is required.
#
def index
trackers = Tracker.all
template_map = {}
trackers.each do |tracker|
tracker_id = tracker.id
templates = GlobalNoteTemplate.search_by_tracker(tracker_id).sorted
template_map[Tracker.find(tracker_id)] = templates if templates.any?
end

render layout: !request.xhr?, locals: { template_map: template_map, trackers: trackers }
end

def new
# create empty instance
@global_note_template = GlobalNoteTemplate.new
render render_form_params
end

def create
@global_note_template = GlobalNoteTemplate.new(template_params)
@global_note_template.author = User.current

save_and_flash(:notice_successful_create, :new) && return
end

def show
render render_form_params
end

def update
# Workaround in case author id is null
@global_note_template.author = User.current if @global_note_template.author.blank?
@global_note_template.safe_attributes = template_params

save_and_flash(:notice_successful_update, :show)
end

def destroy
unless @global_note_template.destroy
flash[:error] = l(:enabled_template_cannot_destroy)
redirect_to action: :show, id: @global_note_template
return
end

flash[:notice] = l(:notice_successful_delete)
redirect_to action: 'index'
end

def find_project
@projects = Project.all
end

def find_object
@global_note_template = GlobalNoteTemplate.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
end

def save_and_flash(message, action_on_failure)
unless @global_note_template.save
render render_form_params.merge(action: action_on_failure)
return
end

respond_to do |format|
format.html do
flash[:notice] = l(message)
redirect_to action: 'show', id: @global_note_template.id
end
format.js { head 200 }
end
end

def template_params
params.require(:global_note_template)
.permit(:global_note_template_id, :tracker_id, :name, :memo, :description,
:enabled, :author_id, :position, :visibility, role_ids: [], project_ids: [])
end

def render_form_params
trackers = Tracker.all
projects = Project.all

{ layout: !request.xhr?,
locals: { trackers: trackers, apply_all_projects: apply_all_projects?,
note_template: @global_note_template, projects: projects } }
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
end
42 changes: 35 additions & 7 deletions app/controllers/note_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ def index
@template_map[Tracker.find(tracker_id)] = templates if templates.any?
end

@global_note_templates = global_templates(tracker_ids)

respond_to do |format|
format.html do
render layout: !request.xhr?, locals: { tracker_ids: tracker_ids }
render layout: !request.xhr?,
locals: { apply_all_projects: apply_all_projects?, tracker_ids: tracker_ids }
end
format.api do
render formats: :json, locals: { note_templates: note_templates }
Expand Down Expand Up @@ -53,10 +56,19 @@ def update
# load template description
def load
note_template_id = template_params[:note_template_id]
note_template = NoteTemplate.find(note_template_id)

# prevent to load if the template visibility does not match.
render_404 unless note_template.loadable?(user_id: User.current.id)
template_type = template_params[:template_type]

if template_type.present? && template_type == 'global'
project_id = template_params[:project_id]
note_template = GlobalNoteTemplate.find(note_template_id)

# prevent to load if the template visibility does not match.
raise ActiveRecord::RecordNotFound unless note_template.loadable?(user_id: User.current.id, project_id: project_id)
else
note_template = NoteTemplate.find(note_template_id)
# prevent to load if the template visibility does not match.
raise ActiveRecord::RecordNotFound unless note_template.loadable?(user_id: User.current.id)
end

render plain: note_template.template_json
rescue ActiveRecord::RecordNotFound
Expand All @@ -70,11 +82,16 @@ def list_templates
note_templates = NoteTemplate.visible_note_templates_condition(
user_id: User.current.id, project_id: project_id, tracker_id: tracker_id
)

global_note_templates = GlobalNoteTemplate.visible_note_templates_condition(
user_id: User.current.id, project_id: project_id, tracker_id: tracker_id
)

respond_to do |format|
format.html do
render action: '_list_note_templates',
layout: false,
locals: { note_templates: note_templates }
locals: { note_templates: note_templates, global_note_templates: global_note_templates }
end
end
end
Expand Down Expand Up @@ -105,7 +122,7 @@ def find_object

def template_params
params.require(:note_template)
.permit(:note_template_id, :tracker_id, :name, :memo, :description,
.permit(:note_template_id, :project_id, :template_type, :tracker_id, :name, :memo, :description,
:enabled, :author_id, :position, :visibility, role_ids: [])
end

Expand All @@ -117,4 +134,15 @@ def render_form_params
{ layout: !request.xhr?,
locals: { note_template: template, project: @project } }
end

def templates_exist?
@note_templates.present?
end

def global_templates(tracker_id)
return [] if apply_all_projects? && templates_exist?

project_id = apply_all_projects? ? nil : @project.id
GlobalNoteTemplate.get_templates_for_project_tracker(project_id, tracker_id)
end
end
Loading

0 comments on commit dd5b554

Please sign in to comment.