2007-06-24 20:07:06 +04:00
|
|
|
# redMine - project management software
|
2008-04-02 23:52:12 +04:00
|
|
|
# Copyright (C) 2006-2008 Jean-Philippe Lang
|
2007-06-24 20:07:06 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
class TimeEntry < ActiveRecord::Base
|
|
|
|
# could have used polymorphic association
|
|
|
|
# project association here allows easy loading of time entries at project level with one database trip
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :issue
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
|
|
|
|
|
|
|
|
attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
|
2008-06-18 00:01:15 +04:00
|
|
|
|
|
|
|
acts_as_event :title => Proc.new {|o| "#{o.user}: #{lwr(:label_f_hour, o.hours)} (#{(o.issue || o.project).event_title})"},
|
|
|
|
:url => Proc.new {|o| {:controller => 'timelog', :action => 'details', :project_id => o.project}},
|
|
|
|
:author => :user,
|
|
|
|
:description => :comments
|
2007-03-23 15:22:31 +03:00
|
|
|
|
|
|
|
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
|
|
|
validates_numericality_of :hours, :allow_nil => true
|
2007-04-25 19:06:20 +04:00
|
|
|
validates_length_of :comments, :maximum => 255
|
2007-03-23 15:22:31 +03:00
|
|
|
|
2008-05-26 21:12:11 +04:00
|
|
|
def after_initialize
|
2008-06-14 16:31:32 +04:00
|
|
|
if new_record? && self.activity.nil?
|
|
|
|
if default_activity = Enumeration.default('ACTI')
|
|
|
|
self.activity_id = default_activity.id
|
|
|
|
end
|
2008-05-26 21:12:11 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
def before_validation
|
|
|
|
self.project = issue.project if issue && project.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
2007-03-23 16:11:47 +03:00
|
|
|
errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
|
2007-03-23 15:22:31 +03:00
|
|
|
errors.add :project_id, :activerecord_error_invalid if project.nil?
|
|
|
|
errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
|
|
|
|
end
|
|
|
|
|
2008-04-02 23:52:12 +04:00
|
|
|
def hours=(h)
|
2008-04-26 15:59:51 +04:00
|
|
|
write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
|
2008-04-02 23:52:12 +04:00
|
|
|
end
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
# tyear, tmonth, tweek assigned where setting spent_on attributes
|
|
|
|
# these attributes make time aggregations easier
|
|
|
|
def spent_on=(date)
|
|
|
|
super
|
|
|
|
self.tyear = spent_on ? spent_on.year : nil
|
|
|
|
self.tmonth = spent_on ? spent_on.month : nil
|
2007-04-25 19:06:20 +04:00
|
|
|
self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
2008-02-26 21:15:58 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the time entry can be edited by usr, otherwise false
|
|
|
|
def editable_by?(usr)
|
2008-03-15 00:17:09 +03:00
|
|
|
(usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
2008-02-26 21:15:58 +03:00
|
|
|
end
|
2008-02-27 23:50:19 +03:00
|
|
|
|
|
|
|
def self.visible_by(usr)
|
|
|
|
with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|