This is my after_create callback:
after_create { |f| if f.target.class.eql?(Question)
if f.target.user != User.current_user
Notify.create_notify( Notify::QUESTION_FOLLOW, f.target.user, User.current_user, f.target)
end
elsif f.target.class.eql?(User)
Notify.create_notify( Notify::USER_FOLLOW, f.target, User.current_user,f.target) if f.target.can_mail_user(:follower)
end
}
I tried move that to a block so now it looks like below:
after_create do |f|
if f.target.class.eql?(Question)
if f.target.user != User.current_user
Notify.create_notify( Notify::QUESTION_FOLLOW, f.target.user, User.current_user, f.target)
end
elsif f.target.class.eql?(User)
Notify.create_notify( Notify::USER_FOLLOW, f.target, User.current_user,f.target) if f.target.can_mail_user(:follower)
end
end
What else I can do to improve that code?
Most of that should go to
Notify. I assume this is aFollowclass. A useful question to ask yourself in refactoring is ‘does this class need to know about this behavior?’, and I think in this case the answer is no. Try doing something like this:…and the rest of the branching logic goes in Notify, whose job it is to figure out which notification to send.