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/story.rb

160 lines
4.0 KiB

class Story < WorkPackage
unloadable
15 years ago
extend OpenProject::Backlogs::Mixins::PreventIssueSti
def self.backlogs(project_id, sprint_ids, options = {})
14 years ago
options.reverse_merge!({ :order => Story::ORDER,
:conditions => Story.condition(project_id, sprint_ids) })
candidates = Story.all(options)
stories_by_version = Hash.new do |hash, sprint_id|
hash[sprint_id] = []
end
15 years ago
candidates.each do |story|
last_rank = stories_by_version[story.fixed_version_id].size > 0 ?
stories_by_version[story.fixed_version_id].last.rank :
0
story.rank = last_rank + 1
stories_by_version[story.fixed_version_id] << story
end
stories_by_version
end
def self.sprint_backlog(project, sprint, options={})
Story.backlogs(project.id, [sprint.id], options)[sprint.id]
end
def self.create_and_position(params, safer_attributes)
Story.new.tap do |s|
s.author = safer_attributes[:author] if safer_attributes[:author]
s.project = safer_attributes[:project] if safer_attributes[:project]
s.safe_attributes = params
13 years ago
if s.save
s.move_after(params['prev_id'])
13 years ago
end
end
end
def self.at_rank(project_id, sprint_id, rank)
return Story.find(:first,
:order => Story::ORDER,
:conditions => Story.condition(project_id, sprint_id),
:joins => :status,
:limit => 1,
:offset => rank - 1)
end
def self.types
types = Setting.plugin_openproject_backlogs["story_types"]
return [] if types.blank?
types.map { |type| Integer(type) }
end
15 years ago
def tasks
Task.tasks_for(self.id)
end
def tasks_and_subtasks
return [] unless Task.type
self.descendants.find_all_by_type_id(Task.type)
end
def direct_tasks_and_subtasks
return [] unless Task.type
self.children.find_all_by_type_id(Task.type).collect { |t| [t] + t.descendants }.flatten
end
def set_points(p)
self.init_journal(User.current)
if p.blank? || p == '-'
self.update_attribute(:story_points, nil)
return
end
if p.downcase == 's'
self.update_attribute(:story_points, 0)
return
end
p = Integer(p)
if p >= 0
self.update_attribute(:story_points, p)
return
end
end
# TODO: Refactor and add tests
#
# groups = tasks.partion(&:closed?)
# {:open => tasks.last.size, :closed => tasks.first.size}
#
def task_status
closed = 0
open = 0
self.tasks.each do |task|
if task.closed?
closed += 1
else
open += 1
end
end
{:open => open, :closed => closed}
end
def update_and_position!(params)
self.safe_attributes = params
self.status_id = nil if params[:status_id] == ''
13 years ago
save.tap do |result|
if result and params[:prev]
reload
move_after(params[:prev])
end
end
end
def rank=(r)
@rank = r
end
def rank
if self.position.blank?
extras = ["and ((#{WorkPackage.table_name}.position is NULL and #{WorkPackage.table_name}.id <= ?) or not #{WorkPackage.table_name}.position is NULL)", self.id]
else
extras = ["and not #{WorkPackage.table_name}.position is NULL and #{WorkPackage.table_name}.position <= ?", self.position]
end
@rank ||= WorkPackage.count(:conditions => Story.condition(self.project.id, self.fixed_version_id, extras), :joins => :status)
return @rank
end
private
def self.condition(project_id, sprint_ids, extras = [])
c = ["project_id = ? AND type_id in (?) AND fixed_version_id in (?)",
project_id, Story.types, sprint_ids]
if extras.size > 0
c[0] += ' ' + extras.shift
c += extras
end
c
end
# This forces NULLS-LAST ordering
ORDER = "CASE WHEN #{WorkPackage.table_name}.position IS NULL THEN 1 ELSE 0 END ASC, CASE WHEN #{WorkPackage.table_name}.position IS NULL THEN #{WorkPackage.table_name}.id ELSE #{WorkPackage.table_name}.position END ASC"
15 years ago
end