]> source.dussan.org Git - redmine.git/commitdiff
Adds a scope to get issues assigned to a user.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 21 Oct 2015 17:00:14 +0000 (17:00 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 21 Oct 2015 17:00:14 +0000 (17:00 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14720 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/my_helper.rb
app/models/issue.rb
app/views/users/show.html.erb
test/unit/issue_test.rb

index acb9d836e896415853a710545696b9229a7dc2a4..a559b9893612d5cc1c84089c34f57a4751ab39f6 100644 (file)
@@ -33,7 +33,7 @@ module MyHelper
 
   def issuesassignedtome_items
     Issue.visible.open.
-      where(:assigned_to_id => ([User.current.id] + User.current.group_ids)).
+      assigned_to(User.current).
       limit(10).
       includes(:status, :project, :tracker, :priority).
       references(:status, :project, :tracker, :priority).
index 3d57c722c72db8787d1858a235111752f1e2a7b0..d07041ff1747556d0d7c767a3e088ae1d717c0e6 100644 (file)
@@ -95,6 +95,13 @@ class Issue < ActiveRecord::Base
     ids = [versions].flatten.compact.map {|v| v.is_a?(Version) ? v.id : v}
     ids.any? ? where(:fixed_version_id => ids) : where('1=0')
   }
+  scope :assigned_to, lambda {|arg|
+    arg = Array(arg).uniq
+    ids = arg.map {|p| p.is_a?(Principal) ? p.id : p}
+    ids += arg.select {|p| p.is_a?(User)}.map(&:group_ids).flatten.uniq
+    ids.compact!
+    ids.any? ? where(:assigned_to_id => ids) : none
+  }
 
   before_validation :clear_disabled_fields
   before_create :default_assign
index c5180885f323673c9db69e4ba6735791792fb32f..a4d0c4b5b093f692fb07de66e120807b4d1d8b85 100644 (file)
@@ -27,7 +27,7 @@
 <ul>
   <li><%= link_to l(:label_assigned_issues),
         issues_path(:set_filter => 1, :assigned_to_id => 'me', :sort => 'priority:desc,updated_on:desc') %>:
-      <%= Issue.visible.open.where(:assigned_to_id => ([@user.id] + @user.group_ids)).count %>
+      <%= Issue.visible.open.assigned_to(@user).count %>
   <li><%= link_to l(:label_reported_issues),
         issues_path(:set_filter => 1, :status_id => '*', :author_id => @user.id) %>:
       <%= Issue.visible.where(:author_id => @user.id).count %>
index bed63b767476e76a8a0b2832319618f73fca2098..4b9fbda0b1fa3c753b1168890bfb98f090a936c0 100644 (file)
@@ -418,6 +418,22 @@ class IssueTest < ActiveSupport::TestCase
     assert_equal 0, Issue.fixed_version([]).count
   end
 
+  def test_assigned_to_scope_should_return_issues_assigned_to_the_user
+    user = User.generate!
+    issue = Issue.generate!
+    Issue.where(:id => issue.id).update_all :assigned_to_id => user.id
+    assert_equal [issue], Issue.assigned_to(user).to_a
+  end
+
+  def test_assigned_to_scope_should_return_issues_assigned_to_the_user_groups
+    group = Group.generate!
+    user = User.generate!
+    group.users << user
+    issue = Issue.generate!
+    Issue.where(:id => issue.id).update_all :assigned_to_id => group.id
+    assert_equal [issue], Issue.assigned_to(user).to_a
+  end
+
   def test_errors_full_messages_should_include_custom_fields_errors
     field = IssueCustomField.find_by_name('Database')