Changing the value of a custom field in a Redmine hook

Problem:

In my problems, there are currently 3 custom fields, say FieldA (select list), FieldB (irrelevant) and FieldC (text).

What should happen is that when you save FieldC takes on <FieldA>-<date in Ymd>-<number from database>

As an example, suppose FieldA has the value "Test", and today is the eighth of January 2015. FieldC should be Test-20150108-001, where 001 comes from the user table in the database, which is unique to the FieldA value and is reset every year.

What i did now:

I used the command line script to create a plugin through

 ruby script/rails generate redmine_plugin subticket

And the model through

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 this value, and year is the current year for which lastnum is applicable).

prepend init.rb, , :

 require_dependency 'subticket_hooks'

lib/subticket_hooks.rb :

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

  end
end

, / . , , , , (, !)

, , - !

+4
3

: redmine custom_field_values, CustomFieldValue. CustomFieldValue , .

, , . , , .

:

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

, !

+2

: Users.custom_value_for (field.id)

:? (). .

0

: object.custom_field_value(field.id)

: object.custom_field_values = {field.id => val}. : object.save_custom_field_values. , object.custom_value_for(field.id).id?

Create / Update: Easy. Just add a line object.custom_vield_valuesbefore updating the code. Returns a list of values ​​and starts creating an 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

Documents: Rubydoc

0
source

Source: https://habr.com/ru/post/1570436/


All Articles