##// END OF EJS Templates
Adds missing scope for r2595....
Jean-Philippe Lang -
r2528:f0c676d3df2c
parent child
Show More
@@ -1,292 +1,294
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Issue < ActiveRecord::Base
18 class Issue < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :tracker
20 belongs_to :tracker
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27
27
28 has_many :journals, :as => :journalized, :dependent => :destroy
28 has_many :journals, :as => :journalized, :dependent => :destroy
29 has_many :time_entries, :dependent => :delete_all
29 has_many :time_entries, :dependent => :delete_all
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31
31
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34
34
35 acts_as_attachable :after_remove => :attachment_removed
35 acts_as_attachable :after_remove => :attachment_removed
36 acts_as_customizable
36 acts_as_customizable
37 acts_as_watchable
37 acts_as_watchable
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 :include => [:project, :journals],
39 :include => [:project, :journals],
40 # sort by id so that limited eager loading doesn't break with postgresql
40 # sort by id so that limited eager loading doesn't break with postgresql
41 :order_column => "#{table_name}.id"
41 :order_column => "#{table_name}.id"
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45
45
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 :author_key => :author_id
47 :author_key => :author_id
48
48
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
50 validates_length_of :subject, :maximum => 255
50 validates_length_of :subject, :maximum => 255
51 validates_inclusion_of :done_ratio, :in => 0..100
51 validates_inclusion_of :done_ratio, :in => 0..100
52 validates_numericality_of :estimated_hours, :allow_nil => true
52 validates_numericality_of :estimated_hours, :allow_nil => true
53
53
54 named_scope :visible, lambda {|*args| { :include => :project,
54 named_scope :visible, lambda {|*args| { :include => :project,
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
56
56
57 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
58
57 # Returns true if usr or current user is allowed to view the issue
59 # Returns true if usr or current user is allowed to view the issue
58 def visible?(usr=nil)
60 def visible?(usr=nil)
59 (usr || User.current).allowed_to?(:view_issues, self.project)
61 (usr || User.current).allowed_to?(:view_issues, self.project)
60 end
62 end
61
63
62 def after_initialize
64 def after_initialize
63 if new_record?
65 if new_record?
64 # set default values for new records only
66 # set default values for new records only
65 self.status ||= IssueStatus.default
67 self.status ||= IssueStatus.default
66 self.priority ||= Enumeration.priorities.default
68 self.priority ||= Enumeration.priorities.default
67 end
69 end
68 end
70 end
69
71
70 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
72 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
71 def available_custom_fields
73 def available_custom_fields
72 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
74 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
73 end
75 end
74
76
75 def copy_from(arg)
77 def copy_from(arg)
76 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
78 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
77 self.attributes = issue.attributes.dup
79 self.attributes = issue.attributes.dup
78 self.custom_values = issue.custom_values.collect {|v| v.clone}
80 self.custom_values = issue.custom_values.collect {|v| v.clone}
79 self
81 self
80 end
82 end
81
83
82 # Moves/copies an issue to a new project and tracker
84 # Moves/copies an issue to a new project and tracker
83 # Returns the moved/copied issue on success, false on failure
85 # Returns the moved/copied issue on success, false on failure
84 def move_to(new_project, new_tracker = nil, options = {})
86 def move_to(new_project, new_tracker = nil, options = {})
85 options ||= {}
87 options ||= {}
86 issue = options[:copy] ? self.clone : self
88 issue = options[:copy] ? self.clone : self
87 transaction do
89 transaction do
88 if new_project && issue.project_id != new_project.id
90 if new_project && issue.project_id != new_project.id
89 # delete issue relations
91 # delete issue relations
90 unless Setting.cross_project_issue_relations?
92 unless Setting.cross_project_issue_relations?
91 issue.relations_from.clear
93 issue.relations_from.clear
92 issue.relations_to.clear
94 issue.relations_to.clear
93 end
95 end
94 # issue is moved to another project
96 # issue is moved to another project
95 # reassign to the category with same name if any
97 # reassign to the category with same name if any
96 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
98 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
97 issue.category = new_category
99 issue.category = new_category
98 issue.fixed_version = nil
100 issue.fixed_version = nil
99 issue.project = new_project
101 issue.project = new_project
100 end
102 end
101 if new_tracker
103 if new_tracker
102 issue.tracker = new_tracker
104 issue.tracker = new_tracker
103 end
105 end
104 if options[:copy]
106 if options[:copy]
105 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
107 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
106 issue.status = self.status
108 issue.status = self.status
107 end
109 end
108 if issue.save
110 if issue.save
109 unless options[:copy]
111 unless options[:copy]
110 # Manually update project_id on related time entries
112 # Manually update project_id on related time entries
111 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
113 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
112 end
114 end
113 else
115 else
114 Issue.connection.rollback_db_transaction
116 Issue.connection.rollback_db_transaction
115 return false
117 return false
116 end
118 end
117 end
119 end
118 return issue
120 return issue
119 end
121 end
120
122
121 def priority_id=(pid)
123 def priority_id=(pid)
122 self.priority = nil
124 self.priority = nil
123 write_attribute(:priority_id, pid)
125 write_attribute(:priority_id, pid)
124 end
126 end
125
127
126 def estimated_hours=(h)
128 def estimated_hours=(h)
127 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
129 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
128 end
130 end
129
131
130 def validate
132 def validate
131 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
133 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
132 errors.add :due_date, :not_a_date
134 errors.add :due_date, :not_a_date
133 end
135 end
134
136
135 if self.due_date and self.start_date and self.due_date < self.start_date
137 if self.due_date and self.start_date and self.due_date < self.start_date
136 errors.add :due_date, :greater_than_start_date
138 errors.add :due_date, :greater_than_start_date
137 end
139 end
138
140
139 if start_date && soonest_start && start_date < soonest_start
141 if start_date && soonest_start && start_date < soonest_start
140 errors.add :start_date, :invalid
142 errors.add :start_date, :invalid
141 end
143 end
142 end
144 end
143
145
144 def validate_on_create
146 def validate_on_create
145 errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
147 errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
146 end
148 end
147
149
148 def before_create
150 def before_create
149 # default assignment based on category
151 # default assignment based on category
150 if assigned_to.nil? && category && category.assigned_to
152 if assigned_to.nil? && category && category.assigned_to
151 self.assigned_to = category.assigned_to
153 self.assigned_to = category.assigned_to
152 end
154 end
153 end
155 end
154
156
155 def before_save
157 def before_save
156 if @current_journal
158 if @current_journal
157 # attributes changes
159 # attributes changes
158 (Issue.column_names - %w(id description)).each {|c|
160 (Issue.column_names - %w(id description)).each {|c|
159 @current_journal.details << JournalDetail.new(:property => 'attr',
161 @current_journal.details << JournalDetail.new(:property => 'attr',
160 :prop_key => c,
162 :prop_key => c,
161 :old_value => @issue_before_change.send(c),
163 :old_value => @issue_before_change.send(c),
162 :value => send(c)) unless send(c)==@issue_before_change.send(c)
164 :value => send(c)) unless send(c)==@issue_before_change.send(c)
163 }
165 }
164 # custom fields changes
166 # custom fields changes
165 custom_values.each {|c|
167 custom_values.each {|c|
166 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
168 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
167 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
169 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
168 @current_journal.details << JournalDetail.new(:property => 'cf',
170 @current_journal.details << JournalDetail.new(:property => 'cf',
169 :prop_key => c.custom_field_id,
171 :prop_key => c.custom_field_id,
170 :old_value => @custom_values_before_change[c.custom_field_id],
172 :old_value => @custom_values_before_change[c.custom_field_id],
171 :value => c.value)
173 :value => c.value)
172 }
174 }
173 @current_journal.save
175 @current_journal.save
174 end
176 end
175 # Save the issue even if the journal is not saved (because empty)
177 # Save the issue even if the journal is not saved (because empty)
176 true
178 true
177 end
179 end
178
180
179 def after_save
181 def after_save
180 # Reload is needed in order to get the right status
182 # Reload is needed in order to get the right status
181 reload
183 reload
182
184
183 # Update start/due dates of following issues
185 # Update start/due dates of following issues
184 relations_from.each(&:set_issue_to_dates)
186 relations_from.each(&:set_issue_to_dates)
185
187
186 # Close duplicates if the issue was closed
188 # Close duplicates if the issue was closed
187 if @issue_before_change && !@issue_before_change.closed? && self.closed?
189 if @issue_before_change && !@issue_before_change.closed? && self.closed?
188 duplicates.each do |duplicate|
190 duplicates.each do |duplicate|
189 # Reload is need in case the duplicate was updated by a previous duplicate
191 # Reload is need in case the duplicate was updated by a previous duplicate
190 duplicate.reload
192 duplicate.reload
191 # Don't re-close it if it's already closed
193 # Don't re-close it if it's already closed
192 next if duplicate.closed?
194 next if duplicate.closed?
193 # Same user and notes
195 # Same user and notes
194 duplicate.init_journal(@current_journal.user, @current_journal.notes)
196 duplicate.init_journal(@current_journal.user, @current_journal.notes)
195 duplicate.update_attribute :status, self.status
197 duplicate.update_attribute :status, self.status
196 end
198 end
197 end
199 end
198 end
200 end
199
201
200 def init_journal(user, notes = "")
202 def init_journal(user, notes = "")
201 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
203 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
202 @issue_before_change = self.clone
204 @issue_before_change = self.clone
203 @issue_before_change.status = self.status
205 @issue_before_change.status = self.status
204 @custom_values_before_change = {}
206 @custom_values_before_change = {}
205 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
207 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
206 # Make sure updated_on is updated when adding a note.
208 # Make sure updated_on is updated when adding a note.
207 updated_on_will_change!
209 updated_on_will_change!
208 @current_journal
210 @current_journal
209 end
211 end
210
212
211 # Return true if the issue is closed, otherwise false
213 # Return true if the issue is closed, otherwise false
212 def closed?
214 def closed?
213 self.status.is_closed?
215 self.status.is_closed?
214 end
216 end
215
217
216 # Returns true if the issue is overdue
218 # Returns true if the issue is overdue
217 def overdue?
219 def overdue?
218 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
220 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
219 end
221 end
220
222
221 # Users the issue can be assigned to
223 # Users the issue can be assigned to
222 def assignable_users
224 def assignable_users
223 project.assignable_users
225 project.assignable_users
224 end
226 end
225
227
226 # Returns an array of status that user is able to apply
228 # Returns an array of status that user is able to apply
227 def new_statuses_allowed_to(user)
229 def new_statuses_allowed_to(user)
228 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
230 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
229 statuses << status unless statuses.empty?
231 statuses << status unless statuses.empty?
230 statuses.uniq.sort
232 statuses.uniq.sort
231 end
233 end
232
234
233 # Returns the mail adresses of users that should be notified for the issue
235 # Returns the mail adresses of users that should be notified for the issue
234 def recipients
236 def recipients
235 recipients = project.recipients
237 recipients = project.recipients
236 # Author and assignee are always notified unless they have been locked
238 # Author and assignee are always notified unless they have been locked
237 recipients << author.mail if author && author.active?
239 recipients << author.mail if author && author.active?
238 recipients << assigned_to.mail if assigned_to && assigned_to.active?
240 recipients << assigned_to.mail if assigned_to && assigned_to.active?
239 recipients.compact.uniq
241 recipients.compact.uniq
240 end
242 end
241
243
242 def spent_hours
244 def spent_hours
243 @spent_hours ||= time_entries.sum(:hours) || 0
245 @spent_hours ||= time_entries.sum(:hours) || 0
244 end
246 end
245
247
246 def relations
248 def relations
247 (relations_from + relations_to).sort
249 (relations_from + relations_to).sort
248 end
250 end
249
251
250 def all_dependent_issues
252 def all_dependent_issues
251 dependencies = []
253 dependencies = []
252 relations_from.each do |relation|
254 relations_from.each do |relation|
253 dependencies << relation.issue_to
255 dependencies << relation.issue_to
254 dependencies += relation.issue_to.all_dependent_issues
256 dependencies += relation.issue_to.all_dependent_issues
255 end
257 end
256 dependencies
258 dependencies
257 end
259 end
258
260
259 # Returns an array of issues that duplicate this one
261 # Returns an array of issues that duplicate this one
260 def duplicates
262 def duplicates
261 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
263 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
262 end
264 end
263
265
264 # Returns the due date or the target due date if any
266 # Returns the due date or the target due date if any
265 # Used on gantt chart
267 # Used on gantt chart
266 def due_before
268 def due_before
267 due_date || (fixed_version ? fixed_version.effective_date : nil)
269 due_date || (fixed_version ? fixed_version.effective_date : nil)
268 end
270 end
269
271
270 def duration
272 def duration
271 (start_date && due_date) ? due_date - start_date : 0
273 (start_date && due_date) ? due_date - start_date : 0
272 end
274 end
273
275
274 def soonest_start
276 def soonest_start
275 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
277 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
276 end
278 end
277
279
278 def to_s
280 def to_s
279 "#{tracker} ##{id}: #{subject}"
281 "#{tracker} ##{id}: #{subject}"
280 end
282 end
281
283
282 private
284 private
283
285
284 # Callback on attachment deletion
286 # Callback on attachment deletion
285 def attachment_removed(obj)
287 def attachment_removed(obj)
286 journal = init_journal(User.current)
288 journal = init_journal(User.current)
287 journal.details << JournalDetail.new(:property => 'attachment',
289 journal.details << JournalDetail.new(:property => 'attachment',
288 :prop_key => obj.id,
290 :prop_key => obj.id,
289 :old_value => obj.filename)
291 :old_value => obj.filename)
290 journal.save
292 journal.save
291 end
293 end
292 end
294 end
General Comments 0
You need to be logged in to leave comments. Login now