File: app/models/time_entry.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: TimeEntry#18
includes
  SafeAttributes ( Redmine )
inherits from
  Base ( ActiveRecord )
has properties
method: initialize / 2 #71
method: set_project_if_nil #81
method: validate_time_entry #85
method: hours= / 1 #91
method: hours #95
method: spent_on= / 1 #106
method: editable_by? / 1 #117

Class Hierarchy

Object ( Builtin-Module )
Base ( ActiveRecord )
  TimeEntry    #18

Code

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