2

i need to solve a specific problem for Redmine, but i'm a newby in Ruby and Ruby on Rails in particular.

so what i need.

I have some developers in Redmine. For each developer (=user) i need to show (on Homepage and MyPage) the priorities of projects for this user specified by somebody. E.g.:

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

Model. Create a table user_project_prios:

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

Create a model (might look junky, just starting with 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

Controller. I know for home page i can use hooks. like so

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

Now the problem here is user_id. Class User in Redmine seems to be does not expose it's id for public. So how do i find UserProjectPrios's for the current user ?

Or i'm really in the wrong way...?

zishe
  • 10,665
  • 12
  • 64
  • 103
Sergey Poskachey
  • 331
  • 2
  • 12

1 Answers1

0

YEah, so the simple way is:

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

And the model

class UserProjectPrio < ActiveRecord::Base
  belongs_to :project
end

And then, just loop over user_project_prios in template and fetch project like

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

But now i have problems with table. I use the following creation code:

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

And for the resulting fields user_id, project_id no foreign keys are created. Am i missing anything ?

Sergey Poskachey
  • 331
  • 2
  • 12