Last active
August 29, 2015 14:08
-
-
Save jaigouk/22d592ac21c1ac2d4416 to your computer and use it in GitHub Desktop.
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
#Let's take a look at the welcome method of the User model. | |
#This method is doing way too many things, | |
#you really need to split it up into separate private methods. | |
#Create the following private methods: | |
#send_welcome_email, enable_welcome_tour, enable_welcome_promotion. | |
#Then move the code from the welcome method into each one. | |
#Make sure to still call each method from within the welcome method. | |
class User < ActiveRecord::Base | |
def welcome | |
# send_welcome_email | |
WelcomeMailer.welcome(self).deliver | |
# enable_welcome_tour | |
self.welcome_tour = true | |
self.save | |
# enable_welcome_promotion | |
promo = Promotion.new(name: "Thanks for joining!") | |
promo.set_redeemer(self) | |
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
class User < ActiveRecord::Base | |
def welcome | |
send_welcome_email | |
enable_welcome_tour | |
enable_welcome_promotion | |
end | |
def send_welcome_email | |
WelcomeMailer.welcome(self).deliver | |
end | |
def enable_welcome_tour | |
self.welcome_tour = true | |
self.save | |
end | |
def enable_welcome_promotion | |
promo = Promotion.new(name: "Thanks for joining!") | |
promo.set_redeemer(self) | |
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
class User < ActiveRecord::Base | |
def welcome | |
UserWelcome.new(self) | |
UserWelcome.welcome | |
end | |
end | |
class UserWelcome | |
attr_accessor :welcome | |
def initialize(user) | |
@user = user | |
end | |
def welcome | |
@user.class.transaction do | |
send_welcome_email | |
enable_welcome_tour | |
enable_welcome_promotion | |
end | |
end | |
private | |
def send_welcome_email | |
WelcomeMailer.welcome(@user).deliver | |
end | |
def enable_welcome_tour | |
@user.welcome_tour = true | |
@user.save | |
end | |
def enable_welcome_promotion | |
promo = Promotion.new(name: "Thanks for joining!") | |
promo.set_redeemer(@user) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What benefit does moving the logic into a UserWelcome class have over the 2nd file? 2nd file seems cleaner in my opinion