问题
The problem:
My issues currently have 3 custom fields, let's say FieldA (select list), FieldB (irrelevant), and FieldC (text).
What needs to happen is that on save, FieldC takes the value of <FieldA>-<date in Ymd>-<number from database>
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.
What I've done to now:
I've used the command line script to generate a plugin via
ruby script/rails generate redmine_plugin subticket
And a model via
ruby script/rails generate redmine_plugin_model subticket subticket_ids fa:string lastnum:integer year:integer
( 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 ).
I've then proceeded to prepend init.rb
to add a hook listener in order to implenent the presave hooks:
require_dependency 'subticket_hooks'
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!
回答1:
I had the same task
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.
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
And for changing:
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
Hope this helps!
回答2:
Get: object.custom_field_value(field.id)
Update: object.custom_field_values = {field.id => val}
. Don't forget to save: object.save_custom_field_values
. However it doesn't work for creating value, so check via object.custom_value_for(field.id).id?
Create/Update:
Easy. Just add a line object.custom_vield_values
before update code. It returns list of values, and triggers creation of empty value. Example
u = User.find(1)
cf = CustomField.where('name', 'mobile phone').first
u.custom_field_values # returns values, triggers creation
u.custom_field_values = {cf.id = "+123456789"} # set value
u.save_custom_field_values # save
Docs: rubydoc
来源:https://stackoverflow.com/questions/27837058/change-value-of-custom-field-in-redmine-hook