Set value of custom field in Redmine hook

58 Views Asked by At

The problem:

My issues have multiply custom fields and one of them (lets name it FieldA) is the result of sql request from DB.

What needs to happen is that on save, FieldA getting the value from DB table.

What I've done to now:

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

require_dependency 'sql_value_hook'

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

class SqlValueHookListener < Redmine::Hook::ViewListener


  def controller_issues_edit_before_save(context={})
        context[:issue].available_custom_fields.each do |field|
            issue = context[:issue]
        if field.is_a?(IssueCustomField)
          if field.field_format == 'sql'
          cv = CustomValue.where(customized_type: "Issue", customized_id: issue.id).includes(:custom_field).where(custom_fields: {type: 'IssueCustomField', name: 'Number'}).first
          sql = field.sql
          result = ActiveRecord::Base.connection.select_all(sql)
          res = result[0]["id"]
          cv.update value: res.to_s
          end
        end
        end
end
end

However, I cannot set it and have FATAL error without description on:

result = ActiveRecord::Base.connection.select_all(sql)

Don't knowing why, but sql line is correct. Maybe, i just using ActiveRecord::Base.connection not correctly, idk. Do note that this is way beyond my abilities since my core expertise is in a completely different language - take it slow!

0

There are 0 best solutions below