Sessions within a session indexed by a key
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
features Added Appraisal and got working on Rails 3.1. Apr 1, 2012
gemfiles
lib Bumped version to 0.2.0 for new gem release. Apr 2, 2012
spec Added Appraisal and got working on Rails 3.1. Apr 1, 2012
.gitignore Fixed issues with missing development dependencies and Ruby 1.9.2 loa… Apr 1, 2012
.rspec
.travis.yml
Appraisals Added Appraisal and got working on Rails 3.1. Apr 1, 2012
Gemfile Added Gemfile pointing to dependencies in gemspec. Dec 19, 2010
Gemfile.lock Added version dependency restriction or rails less than 3.2.0. Apr 2, 2012
LICENSE Initial gem with readme and some architecture. Oct 19, 2010
README.rdoc Added note about no rails 3.2 support. Apr 1, 2012
Rakefile Added Appraisal and got working on Rails 3.1. Apr 1, 2012
micro_sessions.gemspec Added version dependency restriction or rails less than 3.2.0. Apr 2, 2012

README.rdoc

Build Status

Micro Sessions

In terms of passing data amongst HTTP requests there are really two options:

  • Param or form data passing

  • Storing in cookies

  • Storing in server-side session, keyed off an ID stored as a cookie.

For a certain application where you're using number 3, this comes at a cost - the session is “global”. In other words, in a single browser window with multiple tabs, the server side cannot distinguish between individual “sessions” within that browser session. This gem introduces a fourth, more granular method:

  • Storing in server-side session, keyed off an ID stored as a cookie and keyed off a request parameter.

This could be useful in instances such as a very complex wizard where transmitting the entire history of data RESTfully via param passing may not be feasible, yet you'd still like to support multiple tabs within the same browser window separately.

Installation

This gem requires ActionPack and ActiveSupport 3.0 or greater.

Include the gem in your Gemfile:

gem "micro_sessions"

NOTE: This gem currently only works with rails 3.0 and rails 3.1, but not rails 3.2, due to an issue with overriding url_for. If you want to help with this, see issue #1.

Usage

Simply include the middleware in your controller:

class ApplicationController
  micro_sessions
end

From there you will have access to a micro_session object in your controllers and views, just like the session object.

def new
  micro_session[:key] = "Value"
end

By default the name of the request parameter used to differentiate micro-sessions is “_msid”, though this can be configured:

class ApplicationController
  micro_sessions :key => "_msid"
end

By default the key generated will be a 10-digit random hash, though this can be configured as well:

class ApplicationController
  micro_sessions :type => :integer, :length => 10
end

Valid values for :type: are:

  • :random_hash

  • :integer (simply increments from one)

  • :random_integer (random integer with a maximum of :length digits)

Session data is accessible to the controller and view through the micro_session object, which works identically to the session object but it specific to the micro-session:

micro_session[:key] = "Value"
micro_session[:key] # => "Value"

When transitioning between pages for which you'd like to keep the micro-session alive, the request parameter must be passed along with any link, form, or redirect. The preferred way is to make use of the new :micro_session option on available on the url_for helper, which will accept either a specific value or just “true” to use the “current” micro-session to this request:

link_to user_path(@user, :micro_session => true)

Note that depending on the context in which you're producing this URL, it may not work correctly. Unfortunately the low-level url_for call in the Rails routing engine does not have access to the controller object (and hence the micro_session object), so I can't make this work outside of simple ActionView helpers. You can always explicitly include the URL options by calling:

micro_session.url_options  # => {"_msid" => "0123456789"}

Can be used something like this:

redirect_to user_path(@user, micro_session.url_options)

If you wish to transmit the identifier via a form, be sure to include the hidden field:

<% form_for(@user) do |f| %>
  <%= micro_session_hidden_field %>
  ...
<% end %>