rubyredmineredmine-plugins

How to fetch the possible values of a custom field?


I created a key/value-pair-typed custom field and added some values to it. I now need to write a plugin that hooks into the creation of a new issue, checks the subject and finds a match in the possible values of that field - and if one found, assings that value to the issue.
I'm still struggling to simply get all the possible values of that field in my plugin. I found CustomField.find(id).possible_values and tried to log it with the correct id of my field, but it just shows [].

My code so far:

module My_Plugin
  class Hooks < Redmine::Hook::ViewListener
    def controller_issues_new_before_save(context={ })
      issue = context[:issue]
      project = Project.find(issue[:project_id].to_i)
      if project.name === "MyProjectName"
        File.write('/tmp/redmine', CustomField.find(4).possible_values)            
      end
    end
  end
end

What am I doing wrong? If I call /custom_fields/4/enumerations I can see plenty of active field values.


Solution

  • Okay I found it (CustomField.find(ID_OF_FIELD).enumerations.each_with_index).

    module My_Plugin
      class Hooks < Redmine::Hook::ViewListener
        def controller_issues_new_before_save(context={ })
    
          issue = context[:issue]
          project = Project.find(issue[:project_id].to_i)
          if project.name === "MyProjectName"
              CustomField.find(4).enumerations.each_with_index do |value, position|
                  File.write('/tmp/redmine_'+value.id.to_s, value.name)
              end
          end
        end
      end
    end