annotate .svn/pristine/64/6499f7c61adfe6ad6a509569a7c1fd317d2459d5.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +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 module Redmine
Chris@1494 19 module Utils
Chris@1494 20 class << self
Chris@1494 21 # Returns the relative root url of the application
Chris@1494 22 def relative_url_root
Chris@1494 23 ActionController::Base.respond_to?('relative_url_root') ?
Chris@1494 24 ActionController::Base.relative_url_root.to_s :
Chris@1494 25 ActionController::Base.config.relative_url_root.to_s
Chris@1494 26 end
Chris@1494 27
Chris@1494 28 # Sets the relative root url of the application
Chris@1494 29 def relative_url_root=(arg)
Chris@1494 30 if ActionController::Base.respond_to?('relative_url_root=')
Chris@1494 31 ActionController::Base.relative_url_root=arg
Chris@1494 32 else
Chris@1494 33 ActionController::Base.config.relative_url_root = arg
Chris@1494 34 end
Chris@1494 35 end
Chris@1494 36
Chris@1494 37 # Generates a n bytes random hex string
Chris@1494 38 # Example:
Chris@1494 39 # random_hex(4) # => "89b8c729"
Chris@1494 40 def random_hex(n)
Chris@1494 41 SecureRandom.hex(n)
Chris@1494 42 end
Chris@1494 43 end
Chris@1494 44
Chris@1494 45 module Shell
Chris@1494 46 def shell_quote(str)
Chris@1494 47 if Redmine::Platform.mswin?
Chris@1494 48 '"' + str.gsub(/"/, '\\"') + '"'
Chris@1494 49 else
Chris@1494 50 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@1494 51 end
Chris@1494 52 end
Chris@1494 53 end
Chris@1494 54
Chris@1494 55 module DateCalculation
Chris@1494 56 # Returns the number of working days between from and to
Chris@1494 57 def working_days(from, to)
Chris@1494 58 days = (to - from).to_i
Chris@1494 59 if days > 0
Chris@1494 60 weeks = days / 7
Chris@1494 61 result = weeks * (7 - non_working_week_days.size)
Chris@1494 62 days_left = days - weeks * 7
Chris@1494 63 start_cwday = from.cwday
Chris@1494 64 days_left.times do |i|
Chris@1494 65 unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
Chris@1494 66 result += 1
Chris@1494 67 end
Chris@1494 68 end
Chris@1494 69 result
Chris@1494 70 else
Chris@1494 71 0
Chris@1494 72 end
Chris@1494 73 end
Chris@1494 74
Chris@1494 75 # Adds working days to the given date
Chris@1494 76 def add_working_days(date, working_days)
Chris@1494 77 if working_days > 0
Chris@1494 78 weeks = working_days / (7 - non_working_week_days.size)
Chris@1494 79 result = weeks * 7
Chris@1494 80 days_left = working_days - weeks * (7 - non_working_week_days.size)
Chris@1494 81 cwday = date.cwday
Chris@1494 82 while days_left > 0
Chris@1494 83 cwday += 1
Chris@1494 84 unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
Chris@1494 85 days_left -= 1
Chris@1494 86 end
Chris@1494 87 result += 1
Chris@1494 88 end
Chris@1494 89 next_working_date(date + result)
Chris@1494 90 else
Chris@1494 91 date
Chris@1494 92 end
Chris@1494 93 end
Chris@1494 94
Chris@1494 95 # Returns the date of the first day on or after the given date that is a working day
Chris@1494 96 def next_working_date(date)
Chris@1494 97 cwday = date.cwday
Chris@1494 98 days = 0
Chris@1494 99 while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
Chris@1494 100 days += 1
Chris@1494 101 end
Chris@1494 102 date + days
Chris@1494 103 end
Chris@1494 104
Chris@1494 105 # Returns the index of non working week days (1=monday, 7=sunday)
Chris@1494 106 def non_working_week_days
Chris@1494 107 @non_working_week_days ||= begin
Chris@1494 108 days = Setting.non_working_week_days
Chris@1494 109 if days.is_a?(Array) && days.size < 7
Chris@1494 110 days.map(&:to_i)
Chris@1494 111 else
Chris@1494 112 []
Chris@1494 113 end
Chris@1494 114 end
Chris@1494 115 end
Chris@1494 116 end
Chris@1494 117 end
Chris@1494 118 end