Mercurial > hg > soundsoftware-site
comparison lib/redmine/utils.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
20 class << self | 20 class << self |
21 # Returns the relative root url of the application | 21 # Returns the relative root url of the application |
22 def relative_url_root | 22 def relative_url_root |
23 ActionController::Base.respond_to?('relative_url_root') ? | 23 ActionController::Base.respond_to?('relative_url_root') ? |
24 ActionController::Base.relative_url_root.to_s : | 24 ActionController::Base.relative_url_root.to_s : |
25 ActionController::AbstractRequest.relative_url_root.to_s | 25 ActionController::Base.config.relative_url_root.to_s |
26 end | 26 end |
27 | 27 |
28 # Sets the relative root url of the application | 28 # Sets the relative root url of the application |
29 def relative_url_root=(arg) | 29 def relative_url_root=(arg) |
30 if ActionController::Base.respond_to?('relative_url_root=') | 30 if ActionController::Base.respond_to?('relative_url_root=') |
31 ActionController::Base.relative_url_root=arg | 31 ActionController::Base.relative_url_root=arg |
32 else | 32 else |
33 ActionController::AbstractRequest.relative_url_root=arg | 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 | |
34 end | 114 end |
35 end | 115 end |
36 end | 116 end |
37 end | 117 end |
38 end | 118 end |