Skip to content

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

Notifications You must be signed in to change notification settings

vlado/rails_temporary_data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  
  def update_data
    tmp_data = get_tmp_data("some_key").data
    # do something with tmp data
    tmp_data[:first_name] = "Max" # => Max
    # and then save changes (update tmp data)
    update_tmp_data("some_key", tmp_data) # => { first_name: "Max", last_name: "Cingel", bio: "Very ... very long bio" }
    ...
  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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published