Project

General

Profile

Defect #5329 » 5329_add_year_of_week_to_time_entries.diff

Holger Just, 2010-06-04 17:06

View differences:

test/functional/timelog_controller_test.rb (working copy)
173 173
    assert_equal "8.65", "%.2f" % assigns(:total_hours)
174 174
  end
175 175
  
176
  def test_report_one_criteria_year_spanning_week
177
    get :report, :project_id => 1, :columns => 'week', :from => "2009-12-01", :to => "2010-01-31", :criterias => ['project']
178

  
179
    assert_response :success
180
    assert_template 'report'
181
    assert_not_nil assigns(:total_hours)
182
    assert_equal "3.00", "%.2f" % assigns(:total_hours)
183
    
184
    # one entry for 2009-12 and one for 2010-01
185
    assert_not_nil assigns(:hours)
186
    assert_equal 2, assigns(:hours).count
187
    
188
    assigns(:hours).each do |week|
189
      assert_equal "2009-53", week["week"]
190
    end
191
    hours = assigns(:hours).sum{|week| week["hours"].to_f}
192
    assert_equal "3.00", "%.2f" % hours
193
  end
194
  
195
  
176 196
  def test_report_two_criterias
177 197
    get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
178 198
    assert_response :success
test/fixtures/time_entries.yml (working copy)
13 13
  hours: 4.25
14 14
  user_id: 2
15 15
  tyear: 2007
16
  twyear: 2007
16 17
time_entries_002: 
17 18
  created_on: 2007-03-23 14:11:04 +01:00
18 19
  tweek: 11
......
27 28
  hours: 150.0
28 29
  user_id: 1
29 30
  tyear: 2007
31
  twyear: 2007
30 32
time_entries_003: 
31 33
  created_on: 2007-04-21 12:20:48 +02:00
32 34
  tweek: 16
......
41 43
  hours: 1.0
42 44
  user_id: 1
43 45
  tyear: 2007
46
  twyear: 2007
44 47
time_entries_004: 
45 48
  created_on: 2007-04-22 12:20:48 +02:00
46 49
  tweek: 16
......
55 58
  hours: 7.65
56 59
  user_id: 1
57 60
  tyear: 2007
58
  
61
  twyear: 2007
62
time_entries_005: 
63
  created_on: 2010-04-22 12:20:48 +02:00
64
  tweek: 53
65
  tmonth: 12
66
  project_id: 5
67
  comments: "old year, old week"
68
  updated_on: 2010-04-22 12:20:48 +02:00
69
  activity_id: 11
70
  spent_on: 2009-12-31
71
  issue_id: 
72
  id: 5
73
  hours: 1
74
  user_id: 1
75
  tyear: 2009
76
  twyear: 2009
77
time_entries_006: 
78
  created_on: 2010-04-22 12:20:48 +02:00
79
  tweek: 53
80
  tmonth: 1
81
  project_id: 5
82
  comments: "new year, old week"
83
  updated_on: 2007-04-22 12:20:48 +02:00
84
  activity_id: 11
85
  spent_on: 2010-01-01
86
  issue_id:
87
  id: 6
88
  hours: 2
89
  user_id: 1
90
  tyear: 2010
91
  twyear: 2009
app/models/time_entry.rb (working copy)
23 23
  belongs_to :user
24 24
  belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
25 25
  
26
  attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
26
  attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek, :twyear
27 27

  
28 28
  acts_as_customizable
29 29
  acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
......
62 62
    write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
63 63
  end
64 64
  
65
  # tyear, tmonth, tweek assigned where setting spent_on attributes
65
  # tyear, tmonth, tweek, twyear assigned where setting spent_on attributes
66 66
  # these attributes make time aggregations easier
67 67
  def spent_on=(date)
68 68
    super
69 69
    self.tyear = spent_on ? spent_on.year : nil
70 70
    self.tmonth = spent_on ? spent_on.month : nil
71 71
    self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
72
    self.twyear = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cwyear : nil
72 73
  end
73 74
  
74 75
  # Returns true if the time entry can be edited by usr, otherwise false
app/controllers/timelog_controller.rb (working copy)
81 81
    @criterias = @criterias[0,3]
82 82
    
83 83
    @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
84
    year_column = (@columns == 'week' ? 'twyear' : 'tyear')
84 85
    
85 86
    retrieve_date_range
86 87
    
......
97 98
        sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
98 99
      end
99 100

  
100
      sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
101
      sql = "SELECT #{sql_select}, #{year_column}, tmonth, tweek, spent_on, SUM(hours) AS hours"
101 102
      sql << " FROM #{TimeEntry.table_name}"
102 103
      sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
103 104
      sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
104 105
      sql << " WHERE"
105 106
      sql << " (%s) AND" % sql_condition
106 107
      sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
107
      sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
108
      sql << " GROUP BY #{sql_group_by}, #{year_column}, tmonth, tweek, spent_on"
108 109
      
109 110
      @hours = ActiveRecord::Base.connection.select_all(sql)
110 111
      
......
115 116
        when 'month'
116 117
          row['month'] = "#{row['tyear']}-#{row['tmonth']}"
117 118
        when 'week'
118
          row['week'] = "#{row['tyear']}-#{row['tweek']}"
119
          row['week'] = "#{row['twyear']}-#{row['tweek']}"
119 120
        when 'day'
120 121
          row['day'] = "#{row['spent_on']}"
121 122
        end
......
136 137
          @periods << "#{date_from.year}-#{date_from.month}"
137 138
          date_from = (date_from + 1.month).at_beginning_of_month
138 139
        when 'week'
139
          @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
140
          @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}"
140 141
          date_from = (date_from + 7.day).at_beginning_of_week
141 142
        when 'day'
142 143
          @periods << "#{date_from.to_date}"
db/migrate/20100630181445_add_year_of_week_to_time_entries.rb (revision 0)
1
class AddYearOfWeekToTimeEntries < ActiveRecord::Migration
2
  class TimeEntry < ActiveRecord::Base
3
  end
4
  
5
  def self.up
6
    add_column :time_entries, :twyear, :integer
7
    
8
    TimeEntry.all.each do |t|
9
      t.update_attributes({
10
        :twyear => Date.civil(t.spent_on.year, t.spent_on.month, t.spent_on.day).cwyear
11
      })
12
    end
13
    
14
    change_column :time_entries, :twyear, :integer, :null => false
15
  end
16

  
17
  def self.down
18
    remove_column :time_entries, :twyear
19
  end
20
end
(2-2/4)