Skip to content

Instantly share code, notes, and snippets.

@hamzakc
Created December 11, 2013 07:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamzakc/7906468 to your computer and use it in GitHub Desktop.
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/
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
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