annotate .svn/pristine/8f/8fa37bd50f73aaaf794ed1e7474b6ffe3abf2008.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Utils
Chris@1295 20 class << self
Chris@1295 21 # Returns the relative root url of the application
Chris@1295 22 def relative_url_root
Chris@1295 23 ActionController::Base.respond_to?('relative_url_root') ?
Chris@1295 24 ActionController::Base.relative_url_root.to_s :
Chris@1295 25 ActionController::Base.config.relative_url_root.to_s
Chris@1295 26 end
Chris@1295 27
Chris@1295 28 # Sets the relative root url of the application
Chris@1295 29 def relative_url_root=(arg)
Chris@1295 30 if ActionController::Base.respond_to?('relative_url_root=')
Chris@1295 31 ActionController::Base.relative_url_root=arg
Chris@1295 32 else
Chris@1295 33 ActionController::Base.config.relative_url_root = arg
Chris@1295 34 end
Chris@1295 35 end
Chris@1295 36
Chris@1295 37 # Generates a n bytes random hex string
Chris@1295 38 # Example:
Chris@1295 39 # random_hex(4) # => "89b8c729"
Chris@1295 40 def random_hex(n)
Chris@1295 41 SecureRandom.hex(n)
Chris@1295 42 end
Chris@1295 43 end
Chris@1295 44
Chris@1295 45 module Shell
Chris@1295 46 def shell_quote(str)
Chris@1295 47 if Redmine::Platform.mswin?
Chris@1295 48 '"' + str.gsub(/"/, '\\"') + '"'
Chris@1295 49 else
Chris@1295 50 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@1295 51 end
Chris@1295 52 end
Chris@1295 53 end
Chris@1295 54
Chris@1295 55 module DateCalculation
Chris@1295 56 # Returns the number of working days between from and to
Chris@1295 57 def working_days(from, to)
Chris@1295 58 days = (to - from).to_i
Chris@1295 59 if days > 0
Chris@1295 60 weeks = days / 7
Chris@1295 61 result = weeks * (7 - non_working_week_days.size)
Chris@1295 62 days_left = days - weeks * 7
Chris@1295 63 start_cwday = from.cwday
Chris@1295 64 days_left.times do |i|
Chris@1295 65 unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
Chris@1295 66 result += 1
Chris@1295 67 end
Chris@1295 68 end
Chris@1295 69 result
Chris@1295 70 else
Chris@1295 71 0
Chris@1295 72 end
Chris@1295 73 end
Chris@1295 74
Chris@1295 75 # Adds working days to the given date
Chris@1295 76 def add_working_days(date, working_days)
Chris@1295 77 if working_days > 0
Chris@1295 78 weeks = working_days / (7 - non_working_week_days.size)
Chris@1295 79 result = weeks * 7
Chris@1295 80 days_left = working_days - weeks * (7 - non_working_week_days.size)
Chris@1295 81 cwday = date.cwday
Chris@1295 82 while days_left > 0
Chris@1295 83 cwday += 1
Chris@1295 84 unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
Chris@1295 85 days_left -= 1
Chris@1295 86 end
Chris@1295 87 result += 1
Chris@1295 88 end
Chris@1295 89 next_working_date(date + result)
Chris@1295 90 else
Chris@1295 91 date
Chris@1295 92 end
Chris@1295 93 end
Chris@1295 94
Chris@1295 95 # Returns the date of the first day on or after the given date that is a working day
Chris@1295 96 def next_working_date(date)
Chris@1295 97 cwday = date.cwday
Chris@1295 98 days = 0
Chris@1295 99 while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
Chris@1295 100 days += 1
Chris@1295 101 end
Chris@1295 102 date + days
Chris@1295 103 end
Chris@1295 104
Chris@1295 105 # Returns the index of non working week days (1=monday, 7=sunday)
Chris@1295 106 def non_working_week_days
Chris@1295 107 @non_working_week_days ||= begin
Chris@1295 108 days = Setting.non_working_week_days
Chris@1295 109 if days.is_a?(Array) && days.size < 7
Chris@1295 110 days.map(&:to_i)
Chris@1295 111 else
Chris@1295 112 []
Chris@1295 113 end
Chris@1295 114 end
Chris@1295 115 end
Chris@1295 116 end
Chris@1295 117 end
Chris@1295 118 end