Class: TimeEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/time_entry.rb

Overview

Redmine - project management software Copyright © 2006-2011 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 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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.earilest_date_for_project(project = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'app/models/time_entry.rb', line 95

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

.latest_date_for_project(project = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'app/models/time_entry.rb', line 103

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

.visible_by(usr) ⇒ Object

TODO: remove this method in 1.3.0



88
89
90
91
92
93
# File 'app/models/time_entry.rb', line 88

def self.visible_by(usr)
  ActiveSupport::Deprecation.warn "TimeEntry.visible_by is deprecated and will be removed in Redmine 1.3.0. Use the visible scope instead."
  with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
    yield
  end
end

Instance Method Details

#after_initializeObject



47
48
49
50
51
52
53
54
# File 'app/models/time_entry.rb', line 47

def after_initialize
  if new_record? && self.activity.nil?
    if default_activity = TimeEntryActivity.default
      self.activity_id = default_activity.id
    end
    self.hours = nil if hours == 0
  end
end

#before_validationObject



56
57
58
# File 'app/models/time_entry.rb', line 56

def before_validation
  self.project = issue.project if issue && project.nil?
end

#editable_by?(usr) ⇒ Boolean

Returns true if the time entry can be edited by usr, otherwise false

Returns:

  • (Boolean)


83
84
85
# File 'app/models/time_entry.rb', line 83

def editable_by?(usr)
  (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
end

#hours=(h) ⇒ Object



66
67
68
# File 'app/models/time_entry.rb', line 66

def hours=(h)
  write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
end

#spent_on=(date) ⇒ Object

tyear, tmonth, tweek assigned where setting spent_on attributes these attributes make time aggregations easier



72
73
74
75
76
77
78
79
80
# File 'app/models/time_entry.rb', line 72

def spent_on=(date)
  super
  if spent_on.is_a?(Time)
    self.spent_on = spent_on.to_date
  end
  self.tyear = spent_on ? spent_on.year : nil
  self.tmonth = spent_on ? spent_on.month : nil
  self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
end

#validateObject



60
61
62
63
64
# File 'app/models/time_entry.rb', line 60

def validate
  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)
end