Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: class IssueStatus < ActiveRecord::Base Chris@1517: before_destroy :check_integrity Chris@1517: has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" Chris@1517: acts_as_list Chris@1517: Chris@1517: before_destroy :delete_workflow_rules Chris@1517: after_save :update_default Chris@1517: Chris@1517: validates_presence_of :name Chris@1517: validates_uniqueness_of :name Chris@1517: validates_length_of :name, :maximum => 30 Chris@1517: validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true Chris@1517: Chris@1517: scope :sorted, lambda { order("#{table_name}.position ASC") } Chris@1517: scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} Chris@1517: Chris@1517: def update_default Chris@1517: IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default? Chris@1517: end Chris@1517: Chris@1517: # Returns the default status for new issues Chris@1517: def self.default Chris@1517: where(:is_default => true).first Chris@1517: end Chris@1517: Chris@1517: # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ Chris@1517: def self.update_issue_done_ratios Chris@1517: if Issue.use_status_for_done_ratio? Chris@1517: IssueStatus.where("default_done_ratio >= 0").each do |status| Chris@1517: Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio}) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: return Issue.use_status_for_done_ratio? Chris@1517: end Chris@1517: Chris@1517: # Returns an array of all statuses the given role can switch to Chris@1517: # Uses association cache when called more than one time Chris@1517: def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) Chris@1517: if roles && tracker Chris@1517: role_ids = roles.collect(&:id) Chris@1517: transitions = workflows.select do |w| Chris@1517: role_ids.include?(w.role_id) && Chris@1517: w.tracker_id == tracker.id && Chris@1517: ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) Chris@1517: end Chris@1517: transitions.map(&:new_status).compact.sort Chris@1517: else Chris@1517: [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Same thing as above but uses a database query Chris@1517: # More efficient than the previous method if called just once Chris@1517: def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) Chris@1517: if roles.present? && tracker Chris@1517: conditions = "(author = :false AND assignee = :false)" Chris@1517: conditions << " OR author = :true" if author Chris@1517: conditions << " OR assignee = :true" if assignee Chris@1517: Chris@1517: workflows. Chris@1517: includes(:new_status). Chris@1517: where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})", Chris@1517: {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false} Chris@1517: ]).all. Chris@1517: map(&:new_status).compact.sort Chris@1517: else Chris@1517: [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def <=>(status) Chris@1517: position <=> status.position Chris@1517: end Chris@1517: Chris@1517: def to_s; name end Chris@1517: Chris@1517: private Chris@1517: Chris@1517: def check_integrity Chris@1517: raise "Can't delete status" if Issue.where(:status_id => id).any? Chris@1517: end Chris@1517: Chris@1517: # Deletes associated workflows Chris@1517: def delete_workflow_rules Chris@1517: WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) Chris@1517: end Chris@1517: end