To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / time_entry.rb @ 912:5e80956cc792
History | View | Annotate | Download (4.15 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
||
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 441:cbce1fd3b1b7 | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 441:cbce1fd3b1b7 | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | # could have used polymorphic association
|
||
| 20 | # project association here allows easy loading of time entries at project level with one database trip
|
||
| 21 | belongs_to :project
|
||
| 22 | belongs_to :issue
|
||
| 23 | belongs_to :user
|
||
| 24 | belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' |
||
| 25 | 441:cbce1fd3b1b7 | Chris | |
| 26 | 0:513646585e45 | Chris | attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek |
| 27 | |||
| 28 | acts_as_customizable |
||
| 29 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, |
||
| 30 | 37:94944d00e43c | chris | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
| 31 | 0:513646585e45 | Chris | :author => :user, |
| 32 | :description => :comments |
||
| 33 | |||
| 34 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
||
| 35 | :author_key => :user_id, |
||
| 36 | 441:cbce1fd3b1b7 | Chris | :find_options => {:include => :project} |
| 37 | 0:513646585e45 | Chris | |
| 38 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
||
| 39 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
||
| 40 | validates_length_of :comments, :maximum => 255, :allow_nil => true |
||
| 41 | 909:cbb26bc654de | Chris | before_validation :set_project_if_nil
|
| 42 | validate :validate_time_entry
|
||
| 43 | 0:513646585e45 | Chris | |
| 44 | 441:cbce1fd3b1b7 | Chris | named_scope :visible, lambda {|*args| {
|
| 45 | :include => :project, |
||
| 46 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args) |
||
| 47 | }} |
||
| 48 | |||
| 49 | 0:513646585e45 | Chris | def after_initialize |
| 50 | if new_record? && self.activity.nil? |
||
| 51 | if default_activity = TimeEntryActivity.default |
||
| 52 | self.activity_id = default_activity.id
|
||
| 53 | end
|
||
| 54 | self.hours = nil if hours == 0 |
||
| 55 | end
|
||
| 56 | end
|
||
| 57 | 441:cbce1fd3b1b7 | Chris | |
| 58 | 909:cbb26bc654de | Chris | def set_project_if_nil |
| 59 | 0:513646585e45 | Chris | self.project = issue.project if issue && project.nil? |
| 60 | end
|
||
| 61 | 441:cbce1fd3b1b7 | Chris | |
| 62 | 909:cbb26bc654de | Chris | def validate_time_entry |
| 63 | 0:513646585e45 | Chris | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
| 64 | errors.add :project_id, :invalid if project.nil? |
||
| 65 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) |
||
| 66 | end
|
||
| 67 | 441:cbce1fd3b1b7 | Chris | |
| 68 | 0:513646585e45 | Chris | def hours=(h) |
| 69 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
||
| 70 | end
|
||
| 71 | 441:cbce1fd3b1b7 | Chris | |
| 72 | 0:513646585e45 | Chris | # tyear, tmonth, tweek assigned where setting spent_on attributes
|
| 73 | # these attributes make time aggregations easier
|
||
| 74 | def spent_on=(date) |
||
| 75 | super
|
||
| 76 | 128:07fa8a8b56a8 | Chris | if spent_on.is_a?(Time) |
| 77 | self.spent_on = spent_on.to_date
|
||
| 78 | end
|
||
| 79 | 0:513646585e45 | Chris | self.tyear = spent_on ? spent_on.year : nil |
| 80 | self.tmonth = spent_on ? spent_on.month : nil |
||
| 81 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
||
| 82 | end
|
||
| 83 | 441:cbce1fd3b1b7 | Chris | |
| 84 | 0:513646585e45 | Chris | # Returns true if the time entry can be edited by usr, otherwise false
|
| 85 | def editable_by?(usr) |
||
| 86 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
||
| 87 | end
|
||
| 88 | 441:cbce1fd3b1b7 | Chris | |
| 89 | 22:40f7cfd4df19 | chris | def self.earilest_date_for_project(project=nil) |
| 90 | finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries)) |
||
| 91 | if project
|
||
| 92 | finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)] |
||
| 93 | end
|
||
| 94 | TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions) |
||
| 95 | end
|
||
| 96 | |||
| 97 | def self.latest_date_for_project(project=nil) |
||
| 98 | finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries)) |
||
| 99 | if project
|
||
| 100 | finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)] |
||
| 101 | end
|
||
| 102 | TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions) |
||
| 103 | end
|
||
| 104 | 0:513646585e45 | Chris | end |