14

How would I go about adding the "Spent Time" as a column to be displayed in the issues list?

Joel Meador
  • 2,586
  • 2
  • 19
  • 24

6 Answers6

12

Consolidating Eric and Joel's answers, this is what I needed to do to get a 'Spent time' column added to Redmine 1.0.3. Not sure if there's a better way to get the translation text added.

To give the new field a localised name, added to config/locales/en.yml around line 299 at the end of the field definitions:

  field_spent_hours: Spent time

To add the new column, created lib/spent_time_query_patch.rb with content:

# Based on http://github.com/edavis10/question_plugin/blob/master/lib/question_query_patch.rb
require_dependency 'query'

module QueryPatch
  def self.included(base) # :nodoc:
    base.extend(ClassMethods)

    # Same as typing in the class
    base.class_eval do
      unloadable # Send unloadable so it will not be unloaded in development
      base.add_available_column(QueryColumn.new(:spent_hours))
    end

  end

  module ClassMethods
    unless Query.respond_to?(:available_columns=)
      # Setter for +available_columns+ that isn't provided by the core.
      def available_columns=(v)
        self.available_columns = (v)
      end
    end

    unless Query.respond_to?(:add_available_column)
      # Method to add a column to the +available_columns+ that isn't provided by the core.
      def add_available_column(column)
        self.available_columns << (column)
      end
    end
  end
end

To get the spent_time_query_patch above to actually load, created config/initializers/spent_time_query_patch.rb with content:

require 'spent_time_query_patch'

Query.class_eval do
  include QueryPatch
end
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
user2067021
  • 4,399
  • 37
  • 44
  • 1
    Thanks for writing this down for others. :) – Joel Meador Nov 02 '10 at 18:21
  • This didn't seem to work for me - is there any commands to run to get redmine to see the changes? – Toby Nov 02 '10 at 20:14
  • I needed to stop and restart my web-server. (I'm not a Rails/Ruby expert, but if for some reason your setup isn't configured to autoload .rb files in config/initializers/ then the Query.class_eval chunk of code will never get run and the patch isn't loaded at all. Maybe double check that you've got file owner/permissions on the new files set correctly so the web-server can read them. Perhaps somebody with more Ruby expertise could comment on how to test what paths 'autoload' is active for...) – user2067021 Nov 02 '10 at 21:34
  • The lib and config/initializers directories don't get re-loaded by default without a rails app restart. – Joel Meador Nov 18 '10 at 08:13
  • Thanks a lot user493548 :) Worked like a charm! Redmine 1.2 – Ramon Araujo Jan 10 '12 at 01:07
9

You can also do this by adding the column at runtime. This will add the spent hours column without modifying the Redmine core. Just drop the following code into a file in lib/

Adapted from:

require_dependency 'query'

module QueryPatch
  def self.included(base) # :nodoc:
    base.extend(ClassMethods)

    # Same as typing in the class
    base.class_eval do
      unloadable # Send unloadable so it will not be unloaded in development
      base.add_available_column(QueryColumn.new(:spent_hours))
    end

  end

  module ClassMethods

    unless Query.respond_to?(:available_columns=)
      # Setter for +available_columns+ that isn't provided by the core.
      def available_columns=(v)
        self.available_columns = (v)
      end
    end

    unless Query.respond_to?(:add_available_column)
      # Method to add a column to the +available_columns+ that isn't provided by the core.
      def add_available_column(column)
        self.available_columns 
Eric Davis
  • 1,837
  • 14
  • 15
  • 1
    I successfully used this method last night to make sure the project shows up as a sortable column on its own. The full source code is not in this answer, just go to: http://github.com/edavis10/redmine-question-plugin/tree/master/lib/question_query_patch.rb Thanks Eric and Joel. – banderson623 Jan 09 '09 at 14:45
  • 2
    Since this answer is growing old I thought I should ask before trying: Do you know if this works with Redmine 0.9.3? – Oskar Jun 07 '10 at 15:19
  • @Osker: It should, I don't think that API has changed much. – Eric Davis Jun 10 '11 at 16:00
2

This feature build in from 1.4.0 version

diimdeep
  • 1,096
  • 17
  • 27
2

Also, it would be cool, if the column "Spent time" was sortable.

After looking up the produced SQL, I just implemented the sortable feature this in this way:

base.add_available_column(QueryColumn.new(:spent_hours, 
 :sortable => "(select sum(hours) from time_entries where time_entries.issue_id = t0_r0)")
) 

Replace the respective line. I just hope the issue_id column's name is always "t0_r0" ...

PS: You can find lots of examples in app/models/query.rb lines 122++

2-Digits Problem: Unfortunatly, I had to hack one of the core files: app/helpers/queries_helper.rb

Around line 44, change this:

when 'Fixnum', 'Float'                              
  if column.name == :done_ratio
    progress_bar(value, :width => '80px')
  else
    value.to_s
  end

into:

when 'Fixnum', 'Float'
  if column.name == :done_ratio
    progress_bar(value, :width => '80px')
  elsif column.name == :spent_hours
    sprintf "%.2f", value
  else
    value.to_s
  end

EDIT: Using a patch instead manipulating the source Recently, we did an update of the redmine system, so the above mentioned Fix also was removed. This time, we decided to implement that as a patch.

Open up any plugin (We created a plugin for our monkey-patch changes on core). open up vendor/plugins/redmine_YOURPLUGIN/app/helpers/queries_helper.rb

module QueriesHelper
  def new_column_content(column, issue)
    value = column.value(issue)
    if value.class.name == "Float" and column.name == :spent_hours
      sprintf "%.2f", value
    else
      __column_content(column, issue)
    end 
  end
  alias_method :__column_content, :column_content
  alias_method :column_content, :new_column_content
end
stwienert
  • 3,402
  • 24
  • 28
1

by using AgileDwarf plugin. You can have spent time & you can say for what you spent this time (developement - design -...)

parisssss
  • 803
  • 16
  • 36
1

Since no one answered, I just poked the source until it yielded results. Then I started a blog to explain how I did it.

Add spent time column to default issues list in Redmine

Joel Meador
  • 2,586
  • 2
  • 19
  • 24