From 22d9bad8a038eef751e1270f204211cca3c8af18 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 20 Oct 2005 21:59:48 +0000 Subject: [PATCH] Expose the session model backing CGI::Session git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2696 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 4 ++-- .../session/active_record_store.rb | 15 +++++++++++++++ .../test/controller/active_record_store_test.rb | 16 +++++++++++----- actionpack/test/controller/base_test.rb | 8 ++++++-- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index c7cac84206..319806725d 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Expose the session model backing CGI::Session + * Abbreviate RAILS_ROOT in traces diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 4cb8c11588..067dcacca6 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -4,6 +4,7 @@ require 'action_controller/routing' require 'action_controller/code_generation' require 'action_controller/url_rewriter' require 'drb' +require 'set' module ActionController #:nodoc: class ActionControllerError < StandardError #:nodoc: @@ -846,8 +847,7 @@ module ActionController #:nodoc: end def self.action_methods - #puts "action method: #{public_instance_methods.inspect}" - @action_methods ||= (public_instance_methods - hidden_actions).inject({}) { |h, k| h[k] = true; h } + @action_methods ||= Set.new(public_instance_methods - hidden_actions) end def add_variables_to_assigns diff --git a/actionpack/lib/action_controller/session/active_record_store.rb b/actionpack/lib/action_controller/session/active_record_store.rb index a653947038..90a84511be 100644 --- a/actionpack/lib/action_controller/session/active_record_store.rb +++ b/actionpack/lib/action_controller/session/active_record_store.rb @@ -5,6 +5,16 @@ require 'base64' class CGI class Session + # Return this session's underlying Session model. Useful for the DB-backed session stores. + def model + @dbman.model rescue nil + end + + # Proxy missing methods to the underlying Session model. + def method_missing(method, *args, &block) + if model then model.send(method, *args, &block) else super end + end + # A session store backed by an Active Record class. # # A default class is provided, but any object duck-typing to an Active @@ -277,6 +287,11 @@ class CGI end end + # Access the underlying session model. + def model + @session + end + # Restore session state. The session model handles unmarshaling. def restore if @session diff --git a/actionpack/test/controller/active_record_store_test.rb b/actionpack/test/controller/active_record_store_test.rb index be7f911e70..f10bbf7434 100644 --- a/actionpack/test/controller/active_record_store_test.rb +++ b/actionpack/test/controller/active_record_store_test.rb @@ -53,7 +53,6 @@ module CommonActiveRecordStoreTests @new_session.close end end - end class ActiveRecordStoreTest < Test::Unit::TestCase @@ -73,13 +72,17 @@ class ActiveRecordStoreTest < Test::Unit::TestCase @new_session['foo'] = 'bar' end + def test_model_attribute + assert_kind_of CGI::Session::ActiveRecordStore::Session, @new_session.model + assert_equal @new_session.model.data, @new_session.data + end + def teardown session_class.drop_table! end end class ColumnLimitTest < Test::Unit::TestCase - def setup @session_class = CGI::Session::ActiveRecordStore::Session @session_class.create_table! @@ -97,10 +100,8 @@ class ColumnLimitTest < Test::Unit::TestCase s.data assert_raises(ActionController::SessionOverflowError) { s.save } end - end - class DeprecatedActiveRecordStoreTest < ActiveRecordStoreTest def setup session_class.connection.execute 'create table old_sessions (id integer primary key, sessid text unique, data text)' @@ -128,12 +129,17 @@ class SqlBypassActiveRecordStoreTest < ActiveRecordStoreTest end @session_class end + + def test_model_attribute + assert_kind_of CGI::Session::ActiveRecordStore::SqlBypass, @new_session.model + assert_equal @new_session.model.data, @new_session.data + end end # End of safety net. rescue Object => e - $stderr.puts "Skipping CGI::Session::ActiveRecordStore tests: #{e}" + $stderr.puts "Skipping CGI::Session::ActiveRecordStore tests: #{e}" #$stderr.puts " #{e.backtrace.join("\n ")}" end end diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 664c4de93b..9687aa2748 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -67,7 +67,11 @@ class ControllerInstanceTests < Test::Unit::TestCase end def test_action_methods - @empty_controllers.each {|c| assert_equal({}, c.send(:action_methods), "#{c.class.controller_path} should be empty!")} - @non_empty_controllers.each {|c| assert_equal({"public_action"=>true}, c.send(:action_methods), "#{c.class.controller_path} should not be empty!")} + @empty_controllers.each do |c| + assert_equal Set.new, c.send(:action_methods), "#{c.class.controller_path} should be empty!" + end + @non_empty_controllers.each do |c| + assert_equal Set.new('public_action'), c.send(:action_methods), "#{c.class.controller_path} should not be empty!" + end end end