You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_entry.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # redMine - project management software
  2. # Copyright (C) 2006-2008 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. class TimeEntry < ActiveRecord::Base
  18. # could have used polymorphic association
  19. # project association here allows easy loading of time entries at project level with one database trip
  20. belongs_to :project
  21. belongs_to :issue
  22. belongs_to :user
  23. belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
  24. attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
  25. acts_as_customizable
  26. acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
  27. :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
  28. :author => :user,
  29. :description => :comments
  30. acts_as_activity_provider :timestamp => "#{table_name}.created_on",
  31. :author_key => :user_id,
  32. :find_options => {:include => :project}
  33. validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
  34. validates_numericality_of :hours, :allow_nil => true, :message => :invalid
  35. validates_length_of :comments, :maximum => 255, :allow_nil => true
  36. def after_initialize
  37. if new_record? && self.activity.nil?
  38. if default_activity = TimeEntryActivity.default
  39. self.activity_id = default_activity.id
  40. end
  41. self.hours = nil if hours == 0
  42. end
  43. end
  44. def before_validation
  45. self.project = issue.project if issue && project.nil?
  46. end
  47. def validate
  48. errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
  49. errors.add :project_id, :invalid if project.nil?
  50. errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
  51. end
  52. def hours=(h)
  53. write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
  54. end
  55. # tyear, tmonth, tweek assigned where setting spent_on attributes
  56. # these attributes make time aggregations easier
  57. def spent_on=(date)
  58. super
  59. if spent_on.is_a?(Time)
  60. self.spent_on = spent_on.to_date
  61. end
  62. self.tyear = spent_on ? spent_on.year : nil
  63. self.tmonth = spent_on ? spent_on.month : nil
  64. self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
  65. end
  66. # Returns true if the time entry can be edited by usr, otherwise false
  67. def editable_by?(usr)
  68. (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
  69. end
  70. def self.visible_by(usr)
  71. with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
  72. yield
  73. end
  74. end
  75. def self.earilest_date_for_project(project=nil)
  76. finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
  77. if project
  78. finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
  79. end
  80. TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
  81. end
  82. def self.latest_date_for_project(project=nil)
  83. finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
  84. if project
  85. finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
  86. end
  87. TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
  88. end
  89. end