annotate .svn/pristine/95/95c84bdc44ab6fe07e7cd1c8d5b3a9e1298afcb7.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class TimeEntry < ActiveRecord::Base
Chris@1517 19 include Redmine::SafeAttributes
Chris@1517 20 # could have used polymorphic association
Chris@1517 21 # project association here allows easy loading of time entries at project level with one database trip
Chris@1517 22 belongs_to :project
Chris@1517 23 belongs_to :issue
Chris@1517 24 belongs_to :user
Chris@1517 25 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
Chris@1517 26
Chris@1517 27 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
Chris@1517 28
Chris@1517 29 acts_as_customizable
Chris@1517 30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
Chris@1517 31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
Chris@1517 32 :author => :user,
Chris@1517 33 :group => :issue,
Chris@1517 34 :description => :comments
Chris@1517 35
Chris@1517 36 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
Chris@1517 37 :author_key => :user_id,
Chris@1517 38 :find_options => {:include => :project}
Chris@1517 39
Chris@1517 40 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
Chris@1517 41 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
Chris@1517 42 validates_length_of :comments, :maximum => 255, :allow_nil => true
Chris@1517 43 validates :spent_on, :date => true
Chris@1517 44 before_validation :set_project_if_nil
Chris@1517 45 validate :validate_time_entry
Chris@1517 46
Chris@1517 47 scope :visible, lambda {|*args|
Chris@1517 48 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
Chris@1517 49 }
Chris@1517 50 scope :on_issue, lambda {|issue|
Chris@1517 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}")
Chris@1517 52 }
Chris@1517 53 scope :on_project, lambda {|project, include_subprojects|
Chris@1517 54 includes(:project).where(project.project_condition(include_subprojects))
Chris@1517 55 }
Chris@1517 56 scope :spent_between, lambda {|from, to|
Chris@1517 57 if from && to
Chris@1517 58 where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
Chris@1517 59 elsif from
Chris@1517 60 where("#{TimeEntry.table_name}.spent_on >= ?", from)
Chris@1517 61 elsif to
Chris@1517 62 where("#{TimeEntry.table_name}.spent_on <= ?", to)
Chris@1517 63 else
Chris@1517 64 where(nil)
Chris@1517 65 end
Chris@1517 66 }
Chris@1517 67
Chris@1517 68 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
Chris@1517 69
Chris@1517 70 def initialize(attributes=nil, *args)
Chris@1517 71 super
Chris@1517 72 if new_record? && self.activity.nil?
Chris@1517 73 if default_activity = TimeEntryActivity.default
Chris@1517 74 self.activity_id = default_activity.id
Chris@1517 75 end
Chris@1517 76 self.hours = nil if hours == 0
Chris@1517 77 end
Chris@1517 78 end
Chris@1517 79
Chris@1517 80 def safe_attributes=(attrs, user=User.current)
Chris@1517 81 attrs = super
Chris@1517 82 if !new_record? && issue && issue.project_id != project_id
Chris@1517 83 if user.allowed_to?(:log_time, issue.project)
Chris@1517 84 self.project_id = issue.project_id
Chris@1517 85 end
Chris@1517 86 end
Chris@1517 87 attrs
Chris@1517 88 end
Chris@1517 89
Chris@1517 90 def set_project_if_nil
Chris@1517 91 self.project = issue.project if issue && project.nil?
Chris@1517 92 end
Chris@1517 93
Chris@1517 94 def validate_time_entry
Chris@1517 95 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
Chris@1517 96 errors.add :project_id, :invalid if project.nil?
Chris@1517 97 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
Chris@1517 98 end
Chris@1517 99
Chris@1517 100 def hours=(h)
Chris@1517 101 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
Chris@1517 102 end
Chris@1517 103
Chris@1517 104 def hours
Chris@1517 105 h = read_attribute(:hours)
Chris@1517 106 if h.is_a?(Float)
Chris@1517 107 h.round(2)
Chris@1517 108 else
Chris@1517 109 h
Chris@1517 110 end
Chris@1517 111 end
Chris@1517 112
Chris@1517 113 # tyear, tmonth, tweek assigned where setting spent_on attributes
Chris@1517 114 # these attributes make time aggregations easier
Chris@1517 115 def spent_on=(date)
Chris@1517 116 super
Chris@1517 117 if spent_on.is_a?(Time)
Chris@1517 118 self.spent_on = spent_on.to_date
Chris@1517 119 end
Chris@1517 120 self.tyear = spent_on ? spent_on.year : nil
Chris@1517 121 self.tmonth = spent_on ? spent_on.month : nil
Chris@1517 122 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
Chris@1517 123 end
Chris@1517 124
Chris@1517 125 # Returns true if the time entry can be edited by usr, otherwise false
Chris@1517 126 def editable_by?(usr)
Chris@1517 127 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
Chris@1517 128 end
Chris@1517 129 end