time_entry.rb
120 lines
| 4.4 KiB
| text/x-ruby
|
RubyLexer
|
r5029 | # Redmine - project management software | ||
# Copyright (C) 2006-2011 Jean-Philippe Lang | ||||
|
r569 | # | ||
# 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. | ||||
|
r5700 | # | ||
|
r569 | # 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. | ||||
|
r5700 | # | ||
|
r569 | # 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. | ||||
|
r365 | class TimeEntry < ActiveRecord::Base | ||
|
r9016 | include Redmine::SafeAttributes | ||
|
r365 | # 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 | ||||
|
r2677 | belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' | ||
|
r5700 | |||
|
r365 | attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek | ||
|
r1546 | |||
|
r1672 | acts_as_customizable | ||
|
r2763 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, | ||
|
r4163 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, | ||
|
r1546 | :author => :user, | ||
:description => :comments | ||||
|
r2763 | |||
acts_as_activity_provider :timestamp => "#{table_name}.created_on", | ||||
:author_key => :user_id, | ||||
|
r5700 | :find_options => {:include => :project} | ||
|
r2763 | |||
|
r365 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on | ||
|
r2430 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid | ||
|
r1594 | validates_length_of :comments, :maximum => 255, :allow_nil => true | ||
|
r7333 | before_validation :set_project_if_nil | ||
|
r7328 | validate :validate_time_entry | ||
|
r5700 | |||
named_scope :visible, lambda {|*args| { | ||||
|
r5029 | :include => :project, | ||
|
r5204 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args) | ||
|
r5029 | }} | ||
|
r7964 | named_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}" | ||||
}} | ||||
named_scope :on_project, lambda {|project, include_subprojects| { | ||||
:include => :project, | ||||
:conditions => project.project_condition(include_subprojects) | ||||
}} | ||||
|
r7965 | named_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 | ||||
} | ||||
|
r9016 | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values' | ||
|
r8168 | def initialize(attributes=nil, *args) | ||
super | ||||
|
r1519 | if new_record? && self.activity.nil? | ||
|
r2677 | if default_activity = TimeEntryActivity.default | ||
|
r1519 | self.activity_id = default_activity.id | ||
end | ||||
|
r3118 | self.hours = nil if hours == 0 | ||
|
r1447 | end | ||
end | ||||
|
r5700 | |||
|
r7333 | def set_project_if_nil | ||
|
r365 | self.project = issue.project if issue && project.nil? | ||
end | ||||
|
r5700 | |||
|
r7328 | def validate_time_entry | ||
|
r2430 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) | ||
errors.add :project_id, :invalid if project.nil? | ||||
errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) | ||||
|
r365 | end | ||
|
r5700 | |||
|
r1305 | def hours=(h) | ||
|
r2249 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) | ||
|
r1305 | end | ||
|
r5700 | |||
|
r8860 | def hours | ||
h = read_attribute(:hours) | ||||
if h.is_a?(Float) | ||||
h.round(2) | ||||
else | ||||
h | ||||
end | ||||
end | ||||
|
r365 | # tyear, tmonth, tweek assigned where setting spent_on attributes | ||
# these attributes make time aggregations easier | ||||
def spent_on=(date) | ||||
super | ||||
|
r4588 | if spent_on.is_a?(Time) | ||
self.spent_on = spent_on.to_date | ||||
end | ||||
|
r365 | self.tyear = spent_on ? spent_on.year : nil | ||
self.tmonth = spent_on ? spent_on.month : nil | ||||
|
r476 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil | ||
|
r1159 | end | ||
|
r5700 | |||
|
r1159 | # Returns true if the time entry can be edited by usr, otherwise false | ||
def editable_by?(usr) | ||||
|
r1235 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) | ||
|
r1159 | end | ||
|
r365 | end | ||