apeckham / save_draft

Like Gmail's "save draft" feature for Rails

This URL has Read+Write access

apeckham (author)
Sat Dec 27 23:50:27 -0800 2008
commit  d197507992119e378ee8a9f8bf52701dd1ee2083
tree    ca945f16e1962e40804f94d907eabd27a67d639a
parent  23f11456a9e2aa41a7115fb64046b053684ea8a6
name age message
file .gitignore Loading commit data...
file MIT-LICENSE
file README
file Rakefile
directory generators/
file init.rb
directory lib/
directory test/
README
This plugin implements Gmail's "Save Draft" feature in Rails.

=== INSTALL ===

Install plugin from github:
  script/install plugin git://github.com/apeckham/save_draft.git

Create a table for saved drafts:
  script/generate CreateDraftsTable
  rake db:migrate
  
Then in your controller:
  class MyController < ActionController::Base
    saves_draft :my_param
    ...
  end

=== WUSSUP ===

When a user POSTs a form, the parameter (my_param, above) is serialized and saved in a new Draft object.

When the user GETs a form later and the parameter is not provided, it's retrieved from the Draft object.

The effect should be that if a user starts filling out a form, saves a draft and returns later, the form is already 
populated.

=== SAVING DRAFTS PERIODICALLY ===

In your controller, make an action like this:

  class MyController < ActionController::Base
    saves_draft :my_param

    def save_draft
      render :text => "Draft saved at #{Time.now}"
    end
    
    ...
  end

If you're using Prototype, you can use PeriodicalExecuter to post the form contents to your save_draft action:

  function save_my_form() {
    var serialized_form = Form.serialize('new_form', true);
    new Ajax.Updater('draft_status', '/my_controller/save_draft', {parameters: serialized_form});
  }
  new PeriodicalExecuter(save_my_form, 30);

=== CLEANING UP ===

When you don't need the draft anymore (for example, on a successful save) delete it by calling delete_draft.

  class MyController < ActionController::Base
    saves_draft :my_param
  
    def save
      @my_model = MyModel.new(params[:my_param])
      delete_draft if @my_model.save!
    end
  end



Copyright (c) 2008 Aaron Peckham, released under the MIT license