user2743331
user2743331

Reputation: 31

Redmine Plugin: How to add Project Custom Field

I want to create a Project Custom Field in a plugin. Although running "rake redmine:plugins:migrate RAILS_ENV=production" appears to apply the migration, the project custom field is not created. Here is the migration:

Update: (can't answer my own Question so I'll fix the post with the answer).

class PopulateCustomFields < ActiveRecord::Migration
def self.up
  ProjectCustomField.create(:name => 'Estimated time units'; :field_format => 'list', :possible_values => ['Hours', 'Points'], :default_value => 'Hours', :is_required => true, :editable => true, :visible => true)
end

(I previously had the wrong syntax for :possible_values, I was using {} instead of [], but no error was reported ).

Upvotes: 3

Views: 1702

Answers (1)

Miguel Rentes
Miguel Rentes

Reputation: 1003

I believe you have a typo after 'Estimated time units'. Replace the ';' by ','.

You can test your plugin migration script by installing/removing the plugin using the following commands:

  • for installing the plugin:

rake redmine:plugins:migrate RAILS_ENV=production

  • for uninstalling the plugin:

rake redmine:plugins:migrate NAME=plugin_name VERSION=0 RAILS_ENV=production

Check the official documentation for further details.

An example migration script could be like this:

class PopulateCustomFields < ActiveRecord::Migration
  # method called when installing the plugin
  def self.up
    if CustomField.find_by_name('A New Custom Field').nil?
      CustomField.create(name: 'A New Custom Field', field_format: 'text')
    end
  end

  # method called when installing the plugin
  def self.down
    CustomField.find_by_name('A New Custom Field').delete unless CustomField.find_by_name('A New Custom Field').nil?
  end
end

Check on the redmine database if the custom field 'A New Custom Field' of type 'text' is correctly added/removed when installing/uninstalling the plugin. Also, check that each step has no output errors (see the redmine log as well).

Upvotes: 2

Related Questions