Created
December 11, 2013 07:44
-
-
Save hamzakc/7906468 to your computer and use it in GitHub Desktop.
A different way to write a service object. Made in response to http://re-factor.com/blog/2013/09/27/slow-tests-are-the-symptom-not-the-cause/
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 AddsUserToList | |
attr_reader :notifies_user_klass, :user_klass | |
def initialize(user_klass = User, notifies_user_klass = NotifiesUser) | |
@user_klass = user_klass | |
@notifies_user_klass = notifies_user_klass | |
end | |
def add(params) | |
user_klass.find_by_username!(params.fetch(:username).tap do |user| | |
notifies_user_klass.(user, params.fetch(:mailing_list_name)) | |
user.add_to_mailing_list(params.fetch(:mailing_list_name)) | |
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 MailingListsController < ApplicationController | |
respond_to :json | |
def add_user | |
user = AddsUserToList.new.add(username: params[:username], mailing_list_name: 'blog_list') | |
respond_with user | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment