更改Redmine挂钩中自定义字段的值 [英] Change value of custom field in Redmine hook

查看:123
本文介绍了更改Redmine挂钩中自定义字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我的问题目前有 3 个自定义字段,比如 FieldA(选择列表)、FieldB(不相关)和 FieldC(文本).

My issues currently have 3 custom fields, let's say FieldA (select list), FieldB (irrelevant), and FieldC (text).

需要发生的是,在保存时,FieldC 取--

What needs to happen is that on save, FieldC takes the value of <FieldA>-<date in Ymd>-<number from database>

举个例子,假设 FieldA 的值为Test",今天是 2015 年 1 月 8 日.那么 FieldC 应该是 Test-20150108-001,其中 001 来自数据库中的自定义表,这是唯一的FieldA 的值,并每年重置.

As an example, let's assume that FieldA has the value "Test", and today is the 8th of Jan 2015. FieldC should then be Test-20150108-001 where 001 comes from a custom table in the database, that's unique per value of FieldA, and resets every year.

我现在所做的:

我使用命令行脚本通过

 ruby script/rails generate redmine_plugin subticket

和一个模型通过

ruby script/rails generate redmine_plugin_model subticket subticket_ids fa:string lastnum:integer year:integer

(其中 fa 是 FieldA 的值,lastnum 是用于该值的最后一个数字,而 year 是 lastnum 适用的当前年份).

( where fa is the value of FieldA, lastnum is the last number used for that value, and year is the current year for which the lastnum is applicable ).

然后我继续在 init.rb 前面添加一个钩子侦听器以实现预存钩子:

I've then proceeded to prepend init.rb to add a hook listener in order to implenent the presave hooks:

 require_dependency 'subticket_hooks'

并创建文件lib/subticket_hooks.rb,内容如下:

And created the file lib/subticket_hooks.rb with the following content:

class SubticketHooksListener < Redmine::Hook::ViewListener
  def controller_issues_edit_before_save(context={})
    issue = context[:issue]

  end
end

但是,我在此处找不到有关如何访问/写入自定义字段值的任何文档.仍在努力使模型也能正常工作,但我认为文档对此足够清楚,我可以进行实验(当然,欢迎提供任何信息!)

However, I cannot find any documentation on how to access/write the value of a custom field here. Still working on making the model work as well, but I assume the documentation is clear enough on that for me to experiment (of course any info is welcomed!)

请注意,这超出了我的能力范围,因为我的核心专业知识是一种完全不同的语言 - 慢慢来!

Do note that this is way beyond my abilities since my core expertise is in a completely different language - take it slow!

推荐答案

我有同样的任务

我的解决方案:每个可自定义的 redmine 对象都有 custom_field_values 字段,该值是 CustomFieldValue 的数组.CustomFieldValue 包含当前值、自定义字段描述和自定义对象.

My solution: Every customizable redmine object has custom_field_values field, that value is array of CustomFieldValue. CustomFieldValue contains current value, custom field description and customized object.

我通过整理读取和更改所需的值.可能它不是最好的变体,但我不久前就熟悉了 ruby​​ 语言.

Needed values i reads and alters by sort out. May be it's not best variant, but i acquainted with ruby language not so long ago.

读取自定义字段值的方法:

Method for reading custom fields values:

def object_custom_field_value(object, field_name)
  object.custom_field_values.each do |field|
    if field.custom_field.name == field_name
      return field.value
    end
  end
end

为了改变:

def object_custom_field_set_value(object, field_name, value)
  object.custom_field_values.each do |field|
    if field.custom_field.name == field_name
      field.value = value
    end
  end
end

希望这会有所帮助!

这篇关于更改Redmine挂钩中自定义字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