Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

Closes #5

See merge request !1
  • Loading branch information
dkastl committed Sep 2, 2017
2 parents ca8f748 + 9967e38 commit 5089e76
Show file tree
Hide file tree
Showing 28 changed files with 553 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'immutable-struct'
82 changes: 82 additions & 0 deletions app/controllers/text_blocks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class TextBlocksController < ApplicationController
layout ->{ @project ? 'base' : 'admin' }

self.main_menu = false

before_action :find_project_by_project_id

before_action :require_admin, if: ->{ @project.nil? }
before_action :authorize, if: ->{ @project.present? }

menu_item :settings, only: [:new, :create, :edit, :update, :destroy]
helper_method :index_path

def index
@text_blocks = text_block_scope
end

def edit
@text_block = find_text_block
end

def new
@text_block = TextBlock.new
end

def create
r = RedmineTextBlocks::SaveTextBlock.(text_block_params,
project: @project)
if r.text_block_saved?
redirect_to params[:continue] ? new_path : index_path
else
@text_block = r.text_block
render 'new'
end
end

def update
@text_block = find_text_block
r = RedmineTextBlocks::SaveTextBlock.(text_block_params,
text_block: @text_block)
if r.text_block_saved?
redirect_to index_path
else
render 'edit'
end
end

def destroy
find_text_block.destroy
redirect_to index_path
end


private

def new_path
@project ? new_project_text_block_path(@project) : new_text_block_path
end

def index_path
@project ? settings_project_path(@project, tab: 'text_blocks') : text_blocks_path
end

def text_block_params
params[:text_block].permit :name, :text
end

def find_text_block
text_block_scope.find params[:id]
end

def find_project_by_project_id
if id = params[:project_id]
@project = Project.find id
end
end

def text_block_scope
TextBlock.order(name: :asc).where(project_id: @project&.id)
end

end
14 changes: 14 additions & 0 deletions app/helpers/text_blocks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module TextBlocksHelper
def text_block_options
tags = []
tags << content_tag(:option, value: '') do
t('label_select_text_block')
end
tags += TextBlock.where(project_id: [nil, @project.id]).to_a.map{|tb|
content_tag :option, value: tb.text do
tb.name
end
}
safe_join tags
end
end
22 changes: 22 additions & 0 deletions app/models/text_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class TextBlock < ActiveRecord::Base
belongs_to :project

validates :name, presence: true
validate :name_uniqueness


private

def name_uniqueness
scope = TextBlock.where.not(id: id).where(name: name)

if project_id.present?
scope = scope.where project_id: [ nil, project_id ]
end

if scope.any?
errors.add :name, I18n.t('model.text_block.name_uniqueness')
end
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% if User.current.allowed_to?(:view_text_blocks, @project) %>
<%= select_tag :text_block, text_block_options, id: 'textblock-select', style: 'display:none; border: 1px solid #e0e2e3; background: #f6f6f6; height: 24px; color: #3d454c; vertical-align: bottom; margin-left: 6px; margin-botton: 0;' %>
<%= javascript_include_tag 'text_blocks', plugin: 'redmine_text_blocks' %>
<% end %>
6 changes: 6 additions & 0 deletions app/views/projects/settings/_text_blocks.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>
<%= link_to l(:label_text_block_new), new_project_text_block_path(@project), class: 'icon icon-add' %>
</p>

<%= render partial: 'text_blocks/list', locals: { text_blocks: TextBlock.where(project_id: @project.id) } %>

10 changes: 10 additions & 0 deletions app/views/text_blocks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= error_messages_for 'text_block' %>

<div class="box tabular">
<p><%= f.text_field :name, required: true, size: 25 %></p>
<p><%= f.text_area :text, label: l(:field_text_block_text) %></p>
</div>




17 changes: 17 additions & 0 deletions app/views/text_blocks/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<% if text_blocks.any? %>
<table class="list">
<thead>
<tr>
<th><%= l :label_text_block_name %></th>
<th><%= l :label_text_block_text %></th>
<th></th>
</tr>
</thead>
<tbody>
<%= render collection: text_blocks, partial: 'text_blocks/text_block' %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l :label_no_data %></p>
<% end %>

5 changes: 5 additions & 0 deletions app/views/text_blocks/_text_block.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tr>
<td><%= link_to text_block.name, (text_block.project ? edit_project_text_block_path(text_block.project, text_block) : edit_text_block_path(text_block)) %></td>
<td><%= text_block.text %></td>
<td><%= delete_link(text_block.project ? project_text_block_path(text_block.project, text_block) : text_block_path(text_block)) %>
</tr>
11 changes: 11 additions & 0 deletions app/views/text_blocks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= title [l(:label_text_block_plural), index_path], @text_block.name %>

<%= labelled_form_for @text_block, url: (@project ? project_text_block_path(@project, @text_block) : text_block_path(@text_block)), method: :patch do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<p>
<%= submit_tag l :button_save %>
</p>
<% end %>



10 changes: 10 additions & 0 deletions app/views/text_blocks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="contextual">
<%= link_to l(:label_text_block_new), new_text_block_path, class: 'icon icon-add' %>
</div>

<h2><%= l :label_text_block_plural %></h2>

<%= render partial: 'list', locals: { text_blocks: @text_blocks } %>

<% html_title l :label_text_block_plural -%>

