annotate .svn/pristine/7d/7dca38a5b7c6d3fe11bbdf716206c78cae18789c.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class TimeEntry < ActiveRecord::Base
Chris@1295 19 include Redmine::SafeAttributes
Chris@1295 20 # could have used polymorphic association
Chris@1295 21 # project association here allows easy loading of time entries at project level with one database trip
Chris@1295 22 belongs_to :project
Chris@1295 23 belongs_to :issue
Chris@1295 24 belongs_to :user
Chris@1295 25 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
Chris@1295 26
Chris@1295 27 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
Chris@1295 28
Chris@1295 29 acts_as_customizable
Chris@1295 30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
Chris@1295 31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
Chris@1295 32 :author => :user,
Chris@1295 33 :description => :comments
Chris@1295 34
Chris@1295 35 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
Chris@1295 36 :author_key => :user_id,
Chris@1295 37 :find_options => {:include => :project}
Chris@1295 38
Chris@1295 39 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
Chris@1295 40 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
Chris@1295 41 validates_length_of :comments, :maximum => 255, :allow_nil => true
Chris@1295 42 before_validation :set_project_if_nil
Chris@1295 43 validate :validate_time_entry
Chris@1295 44
Chris@1295 45 scope :visible, lambda {|*args| {
Chris@1295 46 :include => :project,
Chris@1295 47 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
Chris@1295 48 }}
Chris@1295 49 scope :on_issue, lambda {|issue| {
Chris@1295 50 :include => :issue,
Chris@1295 51 :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
Chris@1295 52 }}
Chris@1295 53 scope :on_project, lambda {|project, include_subprojects| {
Chris@1295 54 :include => :project,
Chris@1295 55 :conditions => project.project_condition(include_subprojects)
Chris@1295 56 }}
Chris@1295 57 scope :spent_between, lambda {|from, to|
Chris@1295 58 if from && to
Chris@1295 59 {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
Chris@1295 60 elsif from
Chris@1295 61 {:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
Chris@1295 62 elsif to
Chris@1295 63 {:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
Chris@1295 64 else
Chris@1295 65 {}
Chris@1295 66 end
Chris@1295 67 }
Chris@1295 68
Chris@1295 69 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
Chris@1295 70
Chris@1295 71 def initialize(attributes=nil, *args)
Chris@1295 72 super
Chris@1295 73 if new_record? && self.activity.nil?
Chris@1295 74 if default_activity = TimeEntryActivity.default
Chris@1295 75 self.activity_id = default_activity.id
Chris@1295 76 end
Chris@1295 77 self.hours = nil if hours == 0
Chris@1295 78 end
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 def set_project_if_nil
Chris@1295 82 self.project = issue.project if issue && project.nil?
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 def validate_time_entry
Chris@1295 86 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
Chris@1295 87 errors.add :project_id, :invalid if project.nil?
Chris@1295 88 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
Chris@1295 89 end
Chris@1295 90
Chris@1295 91 def hours=(h)
Chris@1295 92 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
Chris@1295 93 end
Chris@1295 94
Chris@1295 95 def hours
Chris@1295 96 h = read_attribute(:hours)
Chris@1295 97 if h.is_a?(Float)
Chris@1295 98 h.round(2)
Chris@1295 99 else
Chris@1295 100 h
Chris@1295 101 end
Chris@1295 102 end
Chris@1295 103
Chris@1295 104 # tyear, tmonth, tweek assigned where setting spent_on attributes
Chris@1295 105 # these attributes make time aggregations easier
Chris@1295 106 def spent_on=(date)
Chris@1295 107 super
Chris@1295 108 if spent_on.is_a?(Time)
Chris@1295 109 self.spent_on = spent_on.to_date
Chris@1295 110 end
Chris@1295 111 self.tyear = spent_on ? spent_on.year : nil
Chris@1295 112 self.tmonth = spent_on ? spent_on.month : nil
Chris@1295 113 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
Chris@1295 114 end
Chris@1295 115
Chris@1295 116 # Returns true if the time entry can be edited by usr, otherwise false
Chris@1295 117 def editable_by?(usr)
Chris@1295 118 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
Chris@1295 119 end
Chris@1295 120 end