You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.rb 63KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Issue < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. include Redmine::Utils::DateCalculation
  20. include Redmine::I18n
  21. before_save :set_parent_id
  22. include Redmine::NestedSet::IssueNestedSet
  23. belongs_to :project
  24. belongs_to :tracker
  25. belongs_to :status, :class_name => 'IssueStatus'
  26. belongs_to :author, :class_name => 'User'
  27. belongs_to :assigned_to, :class_name => 'Principal'
  28. belongs_to :fixed_version, :class_name => 'Version'
  29. belongs_to :priority, :class_name => 'IssuePriority'
  30. belongs_to :category, :class_name => 'IssueCategory'
  31. has_many :journals, :as => :journalized, :dependent => :destroy, :inverse_of => :journalized
  32. has_many :time_entries, :dependent => :destroy
  33. has_and_belongs_to_many :changesets, lambda {order("#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC")}
  34. has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
  35. has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
  36. acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
  37. acts_as_customizable
  38. acts_as_watchable
  39. acts_as_searchable :columns => ['subject', "#{table_name}.description"],
  40. :preload => [:project, :status, :tracker],
  41. :scope => lambda {|options| options[:open_issues] ? self.open : self.all}
  42. acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
  43. :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
  44. :type => Proc.new {|o| 'issue' + (o.closed? ? '-closed' : '') }
  45. acts_as_activity_provider :scope => preload(:project, :author, :tracker, :status),
  46. :author_key => :author_id
  47. DONE_RATIO_OPTIONS = %w(issue_field issue_status)
  48. attr_accessor :deleted_attachment_ids
  49. attr_reader :current_journal
  50. delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true
  51. validates_presence_of :subject, :project, :tracker
  52. validates_presence_of :priority, :if => Proc.new {|issue| issue.new_record? || issue.priority_id_changed?}
  53. validates_presence_of :status, :if => Proc.new {|issue| issue.new_record? || issue.status_id_changed?}
  54. validates_presence_of :author, :if => Proc.new {|issue| issue.new_record? || issue.author_id_changed?}
  55. validates_length_of :subject, :maximum => 255
  56. validates_inclusion_of :done_ratio, :in => 0..100
  57. validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid}
  58. validates :start_date, :date => true
  59. validates :due_date, :date => true
  60. validate :validate_issue, :validate_required_fields, :validate_permissions
  61. scope :visible, lambda {|*args|
  62. joins(:project).
  63. where(Issue.visible_condition(args.shift || User.current, *args))
  64. }
  65. scope :open, lambda {|*args|
  66. is_closed = args.size > 0 ? !args.first : false
  67. joins(:status).
  68. where(:issue_statuses => {:is_closed => is_closed})
  69. }
  70. scope :recently_updated, lambda { order(:updated_on => :desc) }
  71. scope :on_active_project, lambda {
  72. joins(:project).
  73. where(:projects => {:status => Project::STATUS_ACTIVE})
  74. }
  75. scope :fixed_version, lambda {|versions|
  76. ids = [versions].flatten.compact.map {|v| v.is_a?(Version) ? v.id : v}
  77. ids.any? ? where(:fixed_version_id => ids) : none
  78. }
  79. scope :assigned_to, lambda {|arg|
  80. arg = Array(arg).uniq
  81. ids = arg.map {|p| p.is_a?(Principal) ? p.id : p}
  82. ids += arg.select {|p| p.is_a?(User)}.map(&:group_ids).flatten.uniq
  83. ids.compact!
  84. ids.any? ? where(:assigned_to_id => ids) : none
  85. }
  86. scope :like, lambda {|q|
  87. q = q.to_s
  88. if q.present?
  89. where("LOWER(#{table_name}.subject) LIKE LOWER(?)", "%#{q}%")
  90. end
  91. }
  92. before_validation :default_assign, on: :create
  93. before_validation :clear_disabled_fields
  94. before_save :close_duplicates, :update_done_ratio_from_issue_status,
  95. :force_updated_on_change, :update_closed_on
  96. after_save {|issue| issue.send :after_project_change if !issue.saved_change_to_id? && issue.saved_change_to_project_id?}
  97. after_save :reschedule_following_issues, :update_nested_set_attributes,
  98. :update_parent_attributes, :delete_selected_attachments, :create_journal
  99. # Should be after_create but would be called before previous after_save callbacks
  100. after_save :after_create_from_copy
  101. after_destroy :update_parent_attributes
  102. after_create_commit :send_notification
  103. # Returns a SQL conditions string used to find all issues visible by the specified user
  104. def self.visible_condition(user, options={})
  105. Project.allowed_to_condition(user, :view_issues, options) do |role, user|
  106. sql = if user.id && user.logged?
  107. case role.issues_visibility
  108. when 'all'
  109. '1=1'
  110. when 'default'
  111. user_ids = [user.id] + user.groups.pluck(:id).compact
  112. "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
  113. when 'own'
  114. user_ids = [user.id] + user.groups.pluck(:id).compact
  115. "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
  116. else
  117. '1=0'
  118. end
  119. else
  120. "(#{table_name}.is_private = #{connection.quoted_false})"
  121. end
  122. unless role.permissions_all_trackers?(:view_issues)
  123. tracker_ids = role.permissions_tracker_ids(:view_issues)
  124. if tracker_ids.any?
  125. sql = "(#{sql} AND #{table_name}.tracker_id IN (#{tracker_ids.join(',')}))"
  126. else
  127. sql = '1=0'
  128. end
  129. end
  130. sql
  131. end
  132. end
  133. # Returns true if usr or current user is allowed to view the issue
  134. def visible?(usr=nil)
  135. (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
  136. visible = if user.logged?
  137. case role.issues_visibility
  138. when 'all'
  139. true
  140. when 'default'
  141. !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to))
  142. when 'own'
  143. self.author == user || user.is_or_belongs_to?(assigned_to)
  144. else
  145. false
  146. end
  147. else
  148. !self.is_private?
  149. end
  150. unless role.permissions_all_trackers?(:view_issues)
  151. visible &&= role.permissions_tracker_ids?(:view_issues, tracker_id)
  152. end
  153. visible
  154. end
  155. end
  156. # Returns true if user or current user is allowed to edit or add notes to the issue
  157. def editable?(user=User.current)
  158. attributes_editable?(user) || notes_addable?(user)
  159. end
  160. # Returns true if user or current user is allowed to edit the issue
  161. def attributes_editable?(user=User.current)
  162. user_tracker_permission?(user, :edit_issues)
  163. end
  164. # Overrides Redmine::Acts::Attachable::InstanceMethods#attachments_editable?
  165. def attachments_editable?(user=User.current)
  166. attributes_editable?(user)
  167. end
  168. # Returns true if user or current user is allowed to add notes to the issue
  169. def notes_addable?(user=User.current)
  170. user_tracker_permission?(user, :add_issue_notes)
  171. end
  172. # Returns true if user or current user is allowed to delete the issue
  173. def deletable?(user=User.current)
  174. user_tracker_permission?(user, :delete_issues)
  175. end
  176. def initialize(attributes=nil, *args)
  177. super
  178. if new_record?
  179. # set default values for new records only
  180. self.priority ||= IssuePriority.default
  181. self.watcher_user_ids = []
  182. end
  183. end
  184. def create_or_update(*args)
  185. super
  186. ensure
  187. @status_was = nil
  188. end
  189. private :create_or_update
  190. # AR#Persistence#destroy would raise and RecordNotFound exception
  191. # if the issue was already deleted or updated (non matching lock_version).
  192. # This is a problem when bulk deleting issues or deleting a project
  193. # (because an issue may already be deleted if its parent was deleted
  194. # first).
  195. # The issue is reloaded by the nested_set before being deleted so
  196. # the lock_version condition should not be an issue but we handle it.
  197. def destroy
  198. super
  199. rescue ActiveRecord::StaleObjectError, ActiveRecord::RecordNotFound
  200. # Stale or already deleted
  201. begin
  202. reload
  203. rescue ActiveRecord::RecordNotFound
  204. # The issue was actually already deleted
  205. @destroyed = true
  206. return freeze
  207. end
  208. # The issue was stale, retry to destroy
  209. super
  210. end
  211. alias :base_reload :reload
  212. def reload(*args)
  213. @workflow_rule_by_attribute = nil
  214. @assignable_versions = nil
  215. @relations = nil
  216. @spent_hours = nil
  217. @total_spent_hours = nil
  218. @total_estimated_hours = nil
  219. @last_updated_by = nil
  220. @last_notes = nil
  221. base_reload(*args)
  222. end
  223. # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
  224. def available_custom_fields
  225. (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields) : []
  226. end
  227. def visible_custom_field_values(user=nil)
  228. user_real = user || User.current
  229. custom_field_values.select do |value|
  230. value.custom_field.visible_by?(project, user_real)
  231. end
  232. end
  233. # Overrides Redmine::Acts::Customizable::InstanceMethods#set_custom_field_default?
  234. def set_custom_field_default?(custom_value)
  235. new_record? || project_id_changed?|| tracker_id_changed?
  236. end
  237. # Copies attributes from another issue, arg can be an id or an Issue
  238. def copy_from(arg, options={})
  239. issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
  240. self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on", "status_id", "closed_on")
  241. self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
  242. if options[:keep_status]
  243. self.status = issue.status
  244. end
  245. self.author = User.current
  246. unless options[:attachments] == false
  247. self.attachments = issue.attachments.map do |attachement|
  248. attachement.copy(:container => self)
  249. end
  250. end
  251. unless options[:watchers] == false
  252. self.watcher_user_ids =
  253. issue.watcher_users.select{|u| u.status == User::STATUS_ACTIVE}.map(&:id)
  254. end
  255. @copied_from = issue
  256. @copy_options = options
  257. self
  258. end
  259. # Returns an unsaved copy of the issue
  260. def copy(attributes=nil, copy_options={})
  261. copy = self.class.new.copy_from(self, copy_options)
  262. copy.attributes = attributes if attributes
  263. copy
  264. end
  265. # Returns true if the issue is a copy
  266. def copy?
  267. @copied_from.present?
  268. end
  269. def status_id=(status_id)
  270. if status_id.to_s != self.status_id.to_s
  271. self.status = (status_id.present? ? IssueStatus.find_by_id(status_id) : nil)
  272. end
  273. self.status_id
  274. end
  275. # Sets the status.
  276. def status=(status)
  277. if status != self.status
  278. @workflow_rule_by_attribute = nil
  279. end
  280. association(:status).writer(status)
  281. end
  282. def priority_id=(pid)
  283. self.priority = nil
  284. write_attribute(:priority_id, pid)
  285. end
  286. def category_id=(cid)
  287. self.category = nil
  288. write_attribute(:category_id, cid)
  289. end
  290. def fixed_version_id=(vid)
  291. self.fixed_version = nil
  292. write_attribute(:fixed_version_id, vid)
  293. end
  294. def tracker_id=(tracker_id)
  295. if tracker_id.to_s != self.tracker_id.to_s
  296. self.tracker = (tracker_id.present? ? Tracker.find_by_id(tracker_id) : nil)
  297. end
  298. self.tracker_id
  299. end
  300. # Sets the tracker.
  301. # This will set the status to the default status of the new tracker if:
  302. # * the status was the default for the previous tracker
  303. # * or if the status was not part of the new tracker statuses
  304. # * or the status was nil
  305. def tracker=(tracker)
  306. tracker_was = self.tracker
  307. association(:tracker).writer(tracker)
  308. if tracker != tracker_was
  309. if status == tracker_was.try(:default_status)
  310. self.status = nil
  311. elsif status && tracker && !tracker.issue_status_ids.include?(status.id)
  312. self.status = nil
  313. end
  314. reassign_custom_field_values
  315. @workflow_rule_by_attribute = nil
  316. end
  317. self.status ||= default_status
  318. self.tracker
  319. end
  320. def project_id=(project_id)
  321. if project_id.to_s != self.project_id.to_s
  322. self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
  323. end
  324. self.project_id
  325. end
  326. # Sets the project.
  327. # Unless keep_tracker argument is set to true, this will change the tracker
  328. # to the first tracker of the new project if the previous tracker is not part
  329. # of the new project trackers.
  330. # This will:
  331. # * clear the fixed_version is it's no longer valid for the new project.
  332. # * clear the parent issue if it's no longer valid for the new project.
  333. # * set the category to the category with the same name in the new
  334. # project if it exists, or clear it if it doesn't.
  335. # * for new issue, set the fixed_version to the project default version
  336. # if it's a valid fixed_version.
  337. def project=(project, keep_tracker=false)
  338. project_was = self.project
  339. association(:project).writer(project)
  340. if project != project_was
  341. @safe_attribute_names = nil
  342. end
  343. if project_was && project && project_was != project
  344. @assignable_versions = nil
  345. unless keep_tracker || project.trackers.include?(tracker)
  346. self.tracker = project.trackers.first
  347. end
  348. # Reassign to the category with same name if any
  349. if category
  350. self.category = project.issue_categories.find_by_name(category.name)
  351. end
  352. # Clear the assignee if not available in the new project for new issues (eg. copy)
  353. # For existing issue, the previous assignee is still valid, so we keep it
  354. if new_record? && assigned_to && !assignable_users.include?(assigned_to)
  355. self.assigned_to_id = nil
  356. end
  357. # Keep the fixed_version if it's still valid in the new_project
  358. if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
  359. self.fixed_version = nil
  360. end
  361. # Clear the parent task if it's no longer valid
  362. unless valid_parent_project?
  363. self.parent_issue_id = nil
  364. end
  365. reassign_custom_field_values
  366. @workflow_rule_by_attribute = nil
  367. end
  368. # Set fixed_version to the project default version if it's valid
  369. if new_record? && fixed_version.nil? && project && project.default_version_id?
  370. if project.shared_versions.open.exists?(project.default_version_id)
  371. self.fixed_version_id = project.default_version_id
  372. end
  373. end
  374. self.project
  375. end
  376. def description=(arg)
  377. if arg.is_a?(String)
  378. arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
  379. end
  380. write_attribute(:description, arg)
  381. end
  382. def deleted_attachment_ids
  383. Array(@deleted_attachment_ids).map(&:to_i)
  384. end
  385. # Overrides assign_attributes so that project and tracker get assigned first
  386. def assign_attributes(new_attributes, *args)
  387. return if new_attributes.nil?
  388. attrs = new_attributes.dup
  389. attrs.stringify_keys!
  390. %w(project project_id tracker tracker_id).each do |attr|
  391. if attrs.has_key?(attr)
  392. send "#{attr}=", attrs.delete(attr)
  393. end
  394. end
  395. super attrs, *args
  396. end
  397. def attributes=(new_attributes)
  398. assign_attributes new_attributes
  399. end
  400. def estimated_hours=(h)
  401. write_attribute :estimated_hours, (h.is_a?(String) ? (h.to_hours || h) : h)
  402. end
  403. safe_attributes 'project_id',
  404. 'tracker_id',
  405. 'status_id',
  406. 'category_id',
  407. 'assigned_to_id',
  408. 'priority_id',
  409. 'fixed_version_id',
  410. 'subject',
  411. 'description',
  412. 'start_date',
  413. 'due_date',
  414. 'done_ratio',
  415. 'estimated_hours',
  416. 'custom_field_values',
  417. 'custom_fields',
  418. 'lock_version',
  419. 'notes',
  420. :if => lambda {|issue, user| issue.new_record? || issue.attributes_editable?(user) }
  421. safe_attributes 'notes',
  422. :if => lambda {|issue, user| issue.notes_addable?(user)}
  423. safe_attributes 'private_notes',
  424. :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)}
  425. safe_attributes 'watcher_user_ids',
  426. :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
  427. safe_attributes 'is_private',
  428. :if => lambda {|issue, user|
  429. user.allowed_to?(:set_issues_private, issue.project) ||
  430. (issue.author_id == user.id && user.allowed_to?(:set_own_issues_private, issue.project))
  431. }
  432. safe_attributes 'parent_issue_id',
  433. :if => lambda {|issue, user| (issue.new_record? || issue.attributes_editable?(user)) &&
  434. user.allowed_to?(:manage_subtasks, issue.project)}
  435. safe_attributes 'deleted_attachment_ids',
  436. :if => lambda {|issue, user| issue.attachments_deletable?(user)}
  437. def safe_attribute_names(user=nil)
  438. names = super
  439. names -= disabled_core_fields
  440. names -= read_only_attribute_names(user)
  441. if new_record?
  442. # Make sure that project_id can always be set for new issues
  443. names |= %w(project_id)
  444. end
  445. if dates_derived?
  446. names -= %w(start_date due_date)
  447. end
  448. if priority_derived?
  449. names -= %w(priority_id)
  450. end
  451. if done_ratio_derived?
  452. names -= %w(done_ratio)
  453. end
  454. names
  455. end
  456. # Safely sets attributes
  457. # Should be called from controllers instead of #attributes=
  458. # attr_accessible is too rough because we still want things like
  459. # Issue.new(:project => foo) to work
  460. def safe_attributes=(attrs, user=User.current)
  461. if attrs.respond_to?(:to_unsafe_hash)
  462. attrs = attrs.to_unsafe_hash
  463. end
  464. @attributes_set_by = user
  465. return unless attrs.is_a?(Hash)
  466. attrs = attrs.deep_dup
  467. # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
  468. if (p = attrs.delete('project_id')) && safe_attribute?('project_id')
  469. if p.is_a?(String) && !p.match(/^\d*$/)
  470. p_id = Project.find_by_identifier(p).try(:id)
  471. else
  472. p_id = p.to_i
  473. end
  474. if allowed_target_projects(user).where(:id => p_id).exists?
  475. self.project_id = p_id
  476. end
  477. if project_id_changed? && attrs['category_id'].present? && attrs['category_id'].to_s == category_id_was.to_s
  478. # Discard submitted category on previous project
  479. attrs.delete('category_id')
  480. end
  481. end
  482. if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
  483. if allowed_target_trackers(user).where(:id => t.to_i).exists?
  484. self.tracker_id = t
  485. end
  486. end
  487. if project && tracker.nil?
  488. # Set a default tracker to accept custom field values
  489. # even if tracker is not specified
  490. allowed_trackers = allowed_target_trackers(user)
  491. if attrs['parent_issue_id'].present?
  492. # If parent_issue_id is present, the first tracker for which this field
  493. # is not disabled is chosen as default
  494. self.tracker = allowed_trackers.detect {|t| t.core_fields.include?('parent_issue_id')}
  495. end
  496. self.tracker ||= allowed_trackers.first
  497. end
  498. statuses_allowed = new_statuses_allowed_to(user)
  499. if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
  500. if statuses_allowed.collect(&:id).include?(s.to_i)
  501. self.status_id = s
  502. end
  503. end
  504. if new_record? && !statuses_allowed.include?(status)
  505. self.status = statuses_allowed.first || default_status
  506. end
  507. if (u = attrs.delete('assigned_to_id')) && safe_attribute?('assigned_to_id')
  508. self.assigned_to_id = u
  509. end
  510. attrs = delete_unsafe_attributes(attrs, user)
  511. return if attrs.empty?
  512. if attrs['parent_issue_id'].present?
  513. s = attrs['parent_issue_id'].to_s
  514. unless (m = s.match(%r{\A#?(\d+)\z})) && (m[1] == parent_id.to_s || Issue.visible(user).exists?(m[1]))
  515. @invalid_parent_issue_id = attrs.delete('parent_issue_id')
  516. end
  517. end
  518. if attrs['custom_field_values'].present?
  519. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  520. attrs['custom_field_values'].select! {|k, v| editable_custom_field_ids.include?(k.to_s)}
  521. end
  522. if attrs['custom_fields'].present?
  523. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  524. attrs['custom_fields'].select! {|c| editable_custom_field_ids.include?(c['id'].to_s)}
  525. end
  526. assign_attributes attrs
  527. end
  528. def disabled_core_fields
  529. tracker ? tracker.disabled_core_fields : []
  530. end
  531. # Returns the custom_field_values that can be edited by the given user
  532. def editable_custom_field_values(user=nil)
  533. read_only = read_only_attribute_names(user)
  534. visible_custom_field_values(user).reject do |value|
  535. read_only.include?(value.custom_field_id.to_s)
  536. end
  537. end
  538. # Returns the custom fields that can be edited by the given user
  539. def editable_custom_fields(user=nil)
  540. editable_custom_field_values(user).map(&:custom_field).uniq
  541. end
  542. # Returns the names of attributes that are read-only for user or the current user
  543. # For users with multiple roles, the read-only fields are the intersection of
  544. # read-only fields of each role
  545. # The result is an array of strings where sustom fields are represented with their ids
  546. #
  547. # Examples:
  548. # issue.read_only_attribute_names # => ['due_date', '2']
  549. # issue.read_only_attribute_names(user) # => []
  550. def read_only_attribute_names(user=nil)
  551. workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys
  552. end
  553. # Returns the names of required attributes for user or the current user
  554. # For users with multiple roles, the required fields are the intersection of
  555. # required fields of each role
  556. # The result is an array of strings where sustom fields are represented with their ids
  557. #
  558. # Examples:
  559. # issue.required_attribute_names # => ['due_date', '2']
  560. # issue.required_attribute_names(user) # => []
  561. def required_attribute_names(user=nil)
  562. workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys
  563. end
  564. # Returns true if the attribute is required for user
  565. def required_attribute?(name, user=nil)
  566. required_attribute_names(user).include?(name.to_s)
  567. end
  568. # Returns a hash of the workflow rule by attribute for the given user
  569. #
  570. # Examples:
  571. # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'}
  572. def workflow_rule_by_attribute(user=nil)
  573. return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil?
  574. user_real = user || User.current
  575. roles = user_real.admin ? Role.all.to_a : user_real.roles_for_project(project)
  576. roles = roles.select(&:consider_workflow?)
  577. return {} if roles.empty?
  578. result = {}
  579. workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).to_a
  580. if workflow_permissions.any?
  581. workflow_rules = workflow_permissions.inject({}) do |h, wp|
  582. h[wp.field_name] ||= {}
  583. h[wp.field_name][wp.role_id] = wp.rule
  584. h
  585. end
  586. fields_with_roles = {}
  587. IssueCustomField.where(:visible => false).joins(:roles).pluck(:id, "role_id").each do |field_id, role_id|
  588. fields_with_roles[field_id] ||= []
  589. fields_with_roles[field_id] << role_id
  590. end
  591. roles.each do |role|
  592. fields_with_roles.each do |field_id, role_ids|
  593. unless role_ids.include?(role.id)
  594. field_name = field_id.to_s
  595. workflow_rules[field_name] ||= {}
  596. workflow_rules[field_name][role.id] = 'readonly'
  597. end
  598. end
  599. end
  600. workflow_rules.each do |attr, rules|
  601. next if rules.size < roles.size
  602. uniq_rules = rules.values.uniq
  603. if uniq_rules.size == 1
  604. result[attr] = uniq_rules.first
  605. else
  606. result[attr] = 'required'
  607. end
  608. end
  609. end
  610. @workflow_rule_by_attribute = result if user.nil?
  611. result
  612. end
  613. private :workflow_rule_by_attribute
  614. def done_ratio
  615. if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
  616. status.default_done_ratio
  617. else
  618. read_attribute(:done_ratio)
  619. end
  620. end
  621. def self.use_status_for_done_ratio?
  622. Setting.issue_done_ratio == 'issue_status'
  623. end
  624. def self.use_field_for_done_ratio?
  625. Setting.issue_done_ratio == 'issue_field'
  626. end
  627. def validate_issue
  628. if due_date && start_date && (start_date_changed? || due_date_changed?) && due_date < start_date
  629. errors.add :due_date, :greater_than_start_date
  630. end
  631. if start_date && start_date_changed? && soonest_start && start_date < soonest_start
  632. errors.add :start_date, :earlier_than_minimum_start_date, :date => format_date(soonest_start)
  633. end
  634. if fixed_version
  635. if !assignable_versions.include?(fixed_version)
  636. errors.add :fixed_version_id, :inclusion
  637. elsif reopening? && fixed_version.closed?
  638. errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
  639. end
  640. end
  641. # Checks that the issue can not be added/moved to a disabled tracker
  642. if project && (tracker_id_changed? || project_id_changed?)
  643. if tracker && !project.trackers.include?(tracker)
  644. errors.add :tracker_id, :inclusion
  645. end
  646. end
  647. if assigned_to_id_changed? && assigned_to_id.present?
  648. unless assignable_users.include?(assigned_to)
  649. errors.add :assigned_to_id, :invalid
  650. end
  651. end
  652. # Checks parent issue assignment
  653. if @invalid_parent_issue_id.present?
  654. errors.add :parent_issue_id, :invalid
  655. elsif @parent_issue
  656. if !valid_parent_project?(@parent_issue)
  657. errors.add :parent_issue_id, :invalid
  658. elsif (@parent_issue != parent) && (
  659. self.would_reschedule?(@parent_issue) ||
  660. @parent_issue.self_and_ancestors.any? {|a| a.relations_from.any? {|r| r.relation_type == IssueRelation::TYPE_PRECEDES && r.issue_to.would_reschedule?(self)}}
  661. )
  662. errors.add :parent_issue_id, :invalid
  663. elsif !closed? && @parent_issue.closed?
  664. # cannot attach an open issue to a closed parent
  665. errors.add :base, :open_issue_with_closed_parent
  666. elsif !new_record?
  667. # moving an existing issue
  668. if move_possible?(@parent_issue)
  669. # move accepted
  670. else
  671. errors.add :parent_issue_id, :invalid
  672. end
  673. end
  674. end
  675. end
  676. # Validates the issue against additional workflow requirements
  677. def validate_required_fields
  678. user = new_record? ? author : current_journal.try(:user)
  679. required_attribute_names(user).each do |attribute|
  680. if attribute =~ /^\d+$/
  681. attribute = attribute.to_i
  682. v = custom_field_values.detect {|v| v.custom_field_id == attribute }
  683. if v && Array(v.value).detect(&:present?).nil?
  684. errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank')
  685. end
  686. else
  687. if respond_to?(attribute) && send(attribute).blank? && !disabled_core_fields.include?(attribute)
  688. next if attribute == 'category_id' && project.try(:issue_categories).blank?
  689. next if attribute == 'fixed_version_id' && assignable_versions.blank?
  690. errors.add attribute, :blank
  691. end
  692. end
  693. end
  694. end
  695. def validate_permissions
  696. if @attributes_set_by && new_record? && copy?
  697. unless allowed_target_trackers(@attributes_set_by).include?(tracker)
  698. errors.add :tracker, :invalid
  699. end
  700. end
  701. end
  702. # Overrides Redmine::Acts::Customizable::InstanceMethods#validate_custom_field_values
  703. # so that custom values that are not editable are not validated (eg. a custom field that
  704. # is marked as required should not trigger a validation error if the user is not allowed
  705. # to edit this field).
  706. def validate_custom_field_values
  707. user = new_record? ? author : current_journal.try(:user)
  708. if new_record? || custom_field_values_changed?
  709. editable_custom_field_values(user).each(&:validate_value)
  710. end
  711. end
  712. # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
  713. # even if the user turns off the setting later
  714. def update_done_ratio_from_issue_status
  715. if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
  716. self.done_ratio = status.default_done_ratio
  717. end
  718. end
  719. def init_journal(user, notes = "")
  720. @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
  721. end
  722. # Returns the current journal or nil if it's not initialized
  723. def current_journal
  724. @current_journal
  725. end
  726. # Clears the current journal
  727. def clear_journal
  728. @current_journal = nil
  729. end
  730. # Returns the names of attributes that are journalized when updating the issue
  731. def journalized_attribute_names
  732. names = Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on closed_on)
  733. if tracker
  734. names -= tracker.disabled_core_fields
  735. end
  736. names
  737. end
  738. # Returns the id of the last journal or nil
  739. def last_journal_id
  740. if new_record?
  741. nil
  742. else
  743. journals.maximum(:id)
  744. end
  745. end
  746. # Returns a scope for journals that have an id greater than journal_id
  747. def journals_after(journal_id)
  748. scope = journals.reorder("#{Journal.table_name}.id ASC")
  749. if journal_id.present?
  750. scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i)
  751. end
  752. scope
  753. end
  754. # Returns the journals that are visible to user with their index
  755. # Used to display the issue history
  756. def visible_journals_with_index(user=User.current)
  757. result = journals.
  758. preload(:details).
  759. preload(:user => :email_address).
  760. reorder(:created_on, :id).to_a
  761. result.each_with_index {|j,i| j.indice = i+1}
  762. unless user.allowed_to?(:view_private_notes, project)
  763. result.select! do |journal|
  764. !journal.private_notes? || journal.user == user
  765. end
  766. end
  767. Journal.preload_journals_details_custom_fields(result)
  768. result.select! {|journal| journal.notes? || journal.visible_details.any?}
  769. result
  770. end
  771. # Returns the initial status of the issue
  772. # Returns nil for a new issue
  773. def status_was
  774. if status_id_changed?
  775. if status_id_was.to_i > 0
  776. @status_was ||= IssueStatus.find_by_id(status_id_was)
  777. end
  778. else
  779. @status_was ||= status
  780. end
  781. end
  782. # Return true if the issue is closed, otherwise false
  783. def closed?
  784. status.present? && status.is_closed?
  785. end
  786. # Returns true if the issue was closed when loaded
  787. def was_closed?
  788. status_was.present? && status_was.is_closed?
  789. end
  790. # Return true if the issue is being reopened
  791. def reopening?
  792. if new_record?
  793. false
  794. else
  795. status_id_changed? && !closed? && was_closed?
  796. end
  797. end
  798. alias :reopened? :reopening?
  799. # Return true if the issue is being closed
  800. def closing?
  801. if new_record?
  802. closed?
  803. else
  804. status_id_changed? && closed? && !was_closed?
  805. end
  806. end
  807. # Returns true if the issue is overdue
  808. def overdue?
  809. due_date.present? && (due_date < User.current.today) && !closed?
  810. end
  811. # Is the amount of work done less than it should for the due date
  812. def behind_schedule?
  813. return false if start_date.nil? || due_date.nil?
  814. done_date = start_date + ((due_date - start_date + 1) * done_ratio / 100).floor
  815. return done_date <= User.current.today
  816. end
  817. # Does this issue have children?
  818. def children?
  819. !leaf?
  820. end
  821. # Users the issue can be assigned to
  822. def assignable_users
  823. users = project.assignable_users(tracker).to_a
  824. users << author if author && author.active?
  825. if assigned_to_id_was.present? && assignee = Principal.find_by_id(assigned_to_id_was)
  826. users << assignee
  827. end
  828. users.uniq.sort
  829. end
  830. # Versions that the issue can be assigned to
  831. def assignable_versions
  832. return @assignable_versions if @assignable_versions
  833. versions = project.shared_versions.open.to_a
  834. if fixed_version
  835. if fixed_version_id_changed?
  836. # nothing to do
  837. elsif project_id_changed?
  838. if project.shared_versions.include?(fixed_version)
  839. versions << fixed_version
  840. end
  841. else
  842. versions << fixed_version
  843. end
  844. end
  845. @assignable_versions = versions.uniq.sort
  846. end
  847. # Returns true if this issue is blocked by another issue that is still open
  848. def blocked?
  849. !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
  850. end
  851. # Returns the default status of the issue based on its tracker
  852. # Returns nil if tracker is nil
  853. def default_status
  854. tracker.try(:default_status)
  855. end
  856. # Returns an array of statuses that user is able to apply
  857. def new_statuses_allowed_to(user=User.current, include_default=false)
  858. initial_status = nil
  859. if new_record?
  860. # nop
  861. elsif tracker_id_changed?
  862. if Tracker.where(:id => tracker_id_was, :default_status_id => status_id_was).any?
  863. initial_status = default_status
  864. elsif tracker.issue_status_ids.include?(status_id_was)
  865. initial_status = IssueStatus.find_by_id(status_id_was)
  866. else
  867. initial_status = default_status
  868. end
  869. else
  870. initial_status = status_was
  871. end
  872. initial_assigned_to_id = assigned_to_id_changed? ? assigned_to_id_was : assigned_to_id
  873. assignee_transitions_allowed = initial_assigned_to_id.present? &&
  874. (user.id == initial_assigned_to_id || user.group_ids.include?(initial_assigned_to_id))
  875. statuses = []
  876. statuses += IssueStatus.new_statuses_allowed(
  877. initial_status,
  878. user.admin ? Role.all.to_a : user.roles_for_project(project),
  879. tracker,
  880. author == user,
  881. assignee_transitions_allowed
  882. )
  883. statuses << initial_status unless statuses.empty?
  884. statuses << default_status if include_default || (new_record? && statuses.empty?)
  885. statuses = statuses.compact.uniq.sort
  886. if blocked? || descendants.open.any?
  887. # cannot close a blocked issue or a parent with open subtasks
  888. statuses.reject!(&:is_closed?)
  889. end
  890. if ancestors.open(false).any?
  891. # cannot reopen a subtask of a closed parent
  892. statuses.select!(&:is_closed?)
  893. end
  894. statuses
  895. end
  896. # Returns the original tracker
  897. def tracker_was
  898. Tracker.find_by_id(tracker_id_in_database)
  899. end
  900. # Returns the previous assignee whenever we're before the save
  901. # or in after_* callbacks
  902. def previous_assignee
  903. if previous_assigned_to_id = assigned_to_id_change_to_be_saved.nil? ? assigned_to_id_before_last_save : assigned_to_id_in_database
  904. Principal.find_by_id(previous_assigned_to_id)
  905. end
  906. end
  907. # Returns the users that should be notified
  908. def notified_users
  909. # Author and assignee are always notified unless they have been
  910. # locked or don't want to be notified
  911. notified = [author, assigned_to, previous_assignee].compact.uniq
  912. notified = notified.map {|n| n.is_a?(Group) ? n.users : n}.flatten
  913. notified.uniq!
  914. notified = notified.select {|u| u.active? && u.notify_about?(self)}
  915. notified += project.notified_users
  916. notified.uniq!
  917. # Remove users that can not view the issue
  918. notified.reject! {|user| !visible?(user)}
  919. notified
  920. end
  921. # Returns the email addresses that should be notified
  922. def recipients
  923. notified_users.collect(&:mail)
  924. end
  925. def notify?
  926. @notify != false
  927. end
  928. def notify=(arg)
  929. @notify = arg
  930. end
  931. # Returns the number of hours spent on this issue
  932. def spent_hours
  933. @spent_hours ||= time_entries.sum(:hours) || 0.0
  934. end
  935. # Returns the total number of hours spent on this issue and its descendants
  936. def total_spent_hours
  937. @total_spent_hours ||= if leaf?
  938. spent_hours
  939. else
  940. self_and_descendants.joins(:time_entries).sum("#{TimeEntry.table_name}.hours").to_f || 0.0
  941. end
  942. end
  943. def total_estimated_hours
  944. if leaf?
  945. estimated_hours
  946. else
  947. @total_estimated_hours ||= self_and_descendants.sum(:estimated_hours)
  948. end
  949. end
  950. def relations
  951. @relations ||= IssueRelation::Relations.new(self, (relations_from + relations_to).sort)
  952. end
  953. def last_updated_by
  954. if @last_updated_by
  955. @last_updated_by.presence
  956. else
  957. journals.reorder(:id => :desc).first.try(:user)
  958. end
  959. end
  960. def last_notes
  961. if @last_notes
  962. @last_notes
  963. else
  964. journals.where.not(notes: '').reorder(:id => :desc).first.try(:notes)
  965. end
  966. end
  967. # Preloads relations for a collection of issues
  968. def self.load_relations(issues)
  969. if issues.any?
  970. relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all
  971. issues.each do |issue|
  972. issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
  973. end
  974. end
  975. end
  976. # Preloads visible spent time for a collection of issues
  977. def self.load_visible_spent_hours(issues, user=User.current)
  978. if issues.any?
  979. hours_by_issue_id = TimeEntry.visible(user).where(:issue_id => issues.map(&:id)).group(:issue_id).sum(:hours)
  980. issues.each do |issue|
  981. issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0.0)
  982. end
  983. end
  984. end
  985. # Preloads visible total spent time for a collection of issues
  986. def self.load_visible_total_spent_hours(issues, user=User.current)
  987. if issues.any?
  988. hours_by_issue_id = TimeEntry.visible(user).joins(:issue).
  989. joins("JOIN #{Issue.table_name} parent ON parent.root_id = #{Issue.table_name}.root_id" +
  990. " AND parent.lft <= #{Issue.table_name}.lft AND parent.rgt >= #{Issue.table_name}.rgt").
  991. where("parent.id IN (?)", issues.map(&:id)).group("parent.id").sum(:hours)
  992. issues.each do |issue|
  993. issue.instance_variable_set "@total_spent_hours", (hours_by_issue_id[issue.id] || 0.0)
  994. end
  995. end
  996. end
  997. # Preloads visible relations for a collection of issues
  998. def self.load_visible_relations(issues, user=User.current)
  999. if issues.any?
  1000. issue_ids = issues.map(&:id)
  1001. # Relations with issue_from in given issues and visible issue_to
  1002. relations_from = IssueRelation.joins(:issue_to => :project).
  1003. where(visible_condition(user)).where(:issue_from_id => issue_ids).to_a
  1004. # Relations with issue_to in given issues and visible issue_from
  1005. relations_to = IssueRelation.joins(:issue_from => :project).
  1006. where(visible_condition(user)).
  1007. where(:issue_to_id => issue_ids).to_a
  1008. issues.each do |issue|
  1009. relations =
  1010. relations_from.select {|relation| relation.issue_from_id == issue.id} +
  1011. relations_to.select {|relation| relation.issue_to_id == issue.id}
  1012. issue.instance_variable_set "@relations", IssueRelation::Relations.new(issue, relations.sort)
  1013. end
  1014. end
  1015. end
  1016. # Returns a scope of the given issues and their descendants
  1017. def self.self_and_descendants(issues)
  1018. Issue.joins("JOIN #{Issue.table_name} ancestors" +
  1019. " ON ancestors.root_id = #{Issue.table_name}.root_id" +
  1020. " AND ancestors.lft <= #{Issue.table_name}.lft AND ancestors.rgt >= #{Issue.table_name}.rgt"
  1021. ).
  1022. where(:ancestors => {:id => issues.map(&:id)})
  1023. end
  1024. # Preloads users who updated last a collection of issues
  1025. def self.load_visible_last_updated_by(issues, user=User.current)
  1026. if issues.any?
  1027. issue_ids = issues.map(&:id)
  1028. journal_ids = Journal.joins(issue: :project).
  1029. where(:journalized_type => 'Issue', :journalized_id => issue_ids).
  1030. where(Journal.visible_notes_condition(user, :skip_pre_condition => true)).
  1031. group(:journalized_id).
  1032. maximum(:id).
  1033. values
  1034. journals = Journal.where(:id => journal_ids).preload(:user).to_a
  1035. issues.each do |issue|
  1036. journal = journals.detect {|j| j.journalized_id == issue.id}
  1037. issue.instance_variable_set("@last_updated_by", journal.try(:user) || '')
  1038. end
  1039. end
  1040. end
  1041. # Preloads visible last notes for a collection of issues
  1042. def self.load_visible_last_notes(issues, user=User.current)
  1043. if issues.any?
  1044. issue_ids = issues.map(&:id)
  1045. journal_ids = Journal.joins(issue: :project).
  1046. where(:journalized_type => 'Issue', :journalized_id => issue_ids).
  1047. where(Journal.visible_notes_condition(user, :skip_pre_condition => true)).
  1048. where.not(notes: '').
  1049. group(:journalized_id).
  1050. maximum(:id).
  1051. values
  1052. journals = Journal.where(:id => journal_ids).to_a
  1053. issues.each do |issue|
  1054. journal = journals.detect {|j| j.journalized_id == issue.id}
  1055. issue.instance_variable_set("@last_notes", journal.try(:notes) || '')
  1056. end
  1057. end
  1058. end
  1059. # Finds an issue relation given its id.
  1060. def find_relation(relation_id)
  1061. IssueRelation.where("issue_to_id = ? OR issue_from_id = ?", id, id).find(relation_id)
  1062. end
  1063. # Returns true if this issue blocks the other issue, otherwise returns false
  1064. def blocks?(other)
  1065. all = [self]
  1066. last = [self]
  1067. while last.any?
  1068. current = last.map {|i| i.relations_from.where(:relation_type => IssueRelation::TYPE_BLOCKS).map(&:issue_to)}.flatten.uniq
  1069. current -= last
  1070. current -= all
  1071. return true if current.include?(other)
  1072. last = current
  1073. all += last
  1074. end
  1075. false
  1076. end
  1077. # Returns true if the other issue might be rescheduled if the start/due dates of this issue change
  1078. def would_reschedule?(other)
  1079. all = [self]
  1080. last = [self]
  1081. while last.any?
  1082. current = last.map {|i|
  1083. i.relations_from.where(:relation_type => IssueRelation::TYPE_PRECEDES).map(&:issue_to) +
  1084. i.leaves.to_a +
  1085. i.ancestors.map {|a| a.relations_from.where(:relation_type => IssueRelation::TYPE_PRECEDES).map(&:issue_to)}
  1086. }.flatten.uniq
  1087. current -= last
  1088. current -= all
  1089. return true if current.include?(other)
  1090. last = current
  1091. all += last
  1092. end
  1093. false
  1094. end
  1095. # Returns an array of issues that duplicate this one
  1096. def duplicates
  1097. relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
  1098. end
  1099. # Returns the due date or the target due date if any
  1100. # Used on gantt chart
  1101. def due_before
  1102. due_date || (fixed_version ? fixed_version.effective_date : nil)
  1103. end
  1104. # Returns the time scheduled for this issue.
  1105. #
  1106. # Example:
  1107. # Start Date: 2/26/09, End Date: 3/04/09
  1108. # duration => 6
  1109. def duration
  1110. (start_date && due_date) ? due_date - start_date : 0
  1111. end
  1112. # Returns the duration in working days
  1113. def working_duration
  1114. (start_date && due_date) ? working_days(start_date, due_date) : 0
  1115. end
  1116. def soonest_start(reload=false)
  1117. if @soonest_start.nil? || reload
  1118. relations_to.reload if reload
  1119. dates = relations_to.collect{|relation| relation.successor_soonest_start}
  1120. p = @parent_issue || parent
  1121. if p && Setting.parent_issue_dates == 'derived'
  1122. dates << p.soonest_start
  1123. end
  1124. @soonest_start = dates.compact.max
  1125. end
  1126. @soonest_start
  1127. end
  1128. # Sets start_date on the given date or the next working day
  1129. # and changes due_date to keep the same working duration.
  1130. def reschedule_on(date)
  1131. wd = working_duration
  1132. date = next_working_date(date)
  1133. self.start_date = date
  1134. self.due_date = add_working_days(date, wd)
  1135. end
  1136. # Reschedules the issue on the given date or the next working day and saves the record.
  1137. # If the issue is a parent task, this is done by rescheduling its subtasks.
  1138. def reschedule_on!(date, journal=nil)
  1139. return if date.nil?
  1140. if leaf? || !dates_derived?
  1141. if start_date.nil? || start_date != date
  1142. if start_date && start_date > date
  1143. # Issue can not be moved earlier than its soonest start date
  1144. date = [soonest_start(true), date].compact.max
  1145. end
  1146. if journal
  1147. init_journal(journal.user)
  1148. end
  1149. reschedule_on(date)
  1150. begin
  1151. save
  1152. rescue ActiveRecord::StaleObjectError
  1153. reload
  1154. reschedule_on(date)
  1155. save
  1156. end
  1157. end
  1158. else
  1159. leaves.each do |leaf|
  1160. if leaf.start_date
  1161. # Only move subtask if it starts at the same date as the parent
  1162. # or if it starts before the given date
  1163. if start_date == leaf.start_date || date > leaf.start_date
  1164. leaf.reschedule_on!(date)
  1165. end
  1166. else
  1167. leaf.reschedule_on!(date)
  1168. end
  1169. end
  1170. end
  1171. end
  1172. def dates_derived?
  1173. !leaf? && Setting.parent_issue_dates == 'derived'
  1174. end
  1175. def priority_derived?
  1176. !leaf? && Setting.parent_issue_priority == 'derived'
  1177. end
  1178. def done_ratio_derived?
  1179. !leaf? && Setting.parent_issue_done_ratio == 'derived'
  1180. end
  1181. def <=>(issue)
  1182. if issue.nil?
  1183. -1
  1184. elsif root_id != issue.root_id
  1185. (root_id || 0) <=> (issue.root_id || 0)
  1186. else
  1187. (lft || 0) <=> (issue.lft || 0)
  1188. end
  1189. end
  1190. def to_s
  1191. "#{tracker} ##{id}: #{subject}"
  1192. end
  1193. # Returns a string of css classes that apply to the issue
  1194. def css_classes(user=User.current)
  1195. s = "issue tracker-#{tracker_id} status-#{status_id} #{priority.try(:css_classes)}"
  1196. s << ' closed' if closed?
  1197. s << ' overdue' if overdue?
  1198. s << ' child' if child?
  1199. s << ' parent' unless leaf?
  1200. s << ' private' if is_private?
  1201. if user.logged?
  1202. s << ' created-by-me' if author_id == user.id
  1203. s << ' assigned-to-me' if assigned_to_id == user.id
  1204. s << ' assigned-to-my-group' if user.groups.any? {|g| g.id == assigned_to_id}
  1205. end
  1206. s
  1207. end
  1208. # Unassigns issues from +version+ if it's no longer shared with issue's project
  1209. def self.update_versions_from_sharing_change(version)
  1210. # Update issues assigned to the version
  1211. update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
  1212. end
  1213. # Unassigns issues from versions that are no longer shared
  1214. # after +project+ was moved
  1215. def self.update_versions_from_hierarchy_change(project)
  1216. moved_project_ids = project.self_and_descendants.reload.pluck(:id)
  1217. # Update issues of the moved projects and issues assigned to a version of a moved project
  1218. Issue.update_versions(
  1219. ["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)",
  1220. moved_project_ids, moved_project_ids]
  1221. )
  1222. end
  1223. def parent_issue_id=(arg)
  1224. s = arg.to_s.strip.presence
  1225. if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1]))
  1226. @invalid_parent_issue_id = nil
  1227. elsif s.blank?
  1228. @parent_issue = nil
  1229. @invalid_parent_issue_id = nil
  1230. else
  1231. @parent_issue = nil
  1232. @invalid_parent_issue_id = arg
  1233. end
  1234. end
  1235. def parent_issue_id
  1236. if @invalid_parent_issue_id
  1237. @invalid_parent_issue_id
  1238. elsif instance_variable_defined? :@parent_issue
  1239. @parent_issue.nil? ? nil : @parent_issue.id
  1240. else
  1241. parent_id
  1242. end
  1243. end
  1244. def set_parent_id
  1245. self.parent_id = parent_issue_id
  1246. end
  1247. # Returns true if issue's project is a valid
  1248. # parent issue project
  1249. def valid_parent_project?(issue=parent)
  1250. return true if issue.nil? || issue.project_id == project_id
  1251. case Setting.cross_project_subtasks
  1252. when 'system'
  1253. true
  1254. when 'tree'
  1255. issue.project.root == project.root
  1256. when 'hierarchy'
  1257. issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project)
  1258. when 'descendants'
  1259. issue.project.is_or_is_ancestor_of?(project)
  1260. else
  1261. false
  1262. end
  1263. end
  1264. # Returns an issue scope based on project and scope
  1265. def self.cross_project_scope(project, scope=nil)
  1266. if project.nil?
  1267. return Issue
  1268. end
  1269. case scope
  1270. when 'all', 'system'
  1271. Issue
  1272. when 'tree'
  1273. Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
  1274. :lft => project.root.lft, :rgt => project.root.rgt)
  1275. when 'hierarchy'
  1276. Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt) OR (#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)",
  1277. :lft => project.lft, :rgt => project.rgt)
  1278. when 'descendants'
  1279. Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)",
  1280. :lft => project.lft, :rgt => project.rgt)
  1281. else
  1282. Issue.where(:project_id => project.id)
  1283. end
  1284. end
  1285. def self.by_tracker(project, with_subprojects=false)
  1286. count_and_group_by(:project => project, :association => :tracker, :with_subprojects => with_subprojects)
  1287. end
  1288. def self.by_version(project, with_subprojects=false)
  1289. count_and_group_by(:project => project, :association => :fixed_version, :with_subprojects => with_subprojects)
  1290. end
  1291. def self.by_priority(project, with_subprojects=false)
  1292. count_and_group_by(:project => project, :association => :priority, :with_subprojects => with_subprojects)
  1293. end
  1294. def self.by_category(project, with_subprojects=false)
  1295. count_and_group_by(:project => project, :association => :category, :with_subprojects => with_subprojects)
  1296. end
  1297. def self.by_assigned_to(project, with_subprojects=false)
  1298. count_and_group_by(:project => project, :association => :assigned_to, :with_subprojects => with_subprojects)
  1299. end
  1300. def self.by_author(project, with_subprojects=false)
  1301. count_and_group_by(:project => project, :association => :author, :with_subprojects => with_subprojects)
  1302. end
  1303. def self.by_subproject(project)
  1304. r = count_and_group_by(:project => project, :with_subprojects => true, :association => :project)
  1305. r.reject {|r| r["project_id"] == project.id.to_s}
  1306. end
  1307. # Query generator for selecting groups of issue counts for a project
  1308. # based on specific criteria
  1309. #
  1310. # Options
  1311. # * project - Project to search in.
  1312. # * with_subprojects - Includes subprojects issues if set to true.
  1313. # * association - Symbol. Association for grouping.
  1314. def self.count_and_group_by(options)
  1315. assoc = reflect_on_association(options[:association])
  1316. select_field = assoc.foreign_key
  1317. Issue.
  1318. visible(User.current, :project => options[:project], :with_subprojects => options[:with_subprojects]).
  1319. joins(:status, assoc.name).
  1320. group(:status_id, :is_closed, select_field).
  1321. count.
  1322. map do |columns, total|
  1323. status_id, is_closed, field_value = columns
  1324. is_closed = ['t', 'true', '1'].include?(is_closed.to_s)
  1325. {
  1326. "status_id" => status_id.to_s,
  1327. "closed" => is_closed,
  1328. select_field => field_value.to_s,
  1329. "total" => total.to_s
  1330. }
  1331. end
  1332. end
  1333. # Returns a scope of projects that user can assign the issue to
  1334. def allowed_target_projects(user=User.current, context=nil)
  1335. if new_record? && context.is_a?(Project) && !copy?
  1336. current_project = context.self_and_descendants
  1337. elsif new_record?
  1338. current_project = nil
  1339. else
  1340. current_project = project
  1341. end
  1342. self.class.allowed_target_projects(user, current_project)
  1343. end
  1344. # Returns a scope of projects that user can assign issues to
  1345. # If current_project is given, it will be included in the scope
  1346. def self.allowed_target_projects(user=User.current, current_project=nil)
  1347. condition = Project.allowed_to_condition(user, :add_issues)
  1348. if current_project.is_a?(Project)
  1349. condition = ["(#{condition}) OR #{Project.table_name}.id = ?", current_project.id]
  1350. elsif current_project
  1351. condition = ["(#{condition}) AND #{Project.table_name}.id IN (?)", current_project.map(&:id)]
  1352. end
  1353. Project.where(condition).having_trackers
  1354. end
  1355. # Returns a scope of trackers that user can assign the issue to
  1356. def allowed_target_trackers(user=User.current)
  1357. self.class.allowed_target_trackers(project, user, tracker_id_was)
  1358. end
  1359. # Returns a scope of trackers that user can assign project issues to
  1360. def self.allowed_target_trackers(project, user=User.current, current_tracker=nil)
  1361. if project
  1362. scope = project.trackers.sorted
  1363. unless user.admin?
  1364. roles = user.roles_for_project(project).select {|r| r.has_permission?(:add_issues)}
  1365. unless roles.any? {|r| r.permissions_all_trackers?(:add_issues)}
  1366. tracker_ids = roles.map {|r| r.permissions_tracker_ids(:add_issues)}.flatten.uniq
  1367. if current_tracker
  1368. tracker_ids << current_tracker
  1369. end
  1370. scope = scope.where(:id => tracker_ids)
  1371. end
  1372. end
  1373. scope
  1374. else
  1375. Tracker.none
  1376. end
  1377. end
  1378. private
  1379. def user_tracker_permission?(user, permission)
  1380. if project && !project.active?
  1381. perm = Redmine::AccessControl.permission(permission)
  1382. return false unless perm && perm.read?
  1383. end
  1384. if user.admin?
  1385. true
  1386. else
  1387. roles = user.roles_for_project(project).select {|r| r.has_permission?(permission)}
  1388. roles.any? {|r| r.permissions_all_trackers?(permission) || r.permissions_tracker_ids?(permission, tracker_id)}
  1389. end
  1390. end
  1391. def after_project_change
  1392. # Update project_id on related time entries
  1393. TimeEntry.where({:issue_id => id}).update_all(["project_id = ?", project_id])
  1394. # Delete issue relations
  1395. unless Setting.cross_project_issue_relations?
  1396. relations_from.clear
  1397. relations_to.clear
  1398. end
  1399. # Move subtasks that were in the same project
  1400. children.each do |child|
  1401. next unless child.project_id == project_id_before_last_save
  1402. # Change project and keep project
  1403. child.send :project=, project, true
  1404. unless child.save
  1405. errors.add :base, l(:error_move_of_child_not_possible, :child => "##{child.id}", :errors => child.errors.full_messages.join(", "))
  1406. raise ActiveRecord::Rollback
  1407. end
  1408. end
  1409. end
  1410. # Callback for after the creation of an issue by copy
  1411. # * adds a "copied to" relation with the copied issue
  1412. # * copies subtasks from the copied issue
  1413. def after_create_from_copy
  1414. return unless copy? && !@after_create_from_copy_handled
  1415. if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false
  1416. if @current_journal
  1417. @copied_from.init_journal(@current_journal.user)
  1418. end
  1419. relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO)
  1420. unless relation.save
  1421. logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
  1422. end
  1423. end
  1424. unless @copied_from.leaf? || @copy_options[:subtasks] == false
  1425. copy_options = (@copy_options || {}).merge(:subtasks => false)
  1426. copied_issue_ids = {@copied_from.id => self.id}
  1427. @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child|
  1428. # Do not copy self when copying an issue as a descendant of the copied issue
  1429. next if child == self
  1430. # Do not copy subtasks of issues that were not copied
  1431. next unless copied_issue_ids[child.parent_id]
  1432. # Do not copy subtasks that are not visible to avoid potential disclosure of private data
  1433. unless child.visible?
  1434. logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger
  1435. next
  1436. end
  1437. copy = Issue.new.copy_from(child, copy_options)
  1438. if @current_journal
  1439. copy.init_journal(@current_journal.user)
  1440. end
  1441. copy.author = author
  1442. copy.project = project
  1443. copy.parent_issue_id = copied_issue_ids[child.parent_id]
  1444. copy.fixed_version_id = nil unless child.fixed_version.present? && child.fixed_version.status == 'open'
  1445. copy.assigned_to = nil unless child.assigned_to_id.present? && child.assigned_to.status == User::STATUS_ACTIVE
  1446. unless copy.save
  1447. logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger
  1448. next
  1449. end
  1450. copied_issue_ids[child.id] = copy.id
  1451. end
  1452. end
  1453. @after_create_from_copy_handled = true
  1454. end
  1455. def update_nested_set_attributes
  1456. if saved_change_to_parent_id?
  1457. update_nested_set_attributes_on_parent_change
  1458. end
  1459. remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
  1460. end
  1461. # Updates the nested set for when an existing issue is moved
  1462. def update_nested_set_attributes_on_parent_change
  1463. former_parent_id = parent_id_before_last_save
  1464. # delete invalid relations of all descendants
  1465. self_and_descendants.each do |issue|
  1466. issue.relations.each do |relation|
  1467. relation.destroy unless relation.valid?
  1468. end
  1469. end
  1470. # update former parent
  1471. recalculate_attributes_for(former_parent_id) if former_parent_id
  1472. end
  1473. def update_parent_attributes
  1474. if parent_id
  1475. recalculate_attributes_for(parent_id)
  1476. association(:parent).reset
  1477. end
  1478. end
  1479. def recalculate_attributes_for(issue_id)
  1480. if issue_id && p = Issue.find_by_id(issue_id)
  1481. if p.priority_derived?
  1482. # priority = highest priority of open children
  1483. # priority is left unchanged if all children are closed and there's no default priority defined
  1484. if priority_position = p.children.open.joins(:priority).maximum("#{IssuePriority.table_name}.position")
  1485. p.priority = IssuePriority.find_by_position(priority_position)
  1486. elsif default_priority = IssuePriority.default
  1487. p.priority = default_priority
  1488. end
  1489. end
  1490. if p.dates_derived?
  1491. # start/due dates = lowest/highest dates of children
  1492. p.start_date = p.children.minimum(:start_date)
  1493. p.due_date = p.children.maximum(:due_date)
  1494. if p.start_date && p.due_date && p.due_date < p.start_date
  1495. p.start_date, p.due_date = p.due_date, p.start_date
  1496. end
  1497. end
  1498. if p.done_ratio_derived?
  1499. # done ratio = average ratio of children weighted with their total estimated hours
  1500. unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
  1501. children = p.children.to_a
  1502. if children.any?
  1503. child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0}
  1504. if child_with_total_estimated_hours.any?
  1505. average = child_with_total_estimated_hours.map(&:total_estimated_hours).sum.to_f / child_with_total_estimated_hours.count
  1506. else
  1507. average = 1.0
  1508. end
  1509. done = children.map {|c|
  1510. estimated = c.total_estimated_hours.to_f
  1511. estimated = average unless estimated > 0.0
  1512. ratio = c.closed? ? 100 : (c.done_ratio || 0)
  1513. estimated * ratio
  1514. }.sum
  1515. progress = done / (average * children.count)
  1516. p.done_ratio = progress.floor
  1517. end
  1518. end
  1519. end
  1520. # ancestors will be recursively updated
  1521. p.save(:validate => false)
  1522. end
  1523. end
  1524. # Update issues so their versions are not pointing to a
  1525. # fixed_version that is not shared with the issue's project
  1526. def self.update_versions(conditions=nil)
  1527. # Only need to update issues with a fixed_version from
  1528. # a different project and that is not systemwide shared
  1529. Issue.joins(:project, :fixed_version).
  1530. where("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
  1531. " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
  1532. " AND #{Version.table_name}.sharing <> 'system'").
  1533. where(conditions).each do |issue|
  1534. next if issue.project.nil? || issue.fixed_version.nil?
  1535. unless issue.project.shared_versions.include?(issue.fixed_version)
  1536. issue.init_journal(User.current)
  1537. issue.fixed_version = nil
  1538. issue.save
  1539. end
  1540. end
  1541. end
  1542. def delete_selected_attachments
  1543. if deleted_attachment_ids.present?
  1544. objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
  1545. attachments.delete(objects)
  1546. end
  1547. end
  1548. # Callback on file attachment
  1549. def attachment_added(attachment)
  1550. if current_journal && !attachment.new_record?
  1551. current_journal.journalize_attachment(attachment, :added)
  1552. end
  1553. end
  1554. # Callback on attachment deletion
  1555. def attachment_removed(attachment)
  1556. if current_journal && !attachment.new_record?
  1557. current_journal.journalize_attachment(attachment, :removed)
  1558. current_journal.save
  1559. end
  1560. end
  1561. # Called after a relation is added
  1562. def relation_added(relation)
  1563. if current_journal
  1564. current_journal.journalize_relation(relation, :added)
  1565. current_journal.save
  1566. end
  1567. end
  1568. # Called after a relation is removed
  1569. def relation_removed(relation)
  1570. if current_journal
  1571. current_journal.journalize_relation(relation, :removed)
  1572. current_journal.save
  1573. end
  1574. end
  1575. # Default assignment based on project or category
  1576. def default_assign
  1577. if assigned_to.nil?
  1578. if category && category.assigned_to
  1579. self.assigned_to = category.assigned_to
  1580. elsif project && project.default_assigned_to
  1581. self.assigned_to = project.default_assigned_to
  1582. end
  1583. end
  1584. end
  1585. # Updates start/due dates of following issues
  1586. def reschedule_following_issues
  1587. if saved_change_to_start_date? || saved_change_to_due_date?
  1588. relations_from.each do |relation|
  1589. relation.set_issue_to_dates(@current_journal)
  1590. end
  1591. end
  1592. end
  1593. # Closes duplicates if the issue is being closed
  1594. def close_duplicates
  1595. if Setting.close_duplicate_issues? && closing?
  1596. duplicates.each do |duplicate|
  1597. # Reload is needed in case the duplicate was updated by a previous duplicate
  1598. duplicate.reload
  1599. # Don't re-close it if it's already closed
  1600. next if duplicate.closed?
  1601. # Same user and notes
  1602. if @current_journal
  1603. duplicate.init_journal(@current_journal.user, @current_journal.notes)
  1604. duplicate.private_notes = @current_journal.private_notes
  1605. end
  1606. duplicate.update_attribute :status, self.status
  1607. end
  1608. end
  1609. end
  1610. # Make sure updated_on is updated when adding a note and set updated_on now
  1611. # so we can set closed_on with the same value on closing
  1612. def force_updated_on_change
  1613. if @current_journal || changed?
  1614. self.updated_on = current_time_from_proper_timezone
  1615. if new_record?
  1616. self.created_on = updated_on
  1617. end
  1618. end
  1619. end
  1620. # Callback for setting closed_on when the issue is closed.
  1621. # The closed_on attribute stores the time of the last closing
  1622. # and is preserved when the issue is reopened.
  1623. def update_closed_on
  1624. if closing?
  1625. self.closed_on = updated_on
  1626. end
  1627. end
  1628. # Saves the changes in a Journal
  1629. # Called after_save
  1630. def create_journal
  1631. if current_journal
  1632. current_journal.save
  1633. end
  1634. end
  1635. def send_notification
  1636. if notify? && Setting.notified_events.include?('issue_added')
  1637. Mailer.deliver_issue_add(self)
  1638. end
  1639. end
  1640. def clear_disabled_fields
  1641. if tracker
  1642. tracker.disabled_core_fields.each do |attribute|
  1643. send "#{attribute}=", nil
  1644. end
  1645. self.done_ratio ||= 0
  1646. end
  1647. end
  1648. end