summaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2011-12-04 16:43:32 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2011-12-04 16:43:32 +0000
commit6d498a3c46c999812f9fa78a420516657a3ddab2 (patch)
tree0f03cc39db154a292ff7e0d3906412e9922ce1cf /app/models
parentbddd19c1e662e6e5565ec1b2968b2907780c35de (diff)
downloadredmine-6d498a3c46c999812f9fa78a420516657a3ddab2.tar.gz
redmine-6d498a3c46c999812f9fa78a420516657a3ddab2.zip
Makes spent time column available on the issue list (#971).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8073 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r--app/models/issue.rb19
-rw-r--r--app/models/query.rb23
2 files changed, 37 insertions, 5 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb
index cbe3dd659..9eb567c9e 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -499,13 +499,18 @@ class Issue < ActiveRecord::Base
notified.collect(&:mail)
end
+ # Returns the number of hours spent on this issue
+ def spent_hours
+ @spent_hours ||= time_entries.sum(:hours) || 0
+ 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.sum("#{TimeEntry.table_name}.hours", :include => :time_entries).to_f || 0.0
+ def total_spent_hours
+ @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", :include => :time_entries).to_f || 0.0
end
def relations
@@ -522,6 +527,16 @@ class Issue < ActiveRecord::Base
end
end
+ # Preloads visible spent time for a collection of issues
+ def self.load_visible_spent_hours(issues, user=User.current)
+ if issues.any?
+ hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
+ issues.each do |issue|
+ issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
+ end
+ end
+ end
+
# Finds an issue relation given its id.
def find_relation(relation_id)
IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
diff --git a/app/models/query.rb b/app/models/query.rb
index f3b83d0f4..f41c1cdaa 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -356,11 +356,23 @@ class Query < ActiveRecord::Base
def available_columns
return @available_columns if @available_columns
- @available_columns = ::Query.available_columns
+ @available_columns = ::Query.available_columns.dup
@available_columns += (project ?
project.all_issue_custom_fields :
IssueCustomField.find(:all)
).collect {|cf| QueryCustomFieldColumn.new(cf) }
+
+ if User.current.allowed_to?(:view_time_entries, project, :global => true)
+ index = @available_columns.index {|column| column.name == :estimated_hours}
+ index = (index ? index + 1 : -1)
+ # insert the column after estimated_hours or at the end
+ @available_columns.insert index, QueryColumn.new(:spent_hours,
+ :sortable => "(SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)",
+ :default_order => 'desc',
+ :caption => :label_spent_time
+ )
+ end
+ @available_columns
end
def self.available_columns=(v)
@@ -412,7 +424,7 @@ class Query < ActiveRecord::Base
end
def has_column?(column)
- column_names && column_names.include?(column.name)
+ column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
end
def has_default_columns?
@@ -561,12 +573,17 @@ class Query < ActiveRecord::Base
joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil
- Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq,
+ issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq,
:conditions => statement,
:order => order_option,
:joins => joins,
:limit => options[:limit],
:offset => options[:offset]
+
+ if has_column?(:spent_hours)
+ Issue.load_visible_spent_hours(issues)
+ end
+ issues
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
end