Skip to content

Instantly share code, notes, and snippets.

@slicedpan
Created November 26, 2014 15:35
Show Gist options
  • Save slicedpan/769a745ea088d58fee90 to your computer and use it in GitHub Desktop.
Save slicedpan/769a745ea088d58fee90 to your computer and use it in GitHub Desktop.
Rails force class loading
module ForceLoader
def self.load_directory(path, namespace = nil)
return if !Rails.env.development?
base_path = Rails.root.join(*path.split(/[\/\\]/))
Dir[base_path.join("**", "*.rb")].each do |file|
model_name = Pathname.new(file.to_s).basename.to_s.chomp('.rb').camelcase
if namespace
model_name = namespace.to_s + "::" + model_name
end
begin
Module.const_get(model_name)
rescue Exception => e
Rails.logger.info("Could not autoload '#{model_name}'")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment