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.

version.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 Version < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. after_update :update_issues_from_sharing_change
  20. after_save :update_default_project_version
  21. before_destroy :nullify_projects_default_version
  22. belongs_to :project
  23. has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify do
  24. # Returns the total estimated time for this version
  25. # (sum of leaves estimated_hours)
  26. def estimated_hours
  27. @estimated_hours ||= sum(:estimated_hours).to_f
  28. end
  29. #
  30. # Returns the total amount of open issues for this version.
  31. def open_count
  32. load_counts
  33. @open_count
  34. end
  35. # Returns the total amount of closed issues for this version.
  36. def closed_count
  37. load_counts
  38. @closed_count
  39. end
  40. # Returns the completion percentage of this version based on the amount of open/closed issues
  41. # and the time spent on the open issues.
  42. def completed_percent
  43. if count == 0
  44. 0
  45. elsif open_count == 0
  46. 100
  47. else
  48. issues_progress(false) + issues_progress(true)
  49. end
  50. end
  51. # Returns the percentage of issues that have been marked as 'closed'.
  52. def closed_percent
  53. if count == 0
  54. 0
  55. else
  56. issues_progress(false)
  57. end
  58. end
  59. private
  60. def load_counts
  61. unless @open_count
  62. @open_count = 0
  63. @closed_count = 0
  64. self.group(:status).count.each do |status, count|
  65. if status.is_closed?
  66. @closed_count += count
  67. else
  68. @open_count += count
  69. end
  70. end
  71. end
  72. end
  73. # Returns the average estimated time of assigned issues
  74. # or 1 if no issue has an estimated time
  75. # Used to weight unestimated issues in progress calculation
  76. def estimated_average
  77. if @estimated_average.nil?
  78. average = average(:estimated_hours).to_f
  79. if average == 0
  80. average = 1
  81. end
  82. @estimated_average = average
  83. end
  84. @estimated_average
  85. end
  86. # Returns the total progress of open or closed issues. The returned percentage takes into account
  87. # the amount of estimated time set for this version.
  88. #
  89. # Examples:
  90. # issues_progress(true) => returns the progress percentage for open issues.
  91. # issues_progress(false) => returns the progress percentage for closed issues.
  92. def issues_progress(open)
  93. @issues_progress ||= {}
  94. @issues_progress[open] ||= begin
  95. progress = 0
  96. if count > 0
  97. ratio = open ? 'done_ratio' : 100
  98. done = open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
  99. progress = done / (estimated_average * count)
  100. end
  101. progress
  102. end
  103. end
  104. end
  105. acts_as_customizable
  106. acts_as_attachable :view_permission => :view_files,
  107. :edit_permission => :manage_files,
  108. :delete_permission => :manage_files
  109. VERSION_STATUSES = %w(open locked closed)
  110. VERSION_SHARINGS = %w(none descendants hierarchy tree system)
  111. validates_presence_of :name
  112. validates_uniqueness_of :name, :scope => [:project_id]
  113. validates_length_of :name, :maximum => 60
  114. validates_length_of :description, :wiki_page_title, :maximum => 255
  115. validates :effective_date, :date => true
  116. validates_inclusion_of :status, :in => VERSION_STATUSES
  117. validates_inclusion_of :sharing, :in => VERSION_SHARINGS
  118. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  119. scope :like, lambda {|arg|
  120. if arg.present?
  121. pattern = "%#{arg.to_s.strip}%"
  122. where([Redmine::Database.like("#{Version.table_name}.name", '?'), pattern])
  123. end
  124. }
  125. scope :open, lambda { where(:status => 'open') }
  126. scope :status, lambda {|status|
  127. if status.present?
  128. where(:status => status.to_s)
  129. end
  130. }
  131. scope :visible, lambda {|*args|
  132. joins(:project).
  133. where(Project.allowed_to_condition(args.first || User.current, :view_issues))
  134. }
  135. safe_attributes 'name',
  136. 'description',
  137. 'effective_date',
  138. 'due_date',
  139. 'wiki_page_title',
  140. 'status',
  141. 'sharing',
  142. 'default_project_version',
  143. 'custom_field_values',
  144. 'custom_fields'
  145. # Returns true if +user+ or current user is allowed to view the version
  146. def visible?(user=User.current)
  147. user.allowed_to?(:view_issues, self.project)
  148. end
  149. # Version files have same visibility as project files
  150. def attachments_visible?(*args)
  151. project.present? && project.attachments_visible?(*args)
  152. end
  153. def attachments_deletable?(usr=User.current)
  154. project.present? && project.attachments_deletable?(usr)
  155. end
  156. alias :base_reload :reload
  157. def reload(*args)
  158. @default_project_version = nil
  159. @visible_fixed_issues = nil
  160. base_reload(*args)
  161. end
  162. def start_date
  163. @start_date ||= fixed_issues.minimum('start_date')
  164. end
  165. def due_date
  166. effective_date
  167. end
  168. def due_date=(arg)
  169. self.effective_date=(arg)
  170. end
  171. # Returns the total estimated time for this version
  172. # (sum of leaves estimated_hours)
  173. def estimated_hours
  174. fixed_issues.estimated_hours
  175. end
  176. # Returns the total reported time for this version
  177. def spent_hours
  178. @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
  179. end
  180. def closed?
  181. status == 'closed'
  182. end
  183. def open?
  184. status == 'open'
  185. end
  186. # Returns true if the version is completed: closed or due date reached and no open issues
  187. def completed?
  188. closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
  189. end
  190. def behind_schedule?
  191. if completed_percent == 100
  192. return false
  193. elsif due_date && start_date
  194. done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
  195. return done_date <= User.current.today
  196. else
  197. false # No issues so it's not late
  198. end
  199. end
  200. # Returns the completion percentage of this version based on the amount of open/closed issues
  201. # and the time spent on the open issues.
  202. def completed_percent
  203. fixed_issues.completed_percent
  204. end
  205. # Returns the percentage of issues that have been marked as 'closed'.
  206. def closed_percent
  207. fixed_issues.closed_percent
  208. end
  209. # Returns true if the version is overdue: due date reached and some open issues
  210. def overdue?
  211. effective_date && (effective_date < User.current.today) && (open_issues_count > 0)
  212. end
  213. # Returns assigned issues count
  214. def issues_count
  215. fixed_issues.count
  216. end
  217. # Returns the total amount of open issues for this version.
  218. def open_issues_count
  219. fixed_issues.open_count
  220. end
  221. # Returns the total amount of closed issues for this version.
  222. def closed_issues_count
  223. fixed_issues.closed_count
  224. end
  225. def visible_fixed_issues
  226. @visible_fixed_issues ||= fixed_issues.visible
  227. end
  228. def wiki_page
  229. if project.wiki && !wiki_page_title.blank?
  230. @wiki_page ||= project.wiki.find_page(wiki_page_title)
  231. end
  232. @wiki_page
  233. end
  234. def to_s; name end
  235. def to_s_with_project
  236. "#{project} - #{name}"
  237. end
  238. # Versions are sorted by effective_date and name
  239. # Those with no effective_date are at the end, sorted by name
  240. def <=>(version)
  241. if self.effective_date
  242. if version.effective_date
  243. if self.effective_date == version.effective_date
  244. name == version.name ? id <=> version.id : name <=> version.name
  245. else
  246. self.effective_date <=> version.effective_date
  247. end
  248. else
  249. -1
  250. end
  251. else
  252. if version.effective_date
  253. 1
  254. else
  255. name == version.name ? id <=> version.id : name <=> version.name
  256. end
  257. end
  258. end
  259. # Sort versions by status (open, locked then closed versions)
  260. def self.sort_by_status(versions)
  261. versions.sort do |a, b|
  262. if a.status == b.status
  263. a <=> b
  264. else
  265. b.status <=> a.status
  266. end
  267. end
  268. end
  269. def css_classes
  270. [
  271. completed? ? 'version-completed' : 'version-incompleted',
  272. "version-#{status}"
  273. ].join(' ')
  274. end
  275. def self.fields_for_order_statement(table=nil)
  276. table ||= table_name
  277. [Arel.sql("(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)"), "#{table}.effective_date", "#{table}.name", "#{table}.id"]
  278. end
  279. scope :sorted, lambda { order(fields_for_order_statement) }
  280. # Returns the sharings that +user+ can set the version to
  281. def allowed_sharings(user = User.current)
  282. VERSION_SHARINGS.select do |s|
  283. if sharing == s
  284. true
  285. else
  286. case s
  287. when 'system'
  288. # Only admin users can set a systemwide sharing
  289. user.admin?
  290. when 'hierarchy', 'tree'
  291. # Only users allowed to manage versions of the root project can
  292. # set sharing to hierarchy or tree
  293. project.nil? || user.allowed_to?(:manage_versions, project.root)
  294. else
  295. true
  296. end
  297. end
  298. end
  299. end
  300. # Returns true if the version is shared, otherwise false
  301. def shared?
  302. sharing != 'none'
  303. end
  304. def deletable?
  305. fixed_issues.empty? && !referenced_by_a_custom_field?
  306. end
  307. def default_project_version
  308. if @default_project_version.nil?
  309. project.present? && project.default_version == self
  310. else
  311. @default_project_version
  312. end
  313. end
  314. def default_project_version=(arg)
  315. @default_project_version = (arg == '1' || arg == true)
  316. end
  317. private
  318. # Update the issue's fixed versions. Used if a version's sharing changes.
  319. def update_issues_from_sharing_change
  320. if saved_change_to_sharing?
  321. if VERSION_SHARINGS.index(sharing_before_last_save).nil? ||
  322. VERSION_SHARINGS.index(sharing).nil? ||
  323. VERSION_SHARINGS.index(sharing_before_last_save) > VERSION_SHARINGS.index(sharing)
  324. Issue.update_versions_from_sharing_change self
  325. end
  326. end
  327. end
  328. def update_default_project_version
  329. if @default_project_version && project.present?
  330. project.update_columns :default_version_id => id
  331. end
  332. end
  333. def referenced_by_a_custom_field?
  334. CustomValue.joins(:custom_field).
  335. where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
  336. end
  337. def nullify_projects_default_version
  338. Project.where(:default_version_id => id).update_all(:default_version_id => nil)
  339. end
  340. end