Redmine用户项目优先级插件

时间:2013-07-16 12:37:41

标签: ruby-on-rails redmine redmine-plugins

我需要为Redmine解决一个特定的问题,但我特别喜欢Ruby和Ruby on Rails。

所以我需要的。

我在Redmine有一些开发人员。对于每个开发人员(=用户),我需要显示(在Homepage和MyPage上)某人指定的该用户的项目优先级。例如。:

Jhon:
---
1. Project1
2. Project2
...
Mary:
---
1. Project2
2. Project23
3. Project1
...
我看到的解决方案如下(假设插件名为UserProjectPrios)。

模型。创建表user_project_prios

  • user_id(fk:user)
  • project_id(fk:project)
  • prio(int)

创建一个模型(可能看起来很垃圾,只是从RnR开始:)

class UserProjectPrio < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
  attr_reader :project, :prio

  def initialize (project_id, prio)
    @project = Project.find(project_id)
    @prio = prio
  end

  def self.get_user_projects(user_id)
    user_project_prios = []
    self.find_by_user_id(user_id).each do |up|
      user_project_prios.push(self.new(up.project_id, up.prio, up.enum_issueprio_position))
    end

    user_project_prios
  end
end

控制器。我知道主页我可以使用钩子。像这样

class Hooks < Redmine::Hook::ViewListener
  def view_welcome_index_left (context = {})
    context[:user_name] = User.current.name;
    context[:user_project_prios] = UserProjectPrio.get_user_projects(???user_id???);

    context[:controller].send(:render_to_string, {
        :partial => "hooks/user_project_prios/user_project_prios",
        :locals => context
    })
  end
end

现在问题是user_id。 Redmine中的类用户似乎没有公开它的id。那么如何为当前用户找到UserProjectPrios

或者我的确错了......?

1 个答案:

答案 0 :(得分:0)

YEah,所以简单的方法是:

class Hooks < Redmine::Hook::ViewListener
  def view_welcome_index_left (context = {})
    user_project_prios = UserProjectPrio.all(:conditions => { :user_id => User.current.id }, :order => "prio ASC");

    context[:user_name] = User.current.name;
    context[:user_project_prios] = user_project_prios

    context[:controller].send(:render_to_string, {
        :partial => "hooks/user_project_prios/user_project_prios",
        :locals => context
    })
  end
end

和模型

class UserProjectPrio < ActiveRecord::Base
  belongs_to :project
end

然后,只需在模板中循环遍历user_project_prios并获取项目,如

<ul>
  <% user_project_prios.each do |upp| %>
     <li><%= upp.project.name %></li>
  <% end %>
</ul>

但现在我的桌子有问题了。 我使用以下创建代码:

class CreateUserProjectPrios < ActiveRecord::Migration
  def self.up
    create_table :user_project_prios do |t|
      t.references :user
      t.references :project
      t.string :prio
      t.string :enum_issueprio_position
    end

    change_table :user_project_prios do |t|
      t.index ([:user_id, :project_id, :prio, :enum_issueprio_position], :name => 'unique_key', :unique => true)
    end
  end

  def self.down
    drop_table :user_project_prios
  end
end

对于结果字段user_id,project_id不会创建任何外键。我错过了什么吗?