OpenProject is the leading open source project management software.
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.
 
 
 
 
 
 
openproject/app/models/work_package.rb

444 lines
16 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
# While loading the Issue class below, we lazy load the Project class. Which itself need Issue.
# So we create an 'emtpy' Issue class first, to make Project happy.
class WorkPackage < ActiveRecord::Base
#TODO Remove alternate inheritance column name once single table
# inheritance is no longer needed. The need for a different column name
# comes from Trackers becoming Types.
self.inheritance_column = :sti_type
include NestedAttributesForApi
belongs_to :project
belongs_to :type
belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
belongs_to :responsible, :class_name => "User", :foreign_key => "responsible_id"
belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
belongs_to :planning_element_type, :class_name => "PlanningElementType",
:foreign_key => 'planning_element_type_id'
belongs_to :planning_element_status, :class_name => "PlanningElementStatus",
:foreign_key => 'planning_element_status_id'
has_many :time_entries, :dependent => :delete_all
has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
scope :recently_updated, :order => "#{WorkPackage.table_name}.updated_at DESC"
scope :visible, lambda {|*args| { :include => :project,
:conditions => WorkPackage.visible_condition(args.first ||
User.current) } }
scope :without_deleted, :conditions => "#{WorkPackage.quoted_table_name}.deleted_at IS NULL"
scope :deleted, :conditions => "#{WorkPackage.quoted_table_name}.deleted_at IS NOT NULL"
acts_as_watchable
before_save :store_former_parent_id
include OpenProject::NestedSet::WithRootIdScope
after_save :reschedule_following_issues,
:update_parent_attributes,
:create_alternate_date
after_move :remove_invalid_relations,
:recalculate_attributes_for_former_parent
after_destroy :update_parent_attributes
acts_as_customizable
acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
:include => [:project, :journals],
# sort by id so that limited eager loading doesn't break with postgresql
:order_column => "#{table_name}.id"
##################### WARNING #####################
# Do not change the order of acts_as_attachable #
# and acts_as_journalized! #
# #
# This order ensures that no journal entries are #
# written after a project is destroyed. #
# #
# See test/unit/project_test.rb #
# test_destroying_root_projects_should_clear_data #
# for details. #
###################################################
acts_as_attachable :after_remove => :attachment_removed
acts_as_journalized :event_title => Proc.new {|o| "#{ o.to_s }"},
:event_type => (Proc.new do |o|
t = 'work_package'
if o.changed_data.empty?
t << '-note' unless o.initial?
else
t << (IssueStatus.find_by_id(
o.new_value_for(:status_id)).try(:is_closed?) ? '-closed' : '-edit'
)
end
t
end),
:except => ["root_id"]
register_on_journal_formatter(:id, 'parent_id')
register_on_journal_formatter(:fraction, 'estimated_hours')
register_on_journal_formatter(:decimal, 'done_ratio')
register_on_journal_formatter(:diff, 'description')
register_on_journal_formatter(:attachment, /attachments_?\d+/)
register_on_journal_formatter(:custom_field, /custom_values\d+/)
# Joined
register_on_journal_formatter :named_association, :parent_id, :project_id,
:status_id, :type_id,
:assigned_to_id, :priority_id,
:category_id, :fixed_version_id,
:planning_element_type_id,
:planning_element_status_id,
:author_id, :responsible_id
register_on_journal_formatter :datetime, :start_date, :due_date, :deleted_at
# By planning element
register_on_journal_formatter :plaintext, :subject,
:planning_element_status_comment,
:responsible_id
register_on_journal_formatter :scenario_date, /^scenario_(\d+)_(start|due)_date$/
# acts_as_journalized will create an initial journal on wp creation
# and touch the journaled object:
# journal.rb:47
#
# This will result in optimistic locking increasing the lock_version attribute to 1.
# In order to avoid stale object errors we reload the attributes in question
# after the wp is created.
# As after_create is run before after_save, and journal creation is triggered by an
# after_save hook, we rely on after_save and a specific version, here.
after_save :reload_lock_and_timestamps, :if => Proc.new { |wp| wp.lock_version == 0 }
# Returns a SQL conditions string used to find all work units visible by the specified user
def self.visible_condition(user, options={})
Project.allowed_to_condition(user, :view_work_packages, options)
end
WorkPackageJournal.class_eval do
# Shortcut
def new_status
if details.keys.include? 'status_id'
(newval = details['status_id'].last) ? IssueStatus.find_by_id(newval.to_i) : nil
end
end
end
def self.use_status_for_done_ratio?
Setting.issue_done_ratio == 'issue_status'
end
def self.use_field_for_done_ratio?
Setting.issue_done_ratio == 'issue_field'
end
# Returns true if usr or current user is allowed to view the issue
def visible?(usr=nil)
(usr || User.current).allowed_to?(:view_work_packages, self.project)
end
def copy_from(arg, options = {})
merged_options = { :exclude => ["id",
"root_id",
"parent_id",
"lft",
"rgt",
"type", # type_id is in options, type is for STI.
"created_at",
"updated_at"] + (options[:exclude]|| []).map(&:to_s) }
work_package = arg.is_a?(WorkPackage) ? arg : WorkPackage.visible.find(arg)
# attributes don't come from form, so it's save to force assign
self.force_attributes = work_package.attributes.dup.except(*merged_options[:exclude])
self.parent_issue_id = work_package.parent_id if work_package.parent_id
self.custom_field_values = work_package.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
self.status = work_package.status
self
end
# ACTS AS ATTACHABLE
# Callback on attachment deletion
def attachment_removed(obj)
init_journal(User.current)
create_journal
last_journal.update_attribute(:changed_data, { "attachments_#{obj.id}" => [obj.filename, nil] })
end
# ACTS AS JOURNALIZED
def activity_type
"work_packages"
end
# RELATIONS
def delete_relations(work_package)
unless Setting.cross_project_issue_relations?
work_package.relations_from.clear
work_package.relations_to.clear
end
end
def delete_invalid_relations(invalid_work_packages)
invalid_work_package.each do |work_package|
work_package.relations.each do |relation|
relation.destroy unless relation.valid?
end
end
end
# Returns true if this work package is blocked by another work package that is still open
def blocked?
!relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
end
def relations
IssueRelation.of_issue(self)
end
def relation(id)
IssueRelation.of_issue(self).find(id)
end
def new_relation
self.relations_from.build
end
def add_time_entry
time_entries.build(:project => project,
:work_package => self)
end
def all_dependent_issues(except=[])
except << self
dependencies = []
relations_from.each do |relation|
if relation.issue_to && !except.include?(relation.issue_to)
dependencies << relation.issue_to
dependencies += relation.issue_to.all_dependent_issues(except)
end
end
dependencies
end
# Returns an array of issues that duplicate this one
def duplicates
relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
end
def soonest_start
@soonest_start ||= (
relations_to.collect{|relation| relation.successor_soonest_start} +
ancestors.collect(&:soonest_start)
).compact.max
end
# Updates start/due dates of following issues
def reschedule_following_issues
if start_date_changed? || due_date_changed?
relations_from.each do |relation|
relation.set_issue_to_dates
end
end
end
# Users the work_package can be assigned to
delegate :assignable_users, :to => :project
# Versions that the issue can be assigned to
def assignable_versions
@assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
end
def kind
if self.is_a? Issue
return type
elsif self.is_a? PlanningElement
return planning_element_type
end
end
def to_s
"#{(kind.nil?) ? '' : "#{kind.name} "}##{id}: #{subject}"
end
# Return true if the work_package is closed, otherwise false
def closed?
self.status.nil? || self.status.is_closed?
end
# TODO: move into Business Object and rename to update
# update for now is a private method defined by AR
def update_by(user, attributes)
init_journal(user, attributes.delete(:notes)) if attributes[:notes]
raw_attachments = attributes.delete(:attachments)
add_time_entry_for(user, attributes.delete(:time_entry))
if update_attributes(attributes)
# as attach_files always saves an attachment right away
# it is not possible to stage attaching and check for
# valid. If this would be possible, we could check
# for this along with update_attributes
attachments = Attachment.attach_files(self, raw_attachments)
end
end
def is_milestone?
planning_element_type && planning_element_type.is_milestone?
end
# This is a dummy implementation that is currently overwritten
# by issue
# Adapt once tracker/type is migrated
def new_statuses_allowed_to(user, include_default = false)
IssueStatus.all
end
def self.use_status_for_done_ratio?
Setting.issue_done_ratio == 'issue_status'
end
# Returns the total number of hours spent on this issue and its descendants
#
# Example:
# spent_hours => 0.0
# spent_hours => 50.2
def spent_hours
@spent_hours ||= self_and_descendants.joins(:time_entries)
.sum("#{TimeEntry.table_name}.hours").to_f || 0.0
end
protected
def recalculate_attributes_for(work_package_id)
p = if work_package_id.is_a? WorkPackage
work_package_id
else
WorkPackage.find_by_id(work_package_id)
end
return unless p
p.inherit_priority_from_children
p.inherit_dates_from_children
p.inherit_done_ratio_from_leaves
p.inherit_estimated_hours_from_leaves
# ancestors will be recursively updated
if p.changed?
p.journal_notes = I18n.t('timelines.planning_element_updated_automatically_by_child_changes', :child => "*#{id}")
# Ancestors will be updated by parent's after_save hook.
p.save(:validate => false)
end
end
def update_parent_attributes
recalculate_attributes_for(parent_id) if parent_id.present?
end
def inherit_priority_from_children
# priority = highest priority of children
if priority_position = children.joins(:priority).maximum("#{IssuePriority.table_name}.position")
self.priority = IssuePriority.find_by_position(priority_position)
end
end
def inherit_dates_from_children
active_children = children.without_deleted
unless active_children.empty?
self.start_date = [active_children.minimum(:start_date), active_children.minimum(:due_date)].compact.min
self.due_date = [active_children.maximum(:start_date), active_children.maximum(:due_date)].compact.max
end
end
def inherit_done_ratio_from_leaves
# done ratio = weighted average ratio of leaves
unless WorkPackage.use_status_for_done_ratio? && status && status.default_done_ratio
leaves_count = leaves.count
if leaves_count > 0
average = leaves.average(:estimated_hours).to_f
if average == 0
average = 1
end
done = leaves.joins(:status).sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)").to_f
progress = done / (average * leaves_count)
self.done_ratio = progress.round
end
end
end
def inherit_estimated_hours_from_leaves
# estimate = sum of leaves estimates
self.estimated_hours = leaves.sum(:estimated_hours).to_f
self.estimated_hours = nil if estimated_hours == 0.0
end
def store_former_parent_id
@former_parent_id = (parent_issue_id != parent_id) ? parent_id : false
true # force callback to return true
end
def remove_invalid_relations
# delete invalid relations of all descendants
self_and_descendants.each do |issue|
issue.relations.each do |relation|
relation.destroy unless relation.valid?
end
end
end
def recalculate_attributes_for_former_parent
recalculate_attributes_for(@former_parent_id) if @former_parent_id
end
def reload_lock_and_timestamps
reload(:select => [:lock_version, :created_at, :updated_at])
end
private
def add_time_entry_for(user, attributes)
return if attributes.nil? || attributes.values.all?(&:blank?)
attributes.reverse_merge!({ :user => user,
:spent_on => Date.today })
time_entries.build(attributes)
end
def create_alternate_date
# This is a hack.
# It is required as long as alternate dates exist/are not moved up to work_packages.
# Its purpose is to allow for setting the after_save filter in the correct order
# before acts as journalized and the cleanup method reload_lock_and_timestamps.
return true unless respond_to?(:alternate_dates)
if start_date_changed? or due_date_changed?
alternate_dates.create(:start_date => start_date, :due_date => due_date)
end
end
end