redmine插件开发简介

最稳妥的学习应该是先看官方文档,官方还给了一个具体的插件开发教程,不过如果一步不差按照教程敲代码,其实会发现还是有些问题的,需要稍稍改动。

这里,我自己编写了一个简单的插件,基于redmine-3.4.6-passenger版本。

####

插件名称:field_value_adaption

依赖插件:select_to_select2

插件作用:在issue的自定义字段中,根据某个自定义字段的值,自动填写其他自定义字段的值,这些值存在数据库中,如下数据库,当自定义字段1的值选择’便签’时,自动将686,626填写进入自定义子字段4,

数据库设计:

| Id | field_selector | value | field_selector_by | value_by |

| —— | —— | —— | —— | —— |

| 1 | #issue_custom_field_values_1 | 便签 | #issue_custom_field_values_4 | 686,626 |

| 2 | #issue_custom_field_values_1 | 天气 | #issue_custom_field_values_4 | 691 |

####

控制器 adaption

模型 field_value_relation

#设置开发环境变量

1
export RAILS_ENV="production"
1
cd $REDMINE_ROOT

#需要在redmine根目录下执行以下代码,默认是/usr/src/redmine

#新建插件

1
bundle exec ruby bin/rails generate redmine_plugin field_value_adaption

#新建数据库

1
bundle exec ruby bin/rails generate redmine_plugin_model field_value_adaption field_value_relation field_selector:string value:string field_selector_by:string value_by:string

#迁移数据库

1
bundle exec rake redmine:plugins:migrate

#将数据库编码设置成utf-8,如果没有中文输入,则没必要修改

1
2
alter table field_value_relations default character set utf8;
alter table field_value_relations change field_selector field_selector varchar(255) character set utf8;

#插入数据,可以直接在数据库中插入

1
2
3
4
5
6
7
bundle exec ruby bin/rails console
[rails 3] rails console
>> FieldValueRelation.create(:field_selector => "#issue_custom_field_values_2", :value =>"便签", :field_selector_by => "#issue_custom_field_values_4", :value_by => "686,626")
>> FieldValueRelation.create(:field_selector => "#issue_custom_field_values_2", :value =>"便签", :field_selector_by => "#issue_custom_field_values_5", :value_by => "691")
>> FieldValueRelation.create(:field_selector => "#issue_custom_field_values_2", :value =>"便签", :field_selector_by => "#issue_custom_field_values_8", :value_by => "686,606")
>> FieldValueRelation.create(:field_selector => "#issue_custom_field_values_2", :value =>"天气", :field_selector_by => "#issue_custom_field_values_4", :value_by => "626")
>> exit

#创建控制器

1
bundle exec ruby bin/rails generate redmine_plugin_controller field_value_adaption adaption get

修改控制器代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class AdaptionController < ApplicationController
def get
@allinfo = FieldValueRelation.all.to_a
@fieldsinfo = Hash.new
#这里使用三层嵌套的hash,比如{2=>{"天气"=>{4=>"691,626"}, "浏览器"=>{4=>"686"}}}
@allinfo.each { |x|
unless @fieldsinfo.has_key?(x.field_selector)
@fieldsinfo[x.field_selector] = {}
end
unless @fieldsinfo[x.field_selector].has_key?(x.value)
@fieldsinfo[x.field_selector][x.value] = {}
end
unless @fieldsinfo[x.field_selector][x.value].has_key?(x.field_selector_by)
@fieldsinfo[x.field_selector][x.value 大专栏  Redmine it!][x.field_selector_by] = x.value_by
end
} respond_to do |format|
format.js
end end
end

编写plugins/field_value_adaption/app/views/adaption/get.js.erb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var module_value = {}
<% @fieldsinfo.each { |key1,value1| %>
module_value['<%= key1 %>'] = {}
<% value1.each { |key2,value2| %>
module_value['<%= key1 %>']['<%= key2 %>']={}
<% value2.each { |key3,value3| %>
module_value['<%= key1 %>']['<%= key2 %>']['<%= key3 %>'] = ('<%= value3 %>').split(",")
<% }%>
<% }%>
<% } %>
$(window).load(function() {
for(var key1 in module_value){
$(document).on("select2-selecting",key1,function(e){
for ( var key2 in module_value[key1]) {
if(e.val == key2){
for(var key3 in module_value[key1][key2]){
$(key3).select2("val",module_value[key1][key2][key3]);
}
}
}
})
}
})

配置路由

1
2
3
RedmineApp::Application.routes.draw do
get 'field_adaption_js',:to => 'adaption#get'
end

配置issue页面发送请求的js plugins/field_value_adaption/assets/javascripts/fields_adaption_get.js

1
2
3
4
5
6
7
8
9
10
11
12
$.ajax({
url:"/redmine/field_adaption_js",
dataType:"script",
method:"GET",
beforeSend: function(xhr) {
xhr.withCredentials = true;
},
success: function(data) {
},
error: function(xhr) {
}
})

使用hook机制,在所有载入的url包含issue的请求中调用上述js

plugins/field_value_adaption/lib/fields_adaption_hook_listener.rb

1
2
3
4
5
6
7
8
9
10
11
class FieldsAdaptionHookListener < Redmine::Hook::ViewListener
def current_is_detail_page(context)
ret = context[:controller] && context[:controller].is_a?(IssuesController) && context[:request].original_url.rindex(//issues/S+/)
end def view_layouts_base_html_head(context)
if current_is_detail_page(context)
javascript_include_tag('fields_adaption_get.js',:plugin => :field_value_adaption)
end
end
end

在init.rb中应用该hook

1
2
3
4
5
6
7
8
9
10
11
require 'redmine'
require_dependency 'fields_adaption_hook_listener.rb' Redmine::Plugin.register :field_value_adaption do
name 'Field Value Adaption plugin'
author 'tuobashao'
description 'This is a plugin for Redmine'
version '0.0.1'
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
end

Redmine it!的更多相关文章

  1. bitnami redmine版本由2.3.1升级至3.2.2过程

    环境: 操作系统为ubuntu13.**版本,非长期支持版. 安装目录:/opt/redmine-2.3.1-0/ 所有者用户:root 安装过程: 1. 备份2.3.1数据库 sudo /opt/r ...

  2. redmine整合GIT版本库

    redmine整合GIT版本库   服务器的环境: Ubuntu 11.10 64位 Redmine 1.4.5.stable.10943 git version 1.7.5.4 + gitolite ...

  3. Centos 6.5 部署 redmine 3.3

    验证ruby版本 如果有就卸载安装最新的 yum install gcc* openssl openssl-devel -y wget https://ruby.taobao.org/mirrors/ ...

  4. Redmine 插件安装

    将对应的插件都复制进redmine的plugins 安装对应所需要的GEMS bundle install --without development test rmagick 执行插件合并 bund ...

  5. redmine问题集锦

    当我新建LDAP认证模式时,遇到如下错误:

  6. Redmine新建问题速度慢

    Redmine有时候新建问题 ,更新指派人的时候反应很慢, 很大原因应该是发送邮件方式不对. 1.一种方式是改为异步发送      2.另外检测到Redmine日志 ,会发现发送邮件失败 ,也会导致发 ...

  7. Testlink与Redmine关联

    TestLink是一个开源的测试管理工具,它可以有效地管理整个测试流程(测试需求, 测试计划, 测试用例, 测试执行, 测试结果分析),但不能和开发流程统一起来,从而不能及时参与到开发中去,不能使项目 ...

  8. redmine常见问题

    1.测试Pop3邮件收件任务:rake redmine:email:receive_pop3 RAILS_ENV="production" host=pop.cecgw.cn po ...

  9. Bitnami Redmine插件记录

    1.bitnami安装时自带了开发环境,如ruby.rails.devkit. 为了版本兼容,应使用bitnami的命令行. 2.通过use_redmine启动命令行:运行Bitnami\redmin ...

  10. redmine export long csv file failed: 502 proxy error

    After modified the file \apps\redmine\conf\httpd-vhosts.conf: <VirtualHost *:8080> ServerName ...

随机推荐

  1. 我的第一次JAVA实训——校园公用房管理系统

    老铁们,昨天电脑没电了.这是上周答应的大项目. 别打脸. 详细内容我之后再写,下面是代码地址. Github地址 附:一个寂寞 以上

  2. rabbit-mq cluster安装

    Centos6.5 安装 RabbitMQ3.6.5 一.安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel op ...

  3. js样式添加

    document.getElementsByName("spans")[index].style.color = "blue";

  4. CodeForces 382B 数学推导

    这个题目题意简单,但是TLE得哭哭的... 输入 a b w x c五个数,最终要使得c<=a, 每一秒可以进行一个操作,如果b>=x,则 b=b-x,同时 c--;如果b<x,则a ...

  5. 【转载】Github上优秀的.NET Core项目

    Github上优秀的.NET Core项目 Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志. ...

  6. Mybatis 使用分页查询亿级数据 性能问题 DB使用ORACLE

    一般用到了mybatis框架分页就不用自己写了 直接用RowBounds对象就可以实现,但这个性能确实很低 今天我用到10w级得数据分页查询,到后面几页就迭代了很慢 用于记录 1.10万级数据如下 [ ...

  7. P3810 【模板】三维偏序(陌上花开)(CDQ分治)

    题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai​.b_ibi​.c_ici​ 三个属性,设 f(i) ...

  8. PAT Advanced 1074 Reversing Linked List (25) [链表]

    题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...

  9. PowerShell创建 Profile

    profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...

  10. Spring核心实现篇

    一.Spring Framework的核心:IoC容器的实现 1.1Spring IoC的容器概述 1.1.1 IoC容器和控制反转模式 依赖控制反转的实现有很多种方式.在Spring中,IOC容器是 ...