Skip to content
Browse files

Added easy assignment of fragment cache store through use of symbols …

…for included stores (old way still works too)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 48018bf commit ebb7bd78351b7975f28fdcbadbbf0fe4804be2f2 @dhh dhh committed Sep 13, 2005
View
9 actionpack/CHANGELOG
@@ -1,5 +1,14 @@
*SVN*
+* Added easy assignment of fragment cache store through use of symbols for included stores (old way still works too)
+
+ Before:
+ ActionController::Base.fragment_cache_store =
+ ActionController::Base::Caching::Fragments::FileStore.new("/path/to/cache/directory")
+
+ After:
+ ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
+
* Added ActionController::Base.session_store=, session_store, and session_options to make it easier to tweak the session options (instead of going straight to ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)
* Added TextHelper#cycle to cycle over an array of values on each hit (useful for alternating row colors etc) #2154 [dave-ml@dribin.org]
View
2 actionpack/lib/action_controller.rb
@@ -38,7 +38,6 @@
require 'action_controller/filters'
require 'action_controller/layout'
require 'action_controller/flash'
-require 'action_controller/session'
require 'action_controller/dependencies'
require 'action_controller/pagination'
require 'action_controller/scaffolding'
@@ -67,7 +66,6 @@
include ActionController::Scaffolding
include ActionController::Helpers
include ActionController::Cookies
- include ActionController::Session
include ActionController::Caching
include ActionController::Components
include ActionController::Verification
View
37 actionpack/lib/action_controller/caching.rb
@@ -240,23 +240,28 @@ def after(controller)
#
# Configuration examples (MemoryStore is the default):
#
- # ActionController::Base.fragment_cache_store =
- # ActionController::Caching::Fragments::MemoryStore.new
- #
- # ActionController::Base.fragment_cache_store =
- # ActionController::Caching::Fragments::FileStore.new("/path/to/cache/directory")
- #
- # ActionController::Base.fragment_cache_store =
- # ActionController::Caching::Fragments::DRbStore.new("druby://localhost:9192")
- #
- # ActionController::Base.fragment_cache_store =
- # ActionController::Caching::Fragments::MemCacheStore.new("localhost")
+ # ActionController::Base.fragment_cache_store = :memory_store
+ # ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
+ # ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
+ # ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
+ # ActionController::Base.fragment_cache_store = MyOwnStore.new("parameter")
module Fragments
def self.append_features(base) #:nodoc:
super
base.class_eval do
@@fragment_cache_store = MemoryStore.new
- cattr_accessor :fragment_cache_store
+ cattr_reader :fragment_cache_store
+
+ def self.fragment_cache_store=(store_option)
+ store, *parameters = *([ store_option ].flatten)
+ @@fragment_cache_store = if store.is_a?(Symbol)
+ store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
+ store_class = ActionController::Caching::Fragments.const_get(store_class_name)
+ parameters.empty? ? store.new : store_class.new(*parameters)
+ else
+ store
+ end
+ end
end
end
@@ -349,18 +354,26 @@ def delete_matched(matcher, options) #:nodoc:
end
class DRbStore < MemoryStore #:nodoc:
+ attr_reader :address
+
def initialize(address = 'druby://localhost:9192')
+ @address = address
@data, @mutex = DRbObject.new(nil, address), Mutex.new
end
end
class MemCacheStore < MemoryStore #:nodoc:
+ attr_reader :address
+
def initialize(address = 'localhost')
+ @address = address
@data, @mutex = MemCache.new(address), Mutex.new
end
end
class FileStore #:nodoc:
+ attr_reader :cache_path
+
def initialize(cache_path)
@cache_path = cache_path
end
View
5 actionpack/lib/action_controller/cgi_process.rb
@@ -1,11 +1,6 @@
require 'action_controller/cgi_ext/cgi_ext'
require 'action_controller/cgi_ext/cookie_performance_fix'
require 'action_controller/cgi_ext/raw_post_data_fix'
-require 'action_controller/session/drb_store'
-require 'action_controller/session/mem_cache_store'
-if Object.const_defined?(:ActiveRecord)
- require 'action_controller/session/active_record_store'
-end
module ActionController #:nodoc:
class Base
View
14 actionpack/lib/action_controller/session.rb
@@ -1,14 +0,0 @@
-module ActionController #:nodoc:
- module Session #:nodoc:
- def self.append_features(base) #:nodoc:
- super #:nodoc:
- base.after_filter(:clear_persistant_model_associations)
- end
-
- private
- def clear_persistant_model_associations #:doc:
- session = @session.instance_variable_get("@data")
- session.each { |key, obj| obj.clear_association_cache if obj.respond_to?(:clear_association_cache) } if session
- end
- end
-end
View
21 actionpack/lib/action_controller/session_management.rb
@@ -1,12 +1,17 @@
+require 'action_controller/session/drb_store'
+require 'action_controller/session/mem_cache_store'
+if Object.const_defined?(:ActiveRecord)
+ require 'action_controller/session/active_record_store'
+end
+
module ActionController #:nodoc:
module SessionManagement #:nodoc:
def self.append_features(base)
super
base.extend(ClassMethods)
- base.class_eval do
- alias_method :process_without_session_management_support, :process
- alias_method :process, :process_with_session_management_support
- end
+ base.send(:alias_method, :process_without_session_management_support, :process)
+ base.send(:alias_method, :process, :process_with_session_management_support)
+ base.after_filter(:clear_persistant_model_associations)
end
module ClassMethods
@@ -15,7 +20,7 @@ module ClassMethods
# :mem_cache_store, or :memory_store) or use your own class.
def session_store=(store)
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] =
- store.is_a?(Symbol) ? CGI::Session.const_get(store.to_s.camelize) : store
+ store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store
end
# Returns the session store class currently used.
@@ -100,5 +105,11 @@ def process_with_session_management_support(request, response, method = :perform
request.session_options = self.class.session_options_for(request, action)
process_without_session_management_support(request, response, method, *arguments)
end
+
+ private
+ def clear_persistant_model_associations #:doc:
+ session = @session.instance_variable_get("@data")
+ session.each { |key, obj| obj.clear_association_cache if obj.respond_to?(:clear_association_cache) } if session
+ end
end
end
View
45 actionpack/test/controller/fragment_store_setting_test.rb
@@ -0,0 +1,45 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+MemCache = Struct.new(:MemCache, :address)
+
+class FragmentCacheStoreSettingTest < Test::Unit::TestCase
+ def teardown
+ ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::MemoryStore.new
+ end
+
+ def test_file_fragment_cache_store
+ ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
+ assert_kind_of(
+ ActionController::Caching::Fragments::FileStore,
+ ActionController::Base.fragment_cache_store
+ )
+ assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
+ end
+
+ def test_drb_fragment_cache_store
+ ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192"
+ assert_kind_of(
+ ActionController::Caching::Fragments::DRbStore,
+ ActionController::Base.fragment_cache_store
+ )
+ assert_equal "druby://localhost:9192", ActionController::Base.fragment_cache_store.address
+ end
+
+ def test_mem_cache_fragment_cache_store
+ ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"
+ assert_kind_of(
+ ActionController::Caching::Fragments::MemCacheStore,
+ ActionController::Base.fragment_cache_store
+ )
+ assert_equal "localhost", ActionController::Base.fragment_cache_store.address
+ end
+
+ def test_object_assigned_fragment_cache_store
+ ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::FileStore.new("/path/to/cache/directory")
+ assert_kind_of(
+ ActionController::Caching::Fragments::FileStore,
+ ActionController::Base.fragment_cache_store
+ )
+ assert_equal "/path/to/cache/directory", ActionController::Base.fragment_cache_store.cache_path
+ end
+end
View
10 actionpack/test/controller/session_management_test.rb
@@ -81,4 +81,14 @@ def test_session_off_with_if
get :conditional, :ws => "ws"
assert_equal false, @request.session_options
end
+
+ def test_session_store_setting
+ ActionController::Base.session_store = :drb_store
+ assert_equal CGI::Session::DRbStore, ActionController::Base.session_store
+
+ if Object.const_defined?(:ActiveRecord)
+ ActionController::Base.session_store = :active_record_store
+ assert_equal CGI::Session::ActiveRecordStore, ActionController::Base.session_store
+ end
+ end
end

0 comments on commit ebb7bd7

Please sign in to comment.
Something went wrong with that request. Please try again.