Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created February 14, 2012 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vangberg/1828617 to your computer and use it in GitHub Desktop.
Save vangberg/1828617 to your computer and use it in GitHub Desktop.
class Dumper
attr_accessor :object, :tables
def initialize(object, tables=nil)
@object = object
@tables = tables || Hash.new {|hash,key| hash[key] = []}
end
def dump
puts "Dumping #{object.class.name}##{object.id}"
object.class.reflect_on_all_associations.each do |assoc|
case assoc.macro
when :has_many
object.send(assoc.name).each do |child|
if !tables[child.class.table_name].include?(child.id)
tables[assoc.table_name] << child.id
Dumper.new(child, tables).dump
end
end
when :belongs_to
child = object.send(assoc.name)
if child && !tables[child.class.table_name].include?(child.id)
tables[child.class.table_name] << child.id
Dumper.new(child, tables).dump
end
end
end
self
end
def out
tables.each do |table, ids|
puts "#{table}: #{ids}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment