Last active
August 29, 2015 14:04
-
-
Save christopherstyles/789a671eef082e8eb2c0 to your computer and use it in GitHub Desktop.
Service objects, verb-first
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
| # 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." |
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
| # 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 |
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
| # app/services/remove_user_comments.rb | |
| class RemoveUserComments | |
| def self.call(user) | |
| # user.comments.destroy_all | |
| puts "Removed comments by #{user.name}." | |
| 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
| # 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 |
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
| # 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 |
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
| # 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