<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kabisa Blog &#187; Patrick Baselier</title>
	<atom:link href="http://blog.kabisa.nl/author/patrick/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kabisa.nl</link>
	<description>The Ruby on Rails Experts</description>
	<lastBuildDate>Sun, 09 Oct 2011 07:54:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Share sessions between Rails 2 and Rails 3 applications</title>
		<link>http://blog.kabisa.nl/2010/10/27/share-sessions-between-rails-2-and-rails-3-applications/</link>
		<comments>http://blog.kabisa.nl/2010/10/27/share-sessions-between-rails-2-and-rails-3-applications/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 17:44:34 +0000</pubDate>
		<dc:creator>Patrick Baselier</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.kabisa.nl/?p=156</guid>
		<description><![CDATA[This week we started building a Rails 3 application for one of our customers which had to share data with their existing Rails applications, which were built with version 2.1.2 and 2.3.8. Although session configuration differs from version 2 to 3, getting this done wasn&#8217;t such a hard job, mainly thanks to this blogpost written [...]]]></description>
			<content:encoded><![CDATA[<p>This week we started building a Rails 3 application for one of our customers which had to share data with their existing Rails applications, which were built with version 2.1.2 and 2.3.8.</p>
<p>Although session configuration differs from version 2 to 3, getting this done wasn&#8217;t such a hard job, mainly thanks to <a href="http://random-rails.blogspot.com/2010/08/sharing-sessions-between-rails-2-and.html" target="_blank">this blogpost</a> written by Dan McNevin.</p>
<p>Basically this means the sessions are configured as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 2.1.2</span>
<span style="color:#008000; font-style:italic;"># config/environment.rb</span>
...
<span style="color:#6666ff; font-weight:bold;">Rails::Initializer</span>.<span style="color:#9900CC;">run</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:session_key</span>    =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
    <span style="color:#ff3333; font-weight:bold;">:secret</span>         =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'a really long hex string'</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
  config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">session_store</span> = <span style="color:#ff3333; font-weight:bold;">:cookie_store</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_domain</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'.rails.local'</span>
<span style="color:#008000; font-style:italic;"># End of Rails 2.1.2</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rails 2.3.8 (and probably 2.3.x)</span>
<span style="color:#008000; font-style:italic;"># config/initializers/session_store.rb</span>
<span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:domain</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'.rails.local'</span>,
  <span style="color:#ff3333; font-weight:bold;">:key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
  <span style="color:#ff3333; font-weight:bold;">:secret</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'the same really long hex string'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># End of Rails 2.3.8</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
<span style="color:#008000; font-style:italic;"># config/initializers/session_store.rb</span>
<span style="color:#006600; font-weight:bold;">&#91;</span>AppName<span style="color:#006600; font-weight:bold;">&#93;</span>::Application.<span style="color:#9900CC;">config</span>.<span style="color:#9900CC;">session_store</span> <span style="color:#ff3333; font-weight:bold;">:cookie_store</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>
 <span style="color:#ff3333; font-weight:bold;">:key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
 <span style="color:#ff3333; font-weight:bold;">:domain</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'.rails.local'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config/initializers/secret_token.rb</span>
<span style="color:#006600; font-weight:bold;">&#91;</span>AppName<span style="color:#006600; font-weight:bold;">&#93;</span>::Application.<span style="color:#9900CC;">config</span>.<span style="color:#9900CC;">secret_token</span> = <span style="color:#996600;">'the same really long hex string'</span>
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>Session sharing between 2.1.2 and 2.3.8 worked fine, however when swapping to the 3.0.1 application I got the error:</p>
<blockquote><p>ActionDispatch::Session::SessionRestoreError (Session contains objects whose class definition isn&#8217;t available.<br />
Remember to require the classes for all objects kept in the session.<br />
(Original exception: uninitialized constant ActionController::Flash::FlashHash [NameError])<br />
):</p></blockquote>
<p>In other words (or actually, my own words): the session contains an object (<strong>ActionController::Flash::FlashHash</strong>) which is unfamiliar to Rails 3.</p>
<p>To solve this problem, I added the class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
<span style="color:#008000; font-style:italic;"># config/initializers/session_store.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> ActionController
  <span style="color:#9966CC; font-weight:bold;">module</span> Flash
    <span style="color:#9966CC; font-weight:bold;">class</span> FlashHash <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#CC00FF; font-weight:bold;">Hash</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>m, <span style="color:#006600; font-weight:bold;">*</span>a, <span style="color:#006600; font-weight:bold;">&amp;</span>amp;b<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>Now, the error didn&#8217;t show up anymore and so was the session&#8230; I <em>was</em> able to switch from Rails 2 to Rails 3, but now the session didn&#8217;t contain a single keys!?!?<br />
Assuming there is a <strong>require_user</strong> method doing the authentication, I added a</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
<span style="color:#008000; font-style:italic;"># app/controllers/application_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> require_user
    y request
  ...
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>to this controller action (which is short for <em>puts request.to_yaml</em>) and I was surprised to find the keys, which were stored by the Rails 2 app., in the <strong>env</strong> object in it&#8217;s <strong>action_dispatch.request.unsigned_session_cookie</strong> key:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
  y request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'action_dispatch.request.unsigned_session_cookie'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#008000; font-style:italic;"># =&amp;gt; ---</span>
      serial: <span style="color:#006666;">0</span>
      _csrf_token: nTHGmUfA0sKh1rDZWvt<span style="color:#006600; font-weight:bold;">+</span>1tLZmG3fCWlhf8pkiHGMU5I=
      last_quote_time_en_US: !timestamp
        at: <span style="color:#996600;">&quot;2010-10-27 14:52:15.736034 +02:00&quot;</span>
        <span style="color:#996600;">&quot;@marshal_with_utc_coercion&quot;</span>: <span style="color:#0000FF; font-weight:bold;">true</span>
      session_id: ef4c356efc12113792ecccbde65bba7a
      user_id: <span style="color:#006666;">35</span>
      lang: en_US
      shown_quotes_en_US:
      <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">17688</span>
      ...
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>Pretty hopeless by now, I decided to get the keys I needed out of this Hash and add them to the Rails 3 session manually:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
  session<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user_id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'action_dispatch.request.unsigned_session_cookie'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'user_id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>But this surprised me even more, finding a fully populated session only after adding one key.</p>
<p>Time to investigate the <strong>actionpack</strong> gem.<br />
When you add a key to the session object, this will call the <strong>[]=</strong>-method in the <strong>ActionDispatch::Session::SessionHash</strong> class. The <strong>[]=</strong>-method internally calls a private method <strong>load_for_write!</strong>. My thinking was (since diving deeper into the code didn&#8217;t come to my mind) that the Rails 3 session is fully populated, but not yet loaded when going or returning to the Rails 3 application.</p>
<p>This was an easy one, I just had to reload the session before using it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
<span style="color:#008000; font-style:italic;"># app/controllers/application_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> require_user
    session.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:load_for_write</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
  ...
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>Problem solved? Well, not completely. I was able to successfully browse from the Rails 2 app. to Rails 3, without losing my session, but going back to the Rails 2 app. introduced another problem: keys initially stored as symbols were now turned into string because of the Rails 3 app.</p>
<p>Initially, I tried to solve this by converting all keys back into symbols, but this should introduce another problem, since I was not sure if all session keys were stored as symbols. Rails itself stores the Flash object in the session into the &#8220;flash&#8221; key, instead of :flash.</p>
<p>A better approach is to patch the CGI::Session object and make sure all keys can be stored and retrieved as both string and symbols:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 2.1.2 and 2.3.x</span>
<span style="color:#008000; font-style:italic;"># config/initializers/load_patches.rb</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># Loads patches stored in lib/patches.</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>RAILS_ROOT <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/lib/patches/**/*.rb&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">require</span> file <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># lib/patches/cgi/session.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'cgi/session'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Patching CGI:Session so that it on longer matters if you retrieve a session</span>
<span style="color:#008000; font-style:italic;"># value using a String, Symbol, ... This in order to make it play nicely</span>
<span style="color:#008000; font-style:italic;"># together with Rails 3.</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># = Examples</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;">#  session[:foo] = &quot;Bar&quot;</span>
<span style="color:#008000; font-style:italic;">#  session[:foo] # =&amp;gt; &quot;Bar&quot;</span>
<span style="color:#008000; font-style:italic;">#  session[&quot;foo&quot;] # =&amp;gt; &quot;Bar&quot;</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;">#  session[&quot;qux&quot;] = &quot;Baz&quot;</span>
<span style="color:#008000; font-style:italic;">#  session[:qux] # =&amp;gt; &quot;Baz&quot;</span>
<span style="color:#008000; font-style:italic;">#  session[&quot;qux&quot;] # =&amp;gt; &quot;Baz&quot;</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">CGI</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> Session <span style="color:#008000; font-style:italic;">#:nodoc:</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, val<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@write_lock</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0000FF; font-weight:bold;">true</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span> = val
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># End of Rails 2.1.2 and 2.3.x</span></pre></div></div>

<p>Now it worked! I am able to share sessions between Rails 2.1, 2.3.x and Rails 3.0 application.</p>
<p>To wrap things up:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails 2.1.2</span>
<span style="color:#008000; font-style:italic;"># config/environment.rb</span>
...
<span style="color:#6666ff; font-weight:bold;">Rails::Initializer</span>.<span style="color:#9900CC;">run</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:session_key</span>    =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
    <span style="color:#ff3333; font-weight:bold;">:secret</span>         =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'a really long hex string'</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
  config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">session_store</span> = <span style="color:#ff3333; font-weight:bold;">:cookie_store</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_domain</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'.rails.local'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config/initializers/load_patches.rb</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>RAILS_ROOT <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/lib/patches/**/*.rb&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">require</span> file <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># lib/patches/cgi/session.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'cgi/session'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">CGI</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> Session <span style="color:#008000; font-style:italic;">#:nodoc:</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, val<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@write_lock</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0000FF; font-weight:bold;">true</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span> = val
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># End of Rails 2.1.2</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rails 2.3.8 (and probably 2.3.x)</span>
<span style="color:#008000; font-style:italic;"># config/initializers/session_store.rb</span>
<span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:domain</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'.rails.local'</span>,
  <span style="color:#ff3333; font-weight:bold;">:key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
  <span style="color:#ff3333; font-weight:bold;">:secret</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'the same really long hex string'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config/initializers/load_patches.rb</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>RAILS_ROOT <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/lib/patches/**/*.rb&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">require</span> file <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># lib/patches/cgi/session.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'cgi/session'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">CGI</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> Session <span style="color:#008000; font-style:italic;">#:nodoc:</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, val<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@write_lock</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0000FF; font-weight:bold;">true</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0066ff; font-weight:bold;">@dbman</span>.<span style="color:#9900CC;">restore</span>
      <span style="color:#0066ff; font-weight:bold;">@data</span><span style="color:#006600; font-weight:bold;">&#91;</span>key.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span> = val
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># End of Rails 2.3.8</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rails 3.0.1</span>
<span style="color:#008000; font-style:italic;"># config/initializers/session_store.rb</span>
<span style="color:#006600; font-weight:bold;">&#91;</span>AppName<span style="color:#006600; font-weight:bold;">&#93;</span>::Application.<span style="color:#9900CC;">config</span>.<span style="color:#9900CC;">session_store</span> <span style="color:#ff3333; font-weight:bold;">:cookie_store</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>
 <span style="color:#ff3333; font-weight:bold;">:key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'_sso_session'</span>,
 <span style="color:#ff3333; font-weight:bold;">:domain</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'.rails.local'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> ActionController
  <span style="color:#9966CC; font-weight:bold;">module</span> Flash
    <span style="color:#9966CC; font-weight:bold;">class</span> FlashHash <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#CC00FF; font-weight:bold;">Hash</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>m, <span style="color:#006600; font-weight:bold;">*</span>a, <span style="color:#006600; font-weight:bold;">&amp;</span>amp;b<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config/initializers/secret_token.rb</span>
<span style="color:#006600; font-weight:bold;">&#91;</span>AppName<span style="color:#006600; font-weight:bold;">&#93;</span>::Application.<span style="color:#9900CC;">config</span>.<span style="color:#9900CC;">secret_token</span> = <span style="color:#996600;">'the same really long hex string'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># app/controllers/application_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> require_user
    session.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:load_for_write</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
  ...
<span style="color:#008000; font-style:italic;"># End of Rails 3.0.1</span></pre></div></div>

<p>Next job is to upgrade the legacy code to Rails 3&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kabisa.nl/2010/10/27/share-sessions-between-rails-2-and-rails-3-applications/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Running cucumber features with sunspot_rails</title>
		<link>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/</link>
		<comments>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 07:57:15 +0000</pubDate>
		<dc:creator>Patrick Baselier</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.kabisa.nl/?p=75</guid>
		<description><![CDATA[We wanted to integrate the sunspot_rails gem into our cucumber features. I found a ticket where mat adviced to call Sunspot::Rails::Server.start / stop in one of the available cucumber hooks. Code found in features/support/env.rb is run when Cucumber begins and exits so this seemed the right place to start Solr: Sunspot::Rails::Server.new.start &#160; at_exit do Sunspot::Rails::Server.new.stop [...]]]></description>
			<content:encoded><![CDATA[<p>We wanted to integrate the <a href="http://github.com/outoftime/sunspot" target="_blank">sunspot_rails gem</a> into our cucumber features. I found a <a href="http://outoftime.lighthouseapp.com/projects/20339/tickets/59-cucumber-testing-and-solr_rails" target="_blank">ticket</a> where <a href="http://outoftime.lighthouseapp.com/users/24899" target="_blank">mat</a> adviced to call <strong>Sunspot::Rails::Server.start / stop</strong> in one of the available cucumber hooks.</p>
<p>Code found in <strong>features/support/env.rb</strong> is run when Cucumber begins and exits so this seemed the right place to start Solr:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">at_exit</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">stop</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Also, make sure you&#8217;ve added a cucumber environment to the <strong>config/sunspot.yml</strong> file; we copied the test-environment for that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">test: <span style="color:#006600; font-weight:bold;">&amp;</span>amp;TEST
  solr:
    hostname: localhost
    port: <span style="color:#006666;">8981</span>
    log_level: WARNING
&nbsp;
cucumber:
  <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt;: <span style="color:#006600; font-weight:bold;">*</span>TEST</pre></div></div>

<p>Running cucumber will let all scenarios fail unfortunately and we got a &#8216;<strong>Connection refused &#8211; connect(2) (RSolr::RequestError)</strong>&#8216; in our console.</p>
<p>The reason for this is simple (after I stepped into the sunspot_rails gem to be honest): the <strong>Sunspot::Rails::Server.new.start</strong> method will eventually launch a Java Servlet Container (Jetty) with the Solr WAR and uses the port defined in your <strong>sunspot.yml</strong>.<br />
Now, starting up a server could take some time, so if cucumber&#8217;s features are run before the Solr server is up and running, the &#8216;Connection refused&#8217; sounds trivial then.<br />
First I added a sleep after starting up the server:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
<span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>and this will work for most cases, but I thought it would be better to let cucumber wait just long enough until the server is up and running. When the server is up, you can open up your browser and enter <em>http://localhost:8981/solr</em> to administering Solr. This we can use to find out if we&#8217;ve waited long enough and start our features. I&#8217;ve put this into a seperate class named <strong>CukeSunspot</strong> and created this file in the <strong>features/support</strong> directory:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;net/http&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CukeSunspot
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@server</span> = <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> start
    <span style="color:#0066ff; font-weight:bold;">@started</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">start</span>
    up
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
  <span style="color:#9966CC; font-weight:bold;">def</span> port
    <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">port</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> uri
    <span style="color:#996600;">&quot;http://0.0.0.0:#{port}/solr/&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> up
    <span style="color:#9966CC; font-weight:bold;">while</span> starting
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Sunspot server is starting...&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Sunspot server took #{'%.2f' % (Time.now - @started)} sec. to get up and running. Let's cuke!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> starting
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      request = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ECONNREFUSED
      <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we can the alternative start method and remove the explicit sleep statement in our global before hook. <strong>features/support/env.rb</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">CukeSunspot.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">start</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">at_exit</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">Sunspot::Rails::Server</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">stop</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And voila, if you run cucumber, you scenario&#8217;s should run successfully. On my machine:</p>
<pre>Sunspot server is starting...
Sunspot server is starting...
Sunspot server took 3.74 sec. to get up and running. Let's cuke!</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

