##// END OF EJS Templates
Adds named scopes to replace custom finders....
Eric Davis -
r3443:22c1e2b8cf85
parent child
Show More
@@ -49,8 +49,9 class Issue < ActiveRecord::Base
49 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
49 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
50
50
51 attr_reader :current_journal
51 attr_reader :current_journal
52
52
53 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
53 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
54
54 validates_length_of :subject, :maximum => 255
55 validates_length_of :subject, :maximum => 255
55 validates_inclusion_of :done_ratio, :in => 0..100
56 validates_inclusion_of :done_ratio, :in => 0..100
56 validates_numericality_of :estimated_hours, :allow_nil => true
57 validates_numericality_of :estimated_hours, :allow_nil => true
@@ -60,6 +61,11 class Issue < ActiveRecord::Base
60
61
61 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
62 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
62
63
64 named_scope :recently_updated, :order => "#{self.table_name}.updated_on DESC"
65 named_scope :with_limit, lambda { |limit| { :limit => limit} }
66 named_scope :on_active_project, :include => [:status, :project, :tracker],
67 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
68
63 before_create :default_assign
69 before_create :default_assign
64 before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
70 before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
65 after_save :create_journal
71 after_save :create_journal
@@ -1,10 +1,5
1 <h3><%=l(:label_watched_issues)%> (<%= Issue.visible.count(:include => :watchers,
1 <h3><%=l(:label_watched_issues)%> (<%= Issue.visible.watched_by(user.id).count %>)</h3>
2 :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]) %>)</h3>
2 <% watched_issues = Issue.visible.on_active_project.watched_by(user.id).recently_updated.with_limit(10) %>
3 <% watched_issues = Issue.visible.find(:all,
4 :include => [:status, :project, :tracker, :watchers],
5 :limit => 10,
6 :conditions => ["#{Watcher.table_name}.user_id = ?", user.id],
7 :order => "#{Issue.table_name}.updated_on DESC") %>
8
3
9 <%= render :partial => 'issues/list_simple', :locals => { :issues => watched_issues } %>
4 <%= render :partial => 'issues/list_simple', :locals => { :issues => watched_issues } %>
10 <% if watched_issues.length > 0 %>
5 <% if watched_issues.length > 0 %>
@@ -656,4 +656,24 class IssueTest < ActiveSupport::TestCase
656 assert_equal 2, groups.size
656 assert_equal 2, groups.size
657 assert_equal 5, groups.inject(0) {|sum, group| sum + group['total'].to_i}
657 assert_equal 5, groups.inject(0) {|sum, group| sum + group['total'].to_i}
658 end
658 end
659
660 def test_recently_updated_with_limit_scopes
661 #should return the last updated issue
662 assert_equal 1, Issue.recently_updated.with_limit(1).length
663 assert_equal Issue.find(:first, :order => "updated_on DESC"), Issue.recently_updated.with_limit(1).first
664 end
665
666 def test_on_active_projects_scope
667 assert Project.find(2).archive
668
669 before = Issue.on_active_project.length
670 # test inclusion to results
671 issue = Issue.generate_for_project!(Project.find(1), :tracker => Project.find(2).trackers.first)
672 assert_equal before + 1, Issue.on_active_project.length
673
674 # Move to an archived project
675 issue.project = Project.find(2)
676 assert issue.save
677 assert_equal before, Issue.on_active_project.length
678 end
659 end
679 end
@@ -15,6 +15,10 module Redmine
15 has_many :watchers, :as => :watchable, :dependent => :delete_all
15 has_many :watchers, :as => :watchable, :dependent => :delete_all
16 has_many :watcher_users, :through => :watchers, :source => :user
16 has_many :watcher_users, :through => :watchers, :source => :user
17
17
18 named_scope :watched_by, lambda { |user_id|
19 { :include => :watchers,
20 :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] }
21 }
18 attr_protected :watcher_ids, :watcher_user_ids
22 attr_protected :watcher_ids, :watcher_user_ids
19 end
23 end
20 end
24 end
@@ -60,14 +64,7 module Redmine
60 notified.collect(&:mail).compact
64 notified.collect(&:mail).compact
61 end
65 end
62
66
63 module ClassMethods
67 module ClassMethods; end
64 # Returns the objects that are watched by user
65 def watched_by(user)
66 find(:all,
67 :include => :watchers,
68 :conditions => ["#{Watcher.table_name}.user_id = ?", user.id])
69 end
70 end
71 end
68 end
72 end
69 end
73 end
70 end
General Comments 0
You need to be logged in to leave comments. Login now