Skip to content

Instantly share code, notes, and snippets.

@jeffrydegrande
Created October 3, 2011 22:56
Show Gist options
  • Save jeffrydegrande/1260478 to your computer and use it in GitHub Desktop.
Save jeffrydegrande/1260478 to your computer and use it in GitHub Desktop.
# my apologies for the stupid example but ..
# rather than the usual something like this:
class Nubble
before_save :prepare_something, :if => :not_prepared_already
after_create :do_something_with_the_nubble, :if => :something_else_happens_to_be_true
def prepare_something
# do something lengthy
self.status = :prepared
end
def do_something_with_the_nubble
# do something else that takes a long time
end
def not_prepared_already
self.status == :prepared
end
def something_else_happens_to_be_true
#@#$%^&*I(OP{
end
end
# I tend to prefer something like this:
class CreatesNubbles
def create
@nubble = Nubble.new
prepare
@nubble.save!
do_something_with_the_nubble
end
def prepare
unless @nubble.prepared?
# do something lengthy
@nubble.set_status :prepared
end
end
def something_with_the_nubble
if something_else_happens_to_be_true
# do something else that's friggin lengthy
end
end
end
# I find it both far easier to read & easier to test. And of course I don't need to
# load rails which make tests wicked fast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment