Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Created September 11, 2012 14:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyhaines/3699356 to your computer and use it in GitHub Desktop.
Save coreyhaines/3699356 to your computer and use it in GitHub Desktop.
Environment-based initializers in Rails - USE THEM!
# what? why does your production code care what environment it is in? SILLY!
unless Rails.development? || Rails.test?
Slottd::CreatesReservationTimer.for(slot.id)
end
# in code, no more environment check
Slottd::CreatesReservationTimer.for(slot.id)
# in config/development.rb
module Slottd
CreatesReservationTimer = Timers::Null
end
# in config/production.rb
module Slottd
CreatesReservationTimer = Timers::IronWorker
end
@stevenharman
Copy link

I like the way the "production" code ends up, but having the constant defined via initializer would likely surprise me. At least the first time I discovered it. And every time I forgot and re-discovered it. Probably worth it though.

@jbonnet
Copy link

jbonnet commented Sep 12, 2012

...and the 'old_school_use' has the advantage of having it all together, and making reading easier.

jb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment