Forked from jsuchal/1-event_subscribers_initializer_or_global_config.rb
Last active
August 29, 2015 13:55
-
-
Save kremso/8729126 to your computer and use it in GitHub Desktop.
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
| bus = On::Bus.new. | |
| register_subscriber(DonationReceipt). | |
| register_subscriber(NotifyAccountingSystem). | |
| register_subscriber(UpdatePublicStats). | |
| register_subscriber(LogInternalMetrics). | |
| register_subscriber(UpdateAnalyticsDashboard). | |
| register_subscriber(RealtimeViewerUpdate) |
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 DonationsController # < ActionController::Base | |
| def create | |
| DonationService.new.add_donation(current_user, params[:donation], bus.register_subscriber(self)) | |
| end | |
| 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 | |
| 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 DonationService | |
| def initialize(options={}) | |
| @donation_class = options.fetch(:donation_class) { Donation } | |
| end | |
| def add_donation(donor, donation_attributes, listener) | |
| donation = @donation_class.new(donation_attributes.merge(donor: donor)) | |
| if donation.valid? | |
| donation.save! | |
| listener.donation_created(donation) | |
| else | |
| listener.donation_invalid(donation) | |
| 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 DonationReceipt | |
| def donation_created(donation) | |
| Mailer.delay.send_donation_receipt(donation) | |
| 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 NotifyAccountingSystem | |
| def donation_created(donation) | |
| #.. | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment