From 56e3e2fde5f2b824df37fabc55db34f4ff337ebb Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 13 Nov 2006 18:59:01 +0000 Subject: Always clear model associations from session. Closes #4795. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5512 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../lib/action_controller/session/drb_store.rb | 4 ++ .../action_controller/session/mem_cache_store.rb | 4 ++ .../lib/action_controller/session_management.rb | 6 +-- actionpack/lib/action_controller/test_process.rb | 33 +++++++++----- .../test/controller/session_management_test.rb | 51 ++++++++++++++++++++++ 6 files changed, 87 insertions(+), 13 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 54335ee107..63e6741576 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Always clear model associations from session. #4795 [sd@notso.net, andylien@gmail.com] + * Update to Prototype 1.5.0_rc2. [Sam Stephenson] * Remove JavaScriptLiteral in favor of ActiveSupport::JSON::Variable. [Sam Stephenson] diff --git a/actionpack/lib/action_controller/session/drb_store.rb b/actionpack/lib/action_controller/session/drb_store.rb index 8ea23e8fff..4feb2636e7 100644 --- a/actionpack/lib/action_controller/session/drb_store.rb +++ b/actionpack/lib/action_controller/session/drb_store.rb @@ -26,6 +26,10 @@ class CGI #:nodoc:all def delete @@session_data.delete(@session_id) end + + def data + @@session_data[@session_id] + end end end end diff --git a/actionpack/lib/action_controller/session/mem_cache_store.rb b/actionpack/lib/action_controller/session/mem_cache_store.rb index a7076fcd5d..e62c0ef9cd 100644 --- a/actionpack/lib/action_controller/session/mem_cache_store.rb +++ b/actionpack/lib/action_controller/session/mem_cache_store.rb @@ -93,6 +93,10 @@ begin end @session_data = {} end + + def data + @session_data + end end end end diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb index ad1b530d6f..60b0cd5f94 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -120,16 +120,16 @@ module ActionController #:nodoc: end def process_cleanup_with_session_management_support - process_cleanup_without_session_management_support clear_persistent_model_associations + process_cleanup_without_session_management_support end # Clear cached associations in session data so they don't overflow # the database field. Only applies to ActiveRecordStore since there # is not a standard way to iterate over session data. def clear_persistent_model_associations #:doc: - if defined?(@_session) && @_session.instance_variables.include?('@data') - session_data = @_session.instance_variable_get('@data') + if defined?(@_session) && @_session.respond_to?(:data) + session_data = @_session.data if session_data && session_data.respond_to?(:each_value) session_data.each_value do |obj| diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index bfd2abbb48..f581f6be35 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -38,7 +38,7 @@ module ActionController #:nodoc: def reset_session @session = TestSession.new - end + end def raw_post if raw_post = env['RAW_POST_DATA'] @@ -275,27 +275,40 @@ module ActionController #:nodoc: end class TestSession #:nodoc: - def initialize(attributes = {}) + attr_accessor :session_id + + def initialize(attributes = nil) + @session_id = '' @attributes = attributes + @saved_attributes = nil + end + + def data + @attributes ||= @saved_attributes || {} end def [](key) - @attributes[key] + data[key] end def []=(key, value) - @attributes[key] = value + data[key] = value end - def session_id - "" + def update + @saved_attributes = @attributes end - def update() end - def close() end - def delete() @attributes = {} end + def delete + @attributes = nil + end + + def close + update + delete + end end - + # Essentially generates a modified Tempfile object similar to the object # you'd get from the standard library CGI module in a multipart # request. This means you can use an ActionController::TestUploadedFile diff --git a/actionpack/test/controller/session_management_test.rb b/actionpack/test/controller/session_management_test.rb index c611cb8af5..6e100c0c37 100644 --- a/actionpack/test/controller/session_management_test.rb +++ b/actionpack/test/controller/session_management_test.rb @@ -44,6 +44,49 @@ class SessionManagementTest < Test::Unit::TestCase end end + class AssociationCachingTestController < ActionController::Base + class ObjectWithAssociationCache + def initialize + @cached_associations = false + end + + def fetch_associations + @cached_associations = true + end + + def clear_association_cache + @cached_associations = false + end + + def has_cached_associations? + @cached_associations + end + end + + def show + session[:object] = ObjectWithAssociationCache.new + session[:object].fetch_associations + if session[:object].has_cached_associations? + render :text => "has cached associations" + else + render :text => "does not have cached associations" + end + end + + def tell + if session[:object] + if session[:object].has_cached_associations? + render :text => "has cached associations" + else + render :text => "does not have cached associations" + end + else + render :text => "there is no object" + end + end + end + + def setup @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new @@ -91,4 +134,12 @@ class SessionManagementTest < Test::Unit::TestCase assert_equal CGI::Session::ActiveRecordStore, ActionController::Base.session_store end end + + def test_process_cleanup_with_session_management_support + @controller = AssociationCachingTestController.new + get :show + assert_equal "has cached associations", @response.body + get :tell + assert_equal "does not have cached associations", @response.body + end end -- cgit v1.2.3