# HG changeset patch
# User Felix Schäfer <f.schaefer@finn.de>
# Date 1306336276 -7200
# Node ID db548264ca8f042cdbc4eb3e9f4e12d672b07148
# Parent  3d371482190fe1f6bb0c08c760d9f54d84e34e45
Use Rails.cache to cache application settings. #350

diff --git a/app/models/setting.rb b/app/models/setting.rb
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -85,10 +85,6 @@ class Setting < ActiveRecord::Base
   validates_inclusion_of :name, :in => @@available_settings.keys
   validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
 
-  # Hash used to cache setting values
-  @cached_settings = {}
-  @cached_cleared_on = Time.now
-
   def value
     v = read_attribute(:value)
     # Unserialize serialized settings
@@ -104,16 +100,17 @@ class Setting < ActiveRecord::Base
 
   # Returns the value of the setting named name
   def self.[](name)
-    v = @cached_settings[name]
-    v ? v : (@cached_settings[name] = find_or_default(name).value)
+    Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {
+        Marshal.dump(find_or_default(name).value)
+      }).freeze
   end
 
   def self.[]=(name, v)
     setting = find_or_default(name)
     setting.value = (v ? v : "")
-    @cached_settings[name] = nil
+    Rails.cache.delete "chiliproject/setting/#{name}"
     setting.save
-    setting.value
+    setting.value.freeze
   end
 
   # Defines getter and setter for each setting
@@ -150,10 +147,12 @@ class Setting < ActiveRecord::Base
   # Called once per request
   def self.check_cache
     settings_updated_on = Setting.maximum(:updated_on)
-    if settings_updated_on && @cached_cleared_on <= settings_updated_on
-      @cached_settings.clear
-      @cached_cleared_on = Time.now
-      logger.info "Settings cache cleared." if logger
+    cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on')
+    cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
+    if settings_updated_on && cache_cleared_on <= settings_updated_on
+      Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ )
+      Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now))
+      logger.info 'Settings cache cleared.' if logger
     end
   end
 
diff --git a/test/functional/documents_controller_test.rb b/test/functional/documents_controller_test.rb
--- a/test/functional/documents_controller_test.rb
+++ b/test/functional/documents_controller_test.rb
@@ -67,7 +67,7 @@ LOREM
 
   def test_new_with_one_attachment
     ActionMailer::Base.deliveries.clear
-    Setting.notified_events << 'document_added'
+    Setting.notified_events = Setting.notified_events.dup << 'document_added'
     @request.session[:user_id] = 2
     set_tmp_attachments_directory
 
diff --git a/test/functional/news_controller_test.rb b/test/functional/news_controller_test.rb
--- a/test/functional/news_controller_test.rb
+++ b/test/functional/news_controller_test.rb
@@ -67,7 +67,7 @@ class NewsControllerTest < ActionControl
   
   def test_post_create
     ActionMailer::Base.deliveries.clear
-    Setting.notified_events << 'news_added'
+    Setting.notified_events = Setting.notified_events.dup << 'news_added'
 
     @request.session[:user_id] = 2
     post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
diff --git a/test/functional/repositories_filesystem_controller_test.rb b/test/functional/repositories_filesystem_controller_test.rb
--- a/test/functional/repositories_filesystem_controller_test.rb
+++ b/test/functional/repositories_filesystem_controller_test.rb
@@ -35,7 +35,7 @@ class RepositoriesFilesystemControllerTe
     @request    = ActionController::TestRequest.new
     @response   = ActionController::TestResponse.new
     User.current = nil
