Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rails engine to simply save temporary data that is too big for session in database

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore ignore dummy app log files April 21, 2012
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 rails_temporary_data.gemspec
README.md

RailsTemporaryData

Gem Version

Rails engine to simply save big temporary data (too big for session cookie store) in a database. It is great for a step-by-step wizard or similar functionality.

Why

While working on Padbase we needed 2 steps signup process (1. enter property info, 2. enter user info). Info entered in first step could get very large and we couldn't save it in a session because of CookieOverflow, didn't want to switch to ActiveRecord store and didn't want to save invalid property in database with the flag (partial validations, ...). Solution was to create separate table for this temporary data and RailsTemporaryData was born.

This way you get best from both worlds. Standard session data is still saved in a cookie and you can save larger amount of data in a database.

Install

Start by adding the gem to your application's Gemfile

gem 'rails_temporary_data'

Update your bundle

bundle install

Generate migration

rails generate rails_temporary_data

Run migration

rake db:migrate

Example

class DummyController < ApplicationController
  include RailsTemporaryData::ControllerHelpers

  def set_data
    set_tmp_data("some_key", { first_name: "Vlado", last_name: "Cingel", bio: "Very ... very long bio" })
    ...
  end

  def get_data
    tmp_data = get_tmp_data("some_key").data
    # do something with tmp data
    first_name = tmp_data[:first_name] # => Vlado
    ...
  end

end

You can optionally set data expiry time (default is 2 days)

class DummyController < ApplicationController
  include RailsTemporaryData::ControllerHelpers

  def set_data
    set_tmp_data("some_key", { bio: "Very ... very long bio" }, Time.now + 3.days)
    ...
  end

end

To clear data you don't need any more

class DummyController < ApplicationController
  include RailsTemporaryData::ControllerHelpers

  def get_data
    tmp_data = get_tmp_data("some_key").data
    # do something with tmp data
    clear_tmp_data("some_key")
  end

end

To help you clear unwanted and/or expired data rake task is provided. You should set a cron job to call this task daily.

rake rails_temporary_data:delete_expired

TODO

  • Default expires_at as configuration option
  • Generate initializer that will make RailsTemporaryData::ControllerHelpers available to all controllers
Something went wrong with that request. Please try again.