Created
July 6, 2012 22:12
-
-
Save mgreenly/3063049 to your computer and use it in GitHub Desktop.
This file contains 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 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 |
This file contains 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 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 |
This file contains 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 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