Skip to content

Instantly share code, notes, and snippets.

@rafer
Created February 8, 2010 22:07
Show Gist options
  • Save rafer/298629 to your computer and use it in GitHub Desktop.
Save rafer/298629 to your computer and use it in GitHub Desktop.
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