Skip to content

Instantly share code, notes, and snippets.

@jsuchal
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save jsuchal/9024136 to your computer and use it in GitHub Desktop.

Select an option

Save jsuchal/9024136 to your computer and use it in GitHub Desktop.
class DonationsController # < ActionController::Base
def create
donation_service.add_donation(current_user, params, bus.on(
donation_created: ->(donation) { send_user_to_payment_gateway(donation) },
donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
))
end
private
def donation_service
DonationService.new # inject dependencies
end
def send_user_to_payment_gateway(donation)
#...
end
end
class DonationsController # < ActionController::Base
def create
build_service.create_donation(current_user, params, bus.attach(self))
end
def donation_created(donation)
send_user_to_payment_gateway(donation)
end
def donation_invalid(donation)
donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
end
private
def send_user_to_payment_gateway(donation)
#...
end
protected
def build_service
DonationService.new # inject dependencies
end
end
class DonationsController # < ActionController::Base
include Bus::ServiceController.new(service: DonationService, event_prefix: :donation, actions: :create)
def donation_created(donation)
send_user_to_payment_gateway(donation)
end
def donation_invalid(donation)
render donation.errors, status: :bad_request
end
private
def send_user_to_payment_gateway(donation)
#...
end
protected
def service_parameters
[current_user, params]
end
end
class DonationsController # < ActionController::Base
include Bus::ServiceController.new(actions: :create) # implicit from controller name
def donation_created(donation)
send_user_to_payment_gateway(donation)
end
def donation_invalid(donation)
render donation.errors, status: :bad_request
end
private
def send_user_to_payment_gateway(donation)
#...
end
protected
def service_parameters
[current_user, params]
end
end
class DonationsController # < ActionController::Base
service_actions :create
def donation_created(donation)
send_user_to_payment_gateway(donation)
end
def donation_invalid(donation)
render donation.errors, status: :bad_request
end
private
def send_user_to_payment_gateway(donation)
#...
end
protected
def service_parameters
[current_user, params]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment