自定义一个关联其他表的字段。
使用这个插件:
http://projects.andriylesyuk.com/projects/extended-fields/wiki
基于插件,添加对应工程项目的model:
并修改生成的Customproject类:
class Customproject < ActiveRecord::Base
include Redmine::SafeAttributes
scope :visible, lambda {|*args| }
unloadable
end
拷贝目录/usr/share/redmine/plugins/extended_fields/app/views/custom_values/common
中的_project.html.erb 一份拷贝为_customproject.html.erb
和_project.text.erb 一份拷贝为 _customproject.text.erb
并修改两份文件中的Project为Customproject。_customproject.html.erb的内容修改为_customproject.text.erb的内容。
转到/usr/share/redmine目录,重新安装插件:
sudo rake redmine:plugins:migrate RAILS_ENV=production
目录/usr/share/redmine/plugins/extended_fields/lib中,拷贝project_custom_field_format.rb为customproject_custom_field_format.rb
并将文件内容修改为:
class CustomprojectCustomFieldFormat < Redmine::CustomFieldFormat
def format_as_customproject(value)
Customproject.find(value).sname
rescue
nil
end
def edit_as
'customproject'
end
end
修改init.rb,注册类型:
fields.register CustomprojectCustomFieldFormat.new('customproject', :label => :label_customproject, :order => base_order + 7)
国际化:
拷贝/usr/share/redmine/plugins/extended_fields/config/locales中的en.yml为zh.yml
并修改为中文
修改库:
在lib中找到extended_custom_field_patch.rb,修改对应project的内容,相应到customproject
修改extended_fields_helper_patch.rb,修改对应的project的内容到相应的customproject
至此,应该可以看到列表显示了。
修改编辑时,列表过长导致选中困难的问题,策略是可以在录入的时候进行搜索,并只列出包含搜索词的选项。
修改/usr/share/redmine/app/helpers目录下的custom_fields_helper.rb文件,查找方法:
# Return custom field tag with its label tag
def custom_field_tag_with_label(name, custom_value, options={})
custom_field = custom_value.custom_field
field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
case field_format.try(:edit_as)
when "customproject"
custom_field_label_tag(name, custom_value, options) + custom_field_tag_customproject(name, custom_value)
else
custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value)
end
end
custom_field_tag_customproject(name, custom_value)
此方法定义如下:
def custom_field_tag_customproject(name, custom_value)
custom_field = custom_value.custom_field
field_name = "#{name}[custom_field_values][#{custom_field.id}]"
field_name << "[]" if custom_field.multiple?
field_id = "#{name}_custom_field_values_#{custom_field.id}"
tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
blank_option = content_tag('option')
field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
tag_options.merge(:multiple => custom_field.multiple?))
s = s + javascript_tag(" $('#" + field_id + "').addClass('chosen-select').chosen({search_contains:true}).closest('p').css({'overflow':'visible'}).closest('div.splitcontent').css({'overflow':'visible'});")
s
end
采用了chosen插件,当然要将插件的js和样式引入:
将css和javascript文件拷贝到redmine/public/stylesheet和redmine/public/javascript目录,并且修改redmine/app/views/layouts/base.html.erb文件,添加js和样式:
<%= stylesheet_link_tag 'chosen', 'application', :media => 'all' %>
<%= javascript_include_tag "chosen.jquery" %>
可以看到效果啦!
在编辑页面看到chosen生成的选择组件宽度为0,修改chosen.jquery.js文件中的获取宽度方法:
AbstractChosen.prototype.container_width = function() {
if (this.options.width != null && this.options.width != 0) {
return this.options.width;
} else {
if(this.form_field.offsetWidth != 0){
return "" + this.form_field.offsetWidth + "px";
}else{
return "200px";
}
}
};
添加判断如果宽度为0,则使用宽度为200px