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 @ 1566:ac2e4a54a6a6
History | View | Annotate | Download (4.67 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 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 | 929:5f33065ddc4b | Chris | include Redmine::SafeAttributes |
| 20 | 0:513646585e45 | Chris | # 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 | 441:cbce1fd3b1b7 | Chris | |
| 27 | 0:513646585e45 | Chris | 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 | 37:94944d00e43c | chris | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
| 32 | 0:513646585e45 | Chris | :author => :user, |
| 33 | 1464:261b3d9a4903 | Chris | :group => :issue, |
| 34 | 0:513646585e45 | Chris | :description => :comments |
| 35 | |||
| 36 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
||
| 37 | :author_key => :user_id, |
||
| 38 | 441:cbce1fd3b1b7 | Chris | :find_options => {:include => :project} |
| 39 | 0:513646585e45 | Chris | |
| 40 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
||
| 41 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
||
| 42 | validates_length_of :comments, :maximum => 255, :allow_nil => true |
||
| 43 | 1464:261b3d9a4903 | Chris | validates :spent_on, :date => true |
| 44 | 909:cbb26bc654de | Chris | before_validation :set_project_if_nil
|
| 45 | validate :validate_time_entry
|
||
| 46 | 0:513646585e45 | Chris | |
| 47 | 1464:261b3d9a4903 | Chris | scope :visible, lambda {|*args|
|
| 48 | includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)) |
||
| 49 | } |
||
| 50 | scope :on_issue, lambda {|issue|
|
||
| 51 | includes(:issue).where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") |
||
| 52 | } |
||
| 53 | scope :on_project, lambda {|project, include_subprojects|
|
||
| 54 | includes(:project).where(project.project_condition(include_subprojects))
|
||
| 55 | } |
||
| 56 | 1115:433d4f72a19b | Chris | scope :spent_between, lambda {|from, to|
|
| 57 | if from && to
|
||
| 58 | 1464:261b3d9a4903 | Chris | where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
|
| 59 | 1115:433d4f72a19b | Chris | elsif from
|
| 60 | 1464:261b3d9a4903 | Chris | where("#{TimeEntry.table_name}.spent_on >= ?", from)
|
| 61 | 1115:433d4f72a19b | Chris | elsif to
|
| 62 | 1464:261b3d9a4903 | Chris | where("#{TimeEntry.table_name}.spent_on <= ?", to)
|
| 63 | 1115:433d4f72a19b | Chris | else
|
| 64 | 1464:261b3d9a4903 | Chris | where(nil)
|
| 65 | 1115:433d4f72a19b | Chris | end
|
| 66 | } |
||
| 67 | 441:cbce1fd3b1b7 | Chris | |
| 68 | 1115:433d4f72a19b | Chris | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' |
| 69 | 929:5f33065ddc4b | Chris | |
| 70 | 1115:433d4f72a19b | Chris | def initialize(attributes=nil, *args) |
| 71 | super
|
||
| 72 | 0:513646585e45 | Chris | if new_record? && self.activity.nil? |
| 73 | if default_activity = TimeEntryActivity.default |
||
| 74 | self.activity_id = default_activity.id
|
||
| 75 | end
|
||
| 76 | self.hours = nil if hours == 0 |
||
| 77 | end
|
||
| 78 | end
|
||
| 79 | 441:cbce1fd3b1b7 | Chris | |
| 80 | 1517:dffacf8a6908 | Chris | def safe_attributes=(attrs, user=User.current) |
| 81 | attrs = super
|
||
| 82 | if !new_record? && issue && issue.project_id != project_id
|
||
| 83 | if user.allowed_to?(:log_time, issue.project) |
||
| 84 | self.project_id = issue.project_id
|
||
| 85 | end
|
||
| 86 | end
|
||
| 87 | attrs |
||
| 88 | end
|
||
| 89 | |||
| 90 | 909:cbb26bc654de | Chris | def set_project_if_nil |
| 91 | 0:513646585e45 | Chris | self.project = issue.project if issue && project.nil? |
| 92 | end
|
||
| 93 | 441:cbce1fd3b1b7 | Chris | |
| 94 | 909:cbb26bc654de | Chris | def validate_time_entry |
| 95 | 0:513646585e45 | Chris | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
| 96 | errors.add :project_id, :invalid if project.nil? |
||
| 97 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) |
||
| 98 | end
|
||
| 99 | 441:cbce1fd3b1b7 | Chris | |
| 100 | 0:513646585e45 | Chris | def hours=(h) |
| 101 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
||
| 102 | end
|
||
| 103 | 441:cbce1fd3b1b7 | Chris | |
| 104 | 1115:433d4f72a19b | Chris | def hours |
| 105 | h = read_attribute(:hours)
|
||
| 106 | if h.is_a?(Float) |
||
| 107 | h.round(2)
|
||
| 108 | else
|
||
| 109 | h |
||
| 110 | end
|
||
| 111 | end
|
||
| 112 | |||
| 113 | 0:513646585e45 | Chris | # tyear, tmonth, tweek assigned where setting spent_on attributes
|
| 114 | # these attributes make time aggregations easier
|
||
| 115 | def spent_on=(date) |
||
| 116 | super
|
||
| 117 | 128:07fa8a8b56a8 | Chris | if spent_on.is_a?(Time) |
| 118 | self.spent_on = spent_on.to_date
|
||
| 119 | end
|
||
| 120 | 0:513646585e45 | Chris | self.tyear = spent_on ? spent_on.year : nil |
| 121 | self.tmonth = spent_on ? spent_on.month : nil |
||
| 122 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
||
| 123 | end
|
||
| 124 | 441:cbce1fd3b1b7 | Chris | |
| 125 | 0:513646585e45 | Chris | # Returns true if the time entry can be edited by usr, otherwise false
|
| 126 | def editable_by?(usr) |
||
| 127 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
||
| 128 | end
|
||
| 129 | end |