Class: TimeEntry

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

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute_names, #safe_attributes=

Class Method Details

.earilest_date_for_project(project = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'app/models/time_entry.rb', line 91

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



99
100
101
102
103
104
105
# File 'app/models/time_entry.rb', line 99

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



84
85
86
87
88
89
# File 'app/models/time_entry.rb', line 84

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



43
44
45
46
47
48
49
50
# File 'app/models/time_entry.rb', line 43

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



52
53
54
# File 'app/models/time_entry.rb', line 52

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



79
80
81
# File 'app/models/time_entry.rb', line 79

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



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

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



68
69
70
71
72
73
74
75
76
# File 'app/models/time_entry.rb', line 68

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



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

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