11 changes: 11 additions & 0 deletions app/views/text_blocks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= title [l(:label_text_block_plural), index_path], l(:label_text_block_new) %>

<%= labelled_form_for @text_block, url: @project ? project_text_blocks_path(@project) : text_blocks_path do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<p>
<%= submit_tag l :button_create %>
<%= submit_tag l(:button_create_and_continue), name: 'continue' %>
</p>
<% end %>


Binary file added assets/images/text_signature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions assets/javascripts/text_blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var TextBlocks = {
init: function(){
var select = $('#textblock-select');
var toolbar_buttons = select.parent().find('.jstElements');
if(select.parent() != toolbar_buttons) {
toolbar_buttons.find('button:last').after(select);
}
select.show();
},

insert: function(e){
var value = $(this).val();
if(value == '') return;

console.log(value);
var fieldId = $('#textblock-select').parent().next('div.jstEditor').find('textarea').attr('id');

var field = document.getElementById(fieldId);
var field_ = $(field);

var caretPos = field.selectionStart;
var caretEnd = field.selectionEnd;
var textAreaTxt = field_.val();
field_.val(
textAreaTxt.substring(0, caretPos) +
value +
textAreaTxt.substring(caretEnd)
);

field_.focus();
field.selectionStart = caretPos + value.length
field.selectionEnd = caretPos + value.length

$(this).val('');
}

}

$(document).ready(TextBlocks.init);
$(document).on("change", "#textblock-select", TextBlocks.insert);
5 changes: 5 additions & 0 deletions assets/stylesheets/text_blocks.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**************************************************************/
/* ICONS
/**************************************************************/

#admin-menu a.text-blocks { background-image: url(../images/text_signature.png);}
13 changes: 13 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
en:

field_text_block_text: Text

label_select_text_block: "Select text block..."
label_text_block_name: Name
label_text_block_text: Text
label_text_block_plural: Text blocks
label_text_block_new: New text block

model:
text_block:
name_uniqueness: A text block with that name already exists.
13 changes: 13 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
en:

field_text_block_text: テンプレート

label_select_text_block: "テンプレートを選択..."
label_text_block_name: タイトル
label_text_block_text: テンプレート
label_text_block_plural: テンプレート
label_text_block_new: 新しいテンプレート

model:
text_block:
name_uniqueness: その名前のテンプレートはすでに存在します。
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources :text_blocks, only: %i(index new create edit update destroy)

scope 'projects/:project_id' do
resources :text_blocks, only: %i(new create edit update destroy),
as: :project_text_blocks
end
11 changes: 11 additions & 0 deletions db/migrate/20170831050051_create_text_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateTextBlocks < ActiveRecord::Migration
def change
create_table :text_blocks do |t|
t.string :name
t.text :text
t.references :project, index: true, foreign_key: true

t.timestamps null: false
end
end
end
38 changes: 38 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'redmine'

Rails.configuration.to_prepare do
RedmineTextBlocks.setup
end

Redmine::Plugin.register :redmine_text_blocks do
name 'Redmine Text Blocks Plugin'
author 'Jens Krämer, Georepublic'
author_url 'https://hub.georepublic.net/gtt/redmine_text_blocks'
description 'Adds configurable text blocks for replying to issues'
version '0.1.0'

requires_redmine version_or_higher: '3.4.0'

#settings default: {
#}, partial: 'redmine_text_blocks/settings'

project_module :text_blocks do

permission :view_text_blocks, {}, require: :member, read: :true
permission :manage_text_blocks, {
text_blocks: %i( new edit update create destroy ),
projects: %i( manage_text_blocks )
}, require: :member
end

menu :admin_menu, :text_blocks,
{ controller: 'text_blocks', action: 'index' },
caption: :label_text_block_plural, :html => {:class => 'icon'}

end

class TextBlocksListener < Redmine::Hook::ViewListener
render_on :view_layouts_base_html_head, inline: <<-END
<%= stylesheet_link_tag 'text_blocks', :plugin => 'redmine_text_blocks' %>
END
end
8 changes: 8 additions & 0 deletions lib/redmine_text_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'redmine_text_blocks/view_hooks'

module RedmineTextBlocks
def self.setup
ProjectsController.send :helper, RedmineTextBlocks::ProjectSettingsTabs
IssuesController.send :helper, TextBlocksHelper
end
end
21 changes: 21 additions & 0 deletions lib/redmine_text_blocks/project_settings_tabs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module RedmineTextBlocks

# hooks into the helper method that renders the project settings tabs
module ProjectSettingsTabs

def project_settings_tabs
super.tap do |tabs|
if User.current.allowed_to?(:manage_text_blocks, @project)
tabs << {
name: 'text_blocks',
action: :manage_text_blocks,
partial: 'projects/settings/text_blocks',
label: :label_text_block_plural
}
end
end
end

end
end

26 changes: 26 additions & 0 deletions lib/redmine_text_blocks/save_text_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module RedmineTextBlocks
class SaveTextBlock

Result = ImmutableStruct.new :text_block_saved?, :text_block

def self.call(*_)
new(*_).call
end

def initialize(params, text_block: TextBlock.new,
project: text_block.project)
@params = params
@text_block = text_block
@project = project
end


def call
@text_block.project = @project
@text_block.attributes = @params

return Result.new text_block_saved: @text_block.save,
text_block: @text_block
end
end
end
Loading

0 comments on commit 5089e76

Please sign in to comment.