diff app/models/time_entry.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents 5f33065ddc4b
children 622f24f53b42
line wrap: on
line diff
--- a/app/models/time_entry.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/app/models/time_entry.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -42,14 +42,34 @@
   before_validation :set_project_if_nil
   validate :validate_time_entry
 
-  named_scope :visible, lambda {|*args| {
+  scope :visible, lambda {|*args| {
     :include => :project,
     :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
   }}
+  scope :on_issue, lambda {|issue| {
+    :include => :issue,
+    :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
+  }}
+  scope :on_project, lambda {|project, include_subprojects| {
+    :include => :project,
+    :conditions => project.project_condition(include_subprojects)
+  }}
+  scope :spent_between, lambda {|from, to|
+    if from && to
+     {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
+    elsif from
+     {:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
+    elsif to
+     {:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
+    else
+     {}
+    end
+  }
 
-  safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values'
+  safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
 
-  def after_initialize
+  def initialize(attributes=nil, *args)
+    super
     if new_record? && self.activity.nil?
       if default_activity = TimeEntryActivity.default
         self.activity_id = default_activity.id
@@ -72,6 +92,15 @@
     write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
   end
 
+  def hours
+    h = read_attribute(:hours)
+    if h.is_a?(Float)
+      h.round(2)
+    else
+      h
+    end
+  end
+
   # tyear, tmonth, tweek assigned where setting spent_on attributes
   # these attributes make time aggregations easier
   def spent_on=(date)
@@ -88,20 +117,4 @@
   def editable_by?(usr)
     (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
   end
-
-  def self.earilest_date_for_project(project=nil)
-    finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
-    if project
-      finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
-    end
-    TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
-  end
-
-  def self.latest_date_for_project(project=nil)
-    finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
-    if project
-      finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
-    end
-    TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
-  end
 end