redmine 自定义字段

自定义一个关联其他表的字段。

使用这个插件:

http://projects.andriylesyuk.com/projects/extended-fields/wiki

基于插件,添加对应工程项目的model:

ruby script/rails generate redmine_plugin_model extended_fields customproject skey:string sname:string

并修改生成的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


这里可以看到,判断类型为customproject时,调用了添加的方法:
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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redmine是一个开源的项目管理工具,用于跟踪和管理项目的进展、问题和任务。升级Redmine可以带来许多好处,包括修复漏洞、增加新功能和提高性能。下面是升级Redmine的一般步骤: 1.备份数据库:在升级之前,务必备份Redmine的数据库,以防止数据丢失。 2.备份文件:同样地,还需要备份Redmine的文件和配置,包括插件、主题和自定义修改等。 3.下载新版本:从Redmine官方网站或其他可信来源下载最新版本的Redmine。 4.解压文件:将下载的压缩包解压到一个新的目录中。 5.合并自定义修改:如果之前对Redmine进行了自定义修改,需要将这些修改应用到新版本中。可以使用比较工具来比较旧版本和新版本之间的差异,并手动合并修改。 6.更新Gemfile:打开Gemfile文件,将其中的Redmine版本号更新为新版本。 7.更新依赖库:运行bundle install命令来更新Redmine所依赖的库。 8.运行数据库迁移:运行rake db:migrate命令来执行数据库迁移,以更新数据库结构。 9.重新编译资产:运行rake assets:precompile命令来重新编译Redmine的资产文件。 10.重启Redmine:重启Redmine应用程序,以使升级生效。 请注意,升级Redmine可能会涉及到一些特定的步骤和注意事项,具体取决于你当前使用的Redmine版本和所需升级的版本。因此,在进行升级之前,建议先阅读Redmine官方文档或社区论坛上的相关指南,以确保顺利完成升级过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值