Skip to content

Instantly share code, notes, and snippets.

@christopherstyles
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save christopherstyles/789a671eef082e8eb2c0 to your computer and use it in GitHub Desktop.

Select an option

Save christopherstyles/789a671eef082e8eb2c0 to your computer and use it in GitHub Desktop.
Service objects, verb-first
# Illustrate the usage of service objects named in the verb-first form.
# a test user
user = User.where(name: 'Oleg').first
SubscribeUserToList.call(user)
# => "Oleg was added to the mailing list."
UnsubscribeUserFromList.call(user)
# => "Oleg was removed from the mailing list."
SendGoodbyeEmail.call(user)
# => "Sent a goodbye email to Oleg."
RemoveUserComments.call(user)
# => "Removed comments by Oleg."
ArchiveInactiveUsers.call([user])
# => "Oleg was removed from the mailing list."
# => "Sent a goodbye email to Oleg."
# => "Removed comments by Oleg."
# app/services/archive_inactive_users.rb
class ArchiveInactiveUsers
def self.call(users)
users.each do |user|
UnsubscribeUserFromList.call(user)
SendGoodbyeEmail.call(user)
RemoveUserComments.call(user)
end
end
end
# app/services/remove_user_comments.rb
class RemoveUserComments
def self.call(user)
# user.comments.destroy_all
puts "Removed comments by #{user.name}."
end
end
# app/services/send_goodbye_email.rb
class SendGoodbyeEmail
def self.call(user)
# Notifier.goodbye(user).deliver
puts "Sent a goodbye email to #{user.name}."
end
end
# app/services/subscribe_user_to_list.rb
class SubscribeUserToList
def self.call(user, list = MailingList.new)
# list.add!(user.email)
puts "#{user.name} was added to the #{list.name} list."
end
end
# app/services/unsubscribe_user_from_list.rb
class UnsubscribeUserFromList
def self.call(user, list = MailingList.new)
# list.remove!(user.email)
puts "#{user.name} was removed from the #{list.name} list."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment