Skip to content

Instantly share code, notes, and snippets.

@lacco
Created February 9, 2010 21:50
Show Gist options
  • Save lacco/299711 to your computer and use it in GitHub Desktop.
Save lacco/299711 to your computer and use it in GitHub Desktop.
## Heckle for Rails
# Calls heckle for each method of each model
# Idea: Instead of exluding ActiveRecord methods, grep model file for existing methods
# Load all models
Dir['app/models/**/*.rb'].each {|f| require_dependency f}
# Search all model classes
klasses_to_heckle = ActiveRecord::Base.send(:subclasses)-[ActiveRecord::SessionStore::Session]
# Search for methods to heckle, ignore those automatically by active record
methods_to_heckle = []
klasses_to_heckle.each do |klass|
# Remove those methods which are added by assocations
reflection_methods = klass.reflections.keys.map do |ref|
[ref.to_s, "#{ref}=", "loaded_#{ref}?", "build_#{ref}", "create_#{ref}",
"validate_associated_records_for_#{ref}", "autosave_associated_records_for_#{ref}",
"set_#{ref}_target"]
end.flatten
reflection_methods_regex = [
# Ignore all methods ending with id, id=, ids, ids=
/_ids?=?$/
]
klass.instance_methods(false).reject do |method|
reflection_methods.include?(method.to_s) || reflection_methods_regex.any?{|regex| method =~ regex}
end.each do |method|
methods_to_heckle << [klass, method]
end
end
# Call heckle
methods_to_heckle.each do |klass, method|
file = "spec/models/#{klass.to_s.underscore}_spec.rb"
next if Dir[file].empty?
cmd = "spec --heckle #{klass}##{method} #{file}"
puts cmd
system cmd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment