diff 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
line wrap: on
line diff
--- a/lib/redmine/utils.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/lib/redmine/utils.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -22,7 +22,7 @@
       def relative_url_root
         ActionController::Base.respond_to?('relative_url_root') ?
           ActionController::Base.relative_url_root.to_s :
-          ActionController::AbstractRequest.relative_url_root.to_s
+          ActionController::Base.config.relative_url_root.to_s
       end
 
       # Sets the relative root url of the application
@@ -30,7 +30,87 @@
         if ActionController::Base.respond_to?('relative_url_root=')
           ActionController::Base.relative_url_root=arg
         else
-          ActionController::AbstractRequest.relative_url_root=arg
+          ActionController::Base.config.relative_url_root = arg
+        end
+      end
+
+      # Generates a n bytes random hex string
+      # Example:
+      #   random_hex(4) # => "89b8c729"
+      def random_hex(n)
+        SecureRandom.hex(n)
+      end
+    end
+
+    module Shell
+      def shell_quote(str)
+        if Redmine::Platform.mswin?
+          '"' + str.gsub(/"/, '\\"') + '"'
+        else
+          "'" + str.gsub(/'/, "'\"'\"'") + "'"
+        end
+      end
+    end
+
+    module DateCalculation
+      # Returns the number of working days between from and to
+      def working_days(from, to)
+        days = (to - from).to_i
+        if days > 0
+          weeks = days / 7
+          result = weeks * (7 - non_working_week_days.size)
+          days_left = days - weeks * 7
+          start_cwday = from.cwday
+          days_left.times do |i|
+            unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
+              result += 1
+            end
+          end
+          result
+        else
+          0
+        end
+      end
+
+      # Adds working days to the given date
+      def add_working_days(date, working_days)
+        if working_days > 0
+          weeks = working_days / (7 - non_working_week_days.size)
+          result = weeks * 7
+          days_left = working_days - weeks * (7 - non_working_week_days.size)
+          cwday = date.cwday
+          while days_left > 0
+            cwday += 1
+            unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
+              days_left -= 1
+            end
+            result += 1
+          end
+          next_working_date(date + result)
+        else
+          date
+        end
+      end
+
+      # Returns the date of the first day on or after the given date that is a working day
+      def next_working_date(date)
+        cwday = date.cwday
+        days = 0
+        while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
+          days += 1
+        end
+        date + days
+      end
+
+      # Returns the index of non working week days (1=monday, 7=sunday)
+      def non_working_week_days
+        @non_working_week_days ||= begin
+          days = Setting.non_working_week_days
+          if days.is_a?(Array) && days.size < 7
+            days.map(&:to_i)
+          else
+            []
+          end
         end
       end
     end