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