扩展 Redmine 用户模型

Extending Redmine User Model

我是 Rails 上 Ruby 的新手,我正在尝试创建一个插件。作为第一步,我尝试使用新的表单字段修改用户帐户页面。到目前为止,我有这个字段,如果我在数据库中手动插入数据,它会被预先填充,但是,我无法将它保存到输入的数据中。

我正在使用 view_my_account 挂钩。我还在我的补丁中使用 validates_presence_of 进行了验证设置。当我提交一个空字段和一个填充字段时它会抛出一个错误——我假设是因为该字段没有与 Redmine

中的 @user.save 正确连接

这是我拥有的:

init.rb

require 'redmine'

# Patches to Redmine Core
require 'user_patch'

# Hooks
require_dependency 'account_view_hook'

Redmine::Plugin.register :myTest do
 # all plugin info
end

plugins/myTest/lib/user_patch.rb

require_dependency 'user'

module UserPatch
    def self.included(base)
        base.extend(ClassMethods)

        base.send(:include, InstanceMethods)

        base.class_eval do
            unloadable

            validates_presence_of :my_new_field

        end
    end

    module ClassMethods
    end

    module InstanceMethods
    end
end

ActionDispatch::Callbacks.to_prepare do
  User.send(:include, UserPatch)
end

plugins/myTest/lib/account_view_hook.rb

class AccountViewHook < Redmine::Hook::ViewListener
  render_on :view_my_account, :partial => 'account/my_new_field'
end

plugins/myTest/app/views/account/_my_new_field.html.erb

<p><%= form.text_field :my_new_field, :required => true %></p>

以及我要连接到的原始页面 (redmine/app/views/my/account.html.erb)

<div class="contextual">
<%= additional_emails_link(@user) %>
<%= link_to(l(:button_change_password), {:action => 'password'}, :class => 'icon icon-passwd') if @user.change_password_allowed? %>
<%= call_hook(:view_my_account_contextual, :user => @user)%>
</div>

<h2>
  <%= avatar_edit_link(@user, :size => "50") %>
  <%=l(:label_my_account)%>
</h2>

<%= error_messages_for 'user' %>

<%= labelled_form_for :user, @user,
                     :url => { :action => "account" },
                     :html => { :id => 'my_account_form',
                                :method => :post } do |f| %>
<div class="splitcontentleft">
<fieldset class="box tabular">
  <legend><%=l(:label_information_plural)%></legend>
  <p><%= f.text_field :firstname, :required => true %></p>
  <p><%= f.text_field :lastname, :required => true %></p>
  <p><%= f.text_field :mail, :required => true %></p>
  <% unless @user.force_default_language? %>
  <p><%= f.select :language, lang_options_for_select %></p>
  <% end %>
  <% if Setting.openid? %>
  <p><%= f.text_field :identity_url  %></p>
  <% end %>

  <% @user.custom_field_values.select(&:editable?).each do |value| %>
    <p><%= custom_field_tag_with_label :user, value %></p>
  <% end %>
  <%= call_hook(:view_my_account, :user => @user, :form => f) %>
</fieldset>

<%= submit_tag l(:button_save) %>
</div>

<div class="splitcontentright">
<fieldset class="box">
  <legend><%=l(:field_mail_notification)%></legend>
  <%= render :partial => 'users/mail_notifications' %>
</fieldset>

<fieldset class="box tabular">
  <legend><%=l(:label_preferences)%></legend>
  <%= render :partial => 'users/preferences' %>
  <%= call_hook(:view_my_account_preferences, :user => @user, :form => f) %>
</fieldset>

</div>
<% end %>

<% content_for :sidebar do %>
<%= render :partial => 'sidebar' %>
<% end %>

<% html_title(l(:label_my_account)) -%>

必须先将新字段添加到模型中。

常见于Ruby on Rails 你必须生成运行 migration,然后字段执行才有效。

但是Redmine支持模型扩展,无需迁移,甚至无需编程。如果您想将字段添加到 user 模型,请使用可以在 Redmine 管理设置中添加的 custom fields。新字段将立即显示在 user 表格中,无需重新启动 Redmine。

当然,您可以根据需要使用视图挂钩来查看字段,并更改字段的标准执行。请参阅我的 answer 以了解如何使用自定义字段值。