Created
February 8, 2010 22:07
-
-
Save rafer/298629 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
class ModelExtensionLoader | |
def self.activate! | |
class << ActiveRecord::Base | |
alias_method :old_inherited, :inherited | |
def inherited(model) | |
puts "Trying to load extensions #{model} extensions" | |
if extension = ModelExtensionLoader.safe_constantize("Extensions::#{model}") | |
model.send(:include, extension) | |
puts "Included instance methods" | |
if class_methods = ModelExtensionLoader.safe_constantize("Extensions::#{model}::ClassMethods") | |
model.send(:extend, class_methods) | |
puts "Extended class methods" | |
end | |
end | |
old_inherited(model) | |
end | |
end | |
end | |
def self.safe_constantize(name) | |
begin | |
return name.constantize | |
rescue NameError => name_error | |
raise name_error unless name_error.message == "uninitialized constant #{name}" | |
end | |
end | |
end | |
ModelExtensionLoader.activate! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment