Created
July 9, 2012 07:37
-
-
Save tansengming/3074833 to your computer and use it in GitHub Desktop.
Ruby configure blocks
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
# How Clearance / Hoptoad does it | |
module Clearance | |
class << self | |
attr_accessor :configuration | |
end | |
def self.configure | |
self.configuration ||= Configuration.new | |
yield(configuration) | |
end | |
class Configuration | |
attr_accessor :mailer_sender | |
def initialize | |
@mailer_sender = 'donotreply@example.com' | |
end | |
end | |
end | |
# to use | |
Clearance.configure do |config| | |
config.mailer_sender = 'donotreply@example.com' | |
end | |
# How Devise does it | |
module Devise | |
mattr_accessor :mailer_sender # requires active support | |
@@mailer_sender = 'donotreply@example.com' | |
class << self | |
def setup | |
yield self | |
end | |
end | |
end | |
# to use | |
Devise.setup do |config| | |
config.mailer_sender = 'donotreply@example.com' | |
end | |
# references: http://robots.thoughtbot.com/post/344833329/mygem-configure-block |
I solved this by moving this to my lib folder, but I wonder if there's a different solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a little problem with this. As soon as I edit any file in my rails project all classes are reloaded and initializers aren't rerun, leaving my config empty.