2013-07-16 84 views
2

我需要解決Redmine的特定問題,但我特別是Ruby和Ruby on Rails中的newby。Redmine用戶項目優先級插件

所以我需要。

我在Redmine有一些開發人員。對於每個開發者(=用戶),我需要顯示(在主頁和我的頁面)這個用戶指定的項目的優先級。例如:

Jhon: 
--- 
1. Project1 
2. Project2 
... 
Mary: 
--- 
1. Project2 
2. Project23 
3. Project1 
... 
The solution i see is the following (assuming plugin is called UserProjectPrios)。

型號。創建一個表user_project_prios

  • USER_ID(FK:用戶)
  • PROJECT_ID(FK:項目)
  • 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

或者我真的以錯誤的方式......?

+0

那麼,我錯了,該用戶ID是可用的。只是User.current.id。但主要問題仍然存在:是否有更好的解決方案? –

回答

0

呀,這麼簡單的方法就是:

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沒有創建外鍵。我錯過了什麼?

+0

我有同樣的問題 – eri