Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created July 6, 2012 22:12
Show Gist options
  • Save mgreenly/3063049 to your computer and use it in GitHub Desktop.
Save mgreenly/3063049 to your computer and use it in GitHub Desktop.
class WarrantyClaimEventsController < ApplicationController
# . . .
def create
@warranty_claim = WarrantyClaim.find(params[:warranty_claim_id])
WarrantyClaimWorkflow.new(current_user.id, @warranty_claim).send(params[:warranty_claim_event][:name]) do |result|
result.on_success do |message|
flash[:notice] = "warranty claim successfully updated"
redirect_to warranty_claim_path(@warranty_claim)
end
result.on_failure do
render :template => 'warranty_claims/show'
end
end
end
# . . .
end
class WarrantyClaimWorkflow
# . . .
def submit_for_review
raise InvalidWorkflowEvent unless valid_prior_states.include?(@warranty_claim.state)
if validate_ready_to_submit_for_review
WarrantyClaimEvent.transaction do
@warranty_claim.update_attribute(:state, :submitted)
@warranty_claim.warranty_claim_events.create!(:user_id => @user_id, :event => :submit_for_review)
end
yield SuccessResult.new
else
yield FailureResult.new
end
end
def cancel_claim
# . . .
end
def submit_for_credit
# . . .
end
# . . .
end
class WarrantyClaimWorkflow
class InvalidWorkflowEvent < StandardError; end
class SuccessResult
def on_success; yield end
def on_failure; end
end
class FailureResult
def on_success; end
def on_failure; yield end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment