Created
February 19, 2012 23:34
-
-
Save libryder/1866494 to your computer and use it in GitHub Desktop.
Simple solution to enable transaction rollbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| around_filter :check_dry_run | |
| def check_dry_run | |
| ActiveRecord::Base.transaction do | |
| yield | |
| raise ActiveRecord::Rollback, "dry_run specified; no changes made" if @dry_run | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SampleController < ApplicationController | |
| def create | |
| if params[:dry_run].blank? | |
| @dry_run = true | |
| else | |
| @dry_run = false | |
| end | |
| @sample = Sample.new params[:sample] | |
| @sample.save | |
| respond_with @sample | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment