Project

drafter

0.0
No commit activity in last 3 years
No release in over 3 years
A
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Drafter¶ ↑

This is an experimental gem designed to version ActiveRecord objects with attached CarrierWave uploads.

It’s still in development, so don’t use it unless you’re ready for that.

So far, it can correctly handle the following cases:

  • drafts for new records

  • drafts for saved records

  • drafts for new and saved records, with attached CarrierWave uploads

  • drafts for new and saved records, properly saving drafts for sub-objects in has_many associations, with or without attached CarrierWave uploads.

It requires a bit of change in your controller actions: you always need to check whether you’ve got a persisted instance of your class, or whether you’re inflating it from a draft. You might do it like this (for an :update action, in a Padrino example):

“‘ruby

put :update do
  @page = find_or_inflate_draft_for(Page)
  @page.assign_attributes(params[:page])

  if @page.save_draft
    flash[:notice] = "Page was successfully updated."
    redirect url(:pages, :show, @page.id_hash)
  else
    render 'pages/edit'
  end
end

“‘

@page.id_hash gives back either :id => (the actual persisted page object).to_param or :draft_id => (the draft object).to_param, depending on the current state of the @page object (draft or persisted).

You’d need to add some helper code to your application:

“‘ruby

def find_or_inflate_draft_for(klazz)
  if params[:draft_id]
    Draft.find(params[:draft_id]).inflate
  else
    klazz.find(params[:id]).apply_draft
  end
end

# This one can help you if you've got polymorphic has_many associations
# in your application. Your foreign key scheme may be a little different
# than this, adapt as necessary.
#
def find_or_inflate_draft_as(klazz, as)
  if params["draft_#{as}_id"]
    Draft.find(params["draft_#{as}_id"]).inflate
  else
    klazz.find(params["#{as}_id"]).apply_draft
  end
end

“‘

Contributing to drafter¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2012 Dave Hrycyszyn. See LICENSE.txt for further details.