@pcreux
Feb 27, 2014
http://vanruby.org
Last active
August 21, 2022 15:32
-
-
Save pcreux/9277929 to your computer and use it in GitHub Desktop.
Gourmet Service Objects - Lightning Talk - http://vanruby.org - Feb 27, 2014
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
_______ _______ _________ _ | |
( ____ ) ( ___ ) \__ __/ ( ( /| | |
| ( )| | ( ) | ) ( | \ ( | | |
| (____)| | (___) | | | | \ | | | |
| _____) | ___ | | | | (\ \) | | |
| ( | ( ) | | | | | \ | | |
| ) | ) ( | ___) (___ | ) \ | | |
|/ |/ \| \_______/ |/ )_) | |
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
# A service responsible for several action | |
class InviteService | |
def self.accept(invite, user) | |
# ... | |
end | |
def self.reject(invite, user) | |
# ... | |
end | |
def self.send(invite, user) | |
# ... | |
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
# A service with an ugly name | |
class InviteAccepter | |
def self.accept(invite, user) | |
# ... | |
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
# An Accept service that accepts | |
class AcceptInvite | |
def self.accept(invite, user) | |
# ... | |
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
# Use generic method! | |
class AcceptInvite | |
def self.call(invite, user) | |
# ... | |
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
# A service can be a Proc! | |
AcceptInvite = Proc.new(invite, user) { # ... } | |
AcceptInvite = lambda { |invite, user| # ... } | |
AcceptInvite = ->(invite, user) { # ... } |
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
# A service that takes advantage of private methods | |
class AcceptInvite | |
def self.call(*args) | |
new(*args).call | |
end | |
def initialize(invite, user) | |
@invite = invite | |
@user = user | |
end | |
def call | |
return true if invite_already_accepted? | |
update_invite and send_notification | |
end | |
private | |
def send_notification | |
# ... | |
end | |
def already_accepted? | |
# ... | |
end | |
def update_invite | |
# ... | |
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
AcceptInvite.call(invite, user, -> { raise "ERROR" }) | |
# A service with dependency injection | |
class AcceptInvite | |
def self.call(*args) | |
new(*args).call | |
end | |
def initialize(invite, user, notifier=AcceptInviteNotifier) | |
@invite = invite | |
@user = user | |
@notifier = notifier | |
end | |
def call | |
return true if invite_already_accepted? | |
update_invite and send_notification | |
end | |
private | |
def send_notification | |
@notifier.call(invite, user) | |
end | |
def already_accepted? | |
# ... | |
end | |
def update_invite | |
# ... | |
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
# Here is how to use it... | |
class InviteController < LoggedInController | |
def accept | |
invite = Invite.find_by_token!(params[:token]) | |
if AcceptInvite.call(invite, current_user)) | |
redirect_to invite.item, notice: "Invite accepted!" | |
else | |
redirect_to '/', alert: invite.errors.full_sentence | |
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
# Experiment: Mind blown... | |
class InviteController < LoggedInController | |
def accept | |
AcceptInvite.call(params[:token], current_user, | |
success: ->(invite) { redirect_to invite.item, notice: "Invite accepted!" }, | |
not_found: ->(invite) { redirect_to '/', alert: 'Invite not found' }, | |
error: ->(invite) { redirect_to '/', alert: invite.errors.full_sentence }, | |
already_accepted: ->(invite) { redirect_to invite.item } | |
) | |
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
RE-USE! |
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
# Reuse in another controller | |
class API::InviteController < APIController | |
def accept | |
AcceptInvite.call(#...) | |
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
# Use by another service | |
class BatchAcceptInvite | |
# ... | |
def call | |
@invites.each do |invite| | |
AcceptInvite.call(invite, user) | |
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
# Use a service to setup test env | |
Given 'the last invite was accepted by "$name"' do |name| | |
AcceptInvite.call(Invite.last, User.find_by_name!(name)) | |
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
# Use from a rake task! | |
desc "Accept all invites" | |
task :wtf do | |
Invite.pending.find_each do |invite| | |
AcceptInvite.call(invite, User.admin.first) | |
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
# Use from the console! | |
$> AcceptInvite.call(missed_invite, angry_customer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment