redmine 插件编写示例
时间: 2024-02-01 11:03:00 浏览: 34
以下是一个简单的 Redmine 插件编写示例,用于添加一个自定义的字段到问题页面:
1. 创建一个插件目录,例如 `my_plugin`。
2. 在插件目录下创建一个 `init.rb` 文件,将以下代码复制粘贴到文件中:
```ruby
Redmine::Plugin.register :my_plugin do
name 'My plugin'
author 'Your Name'
description 'A plugin for Redmine'
version '0.0.1'
url 'https://example.com/my_plugin'
author_url 'https://example.com'
end
require_dependency 'issue'
# 添加字段到问题页面
module MyPlugin
module IssuePatch
def self.included(base)
base.class_eval do
unloadable
safe_attributes 'my_field'
before_save :update_my_field
end
end
def my_field
@my_field ||= CustomField.find_by_name('My Field').try(:value_for, self)
end
def my_field=(value)
@my_field = value
end
def update_my_field
custom_field = CustomField.find_by_name('My Field')
if custom_field
self.custom_field_values = { custom_field.id => @my_field }
end
end
end
end
Issue.send(:include, MyPlugin::IssuePatch)
```
3. 在插件目录下创建一个 `assets/stylesheets/my_plugin.css` 文件,将以下代码复制粘贴到文件中:
```css
/* 添加自定义样式 */
#issue-form .my-field {
margin-bottom: 1em;
}
```
4. 在插件目录下创建一个 `app/views/hooks/my_plugin_issues_form_details_bottom.html.erb` 文件,将以下代码复制粘贴到文件中:
```erb
<!-- 添加自定义字段到问题页面 -->
<div class="splitcontent">
<div class="splitcontentleft">
<%= f.label :my_field, 'My Field' %>
</div>
<div class="splitcontentright">
<%= f.text_field :my_field, :class => 'my-field' %>
</div>
</div>
```
5. 在插件目录下创建一个 `app/views/hooks/my_plugin_issues_show_details_bottom.html.erb` 文件,将以下代码复制粘贴到文件中:
```erb
<!-- 显示自定义字段 -->
<% if @issue.my_field.present? %>
<div class="attribute">
<strong>My Field:</strong> <%= h(@issue.my_field) %>
</div>
<% end %>
```
6. 启用插件,登录到 Redmine 管理界面,进入 `Administration` -> `Plugins`,找到 `My plugin` 插件并点击 `Enable` 按钮。
7. 创建一个新的问题,您应该能够看到一个新的字段 `My Field`。
这个示例演示了如何使用 Redmine 插件添加自定义字段和自定义样式到问题页面,您可以根据自己的需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)