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