-    Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
+    Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
     @repository = Repository::Filesystem.create(
                       :project       => Project.find(PRJ_ID),
                       :url           => REPOSITORY_PATH,
diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb
--- a/test/unit/comment_test.rb
+++ b/test/unit/comment_test.rb
@@ -33,7 +33,7 @@ class CommentTest < ActiveSupport::TestC
   end
   
   def test_create_should_send_notification
-    Setting.notified_events << 'news_comment_added'
+    Setting.notified_events = Setting.notified_events.dup << 'news_comment_added'
     Watcher.create!(:watchable => @news, :user => @jsmith)
     
     assert_difference 'ActionMailer::Base.deliveries.size' do
diff --git a/test/unit/document_test.rb b/test/unit/document_test.rb
--- a/test/unit/document_test.rb
+++ b/test/unit/document_test.rb
@@ -27,7 +27,7 @@ class DocumentTest < ActiveSupport::Test
 
   def test_create_should_send_email_notification
     ActionMailer::Base.deliveries.clear
-    Setting.notified_events << 'document_added'
+    Setting.notified_events = Setting.notified_events.dup << 'document_added'
     doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
 
     assert doc.save
diff --git a/test/unit/news_test.rb b/test/unit/news_test.rb
--- a/test/unit/news_test.rb
+++ b/test/unit/news_test.rb
@@ -30,7 +30,7 @@ class NewsTest < ActiveSupport::TestCase
   
   def test_create_should_send_email_notification
     ActionMailer::Base.deliveries.clear
-    Setting.notified_events << 'news_added'
+    Setting.notified_events = Setting.notified_events.dup << 'news_added'
     news = Project.find(:first).news.new(valid_news)
 
     assert news.save
diff --git a/test/unit/repository_filesystem_test.rb b/test/unit/repository_filesystem_test.rb
--- a/test/unit/repository_filesystem_test.rb
+++ b/test/unit/repository_filesystem_test.rb
@@ -25,7 +25,9 @@ class RepositoryFilesystemTest < ActiveS
 
   def setup
     @project = Project.find(3)
-    Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
+    unless Setting.enabled_scm.include?('Filesystem')
+      Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem'
+    end
     @repository = Repository::Filesystem.create(
                                :project => @project,
                                :url     => REPOSITORY_PATH
# HG changeset patch
# User Toshi MARUYAMA
# Date 1306569560 -32400
# Node ID 4b6b58b1dc090371e7f828162a053752b2b3e199
# Parent  db548264ca8f042cdbc4eb3e9f4e12d672b07148
s/chiliproject/redmine/

diff --git a/app/models/setting.rb b/app/models/setting.rb
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -100,7 +100,7 @@ class Setting < ActiveRecord::Base
 
   # Returns the value of the setting named name
   def self.[](name)
-    Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {
+    Marshal.load(Rails.cache.fetch("redmine/setting/#{name}") {
         Marshal.dump(find_or_default(name).value)
       }).freeze
   end
@@ -108,7 +108,7 @@ class Setting < ActiveRecord::Base
   def self.[]=(name, v)
     setting = find_or_default(name)
     setting.value = (v ? v : "")
-    Rails.cache.delete "chiliproject/setting/#{name}"
+    Rails.cache.delete "redmine/setting/#{name}"
     setting.save
     setting.value.freeze
   end
@@ -147,11 +147,11 @@ class Setting < ActiveRecord::Base
   # Called once per request
   def self.check_cache
     settings_updated_on = Setting.maximum(:updated_on)
-    cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on')
+    cache_cleared_on = Rails.cache.read('redmine/setting-cleared_on')
     cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
     if settings_updated_on && cache_cleared_on <= settings_updated_on
-      Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ )
-      Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now))
+      Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
+      Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
       logger.info 'Settings cache cleared.' if logger
     end
   end
# HG changeset patch
# User Eric Davis <edavis@littlestreamsoftware.com>
# Date 1306533164 25200
# Node ID be8e07db0577f09b1828934a102ef03ffb10edfd
# Parent  4b6b58b1dc090371e7f828162a053752b2b3e199
[#350] Refactor: extract method

diff --git a/app/models/setting.rb b/app/models/setting.rb
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -150,12 +150,17 @@ class Setting < ActiveRecord::Base
     cache_cleared_on = Rails.cache.read('redmine/setting-cleared_on')
     cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
     if settings_updated_on && cache_cleared_on <= settings_updated_on
-      Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
-      Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
-      logger.info 'Settings cache cleared.' if logger
+      clear_cache
     end
   end
 
+  # Clears all of the Setting caches
+  def self.clear_cache
+    Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
+    Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
+    logger.info 'Settings cache cleared.' if logger
+  end
+
 private
   # Returns the Setting instance for the setting named name
   # (record found in database or new record with default value)
# HG changeset patch
# User Eric Davis <edavis@littlestreamsoftware.com>
# Date 1306536744 25200
# Node ID ef43960e85a8adc6b6604a799521a358af4eab94
# Parent  be8e07db0577f09b1828934a102ef03ffb10edfd
[#350] Force clearing the cache before each test

diff --git a/test/test_helper.rb b/test/test_helper.rb
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -47,6 +47,10 @@ class ActiveSupport::TestCase
   self.use_instantiated_fixtures  = false
 
   # Add more helper methods to be used by all tests here...
+  def setup
+    super
+    Setting.clear_cache
+  end
   
   def log_user(login, password)
     User.anonymous
