Permalink
Browse files

Always clear model associations from session. Closes #4795.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5512 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a303a16 commit 56e3e2fde5f2b824df37fabc55db34f4ff337ebb @jeremy jeremy committed Nov 13, 2006
View
@@ -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]
@@ -26,6 +26,10 @@ def close
def delete
@@session_data.delete(@session_id)
end
+
+ def data
+ @@session_data[@session_id]
+ end
end
end
end
@@ -93,6 +93,10 @@ def delete
end
@session_data = {}
end
+
+ def data
+ @session_data
+ end
end
end
end
@@ -120,16 +120,16 @@ def set_session_options(request)
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|
@@ -38,7 +38,7 @@ def initialize(query_parameters = nil, request_parameters = nil, session = nil)
def reset_session
@session = TestSession.new
- end
+ end
def raw_post
if raw_post = env['RAW_POST_DATA']
@@ -275,27 +275,40 @@ class TestResponse < AbstractResponse #: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
@@ -44,6 +44,49 @@ def another
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 @@ def test_session_store_setting
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

0 comments on commit 56e3e2f

Please sign in to comment.