4 Debugging tools for Has_many_polymorphs.
6 Enable the different tools by setting the environment variable HMP_DEBUG. Settings with special meaning are <tt>"ruby-debug"</tt>, <tt>"trace"</tt>, and <tt>"dependencies"</tt>.
10 Enabled by default when HMP_DEBUG is set.
12 Ouputs a folder <tt>generated_models/</tt> in RAILS_ROOT containing valid Ruby files explaining all the ActiveRecord relationships set up by the plugin, as well as listing the line in the plugin that called each particular association method.
16 Enable by setting HMP_DEBUG to <tt>"ruby-debug"</tt>.
18 Starts <tt>ruby-debug</tt> for the life of the process.
22 Enable by setting HMP_DEBUG to <tt>"ruby-debug"</tt>.
24 Outputs an indented trace of relevant method calls as they occur.
28 Enable by setting HMP_DEBUG to <tt>"dependencies"</tt>.
30 Turns on Rails' default dependency logging.
34 _logger_warn "debug mode enabled"
36 class << ActiveRecord::Base
37 COLLECTION_METHODS = [:belongs_to, :has_many, :has_and_belongs_to_many, :has_one,
38 :has_many_polymorphs, :acts_as_double_polymorphic_join].each do |method_name|
39 alias_method "original_#{method_name}".to_sym, method_name
40 undef_method method_name
43 unless defined? GENERATED_CODE_DIR
44 GENERATED_CODE_DIR = "#{RAILS_ROOT}/generated_models"
47 system "rm -rf #{GENERATED_CODE_DIR}"
48 Dir.mkdir GENERATED_CODE_DIR
50 _logger_warn "no permissions for generated code dir: #{GENERATED_CODE_DIR}"
53 if File.exist? GENERATED_CODE_DIR
54 alias :original_method_missing :method_missing
55 def method_missing(method_name, *args, &block)
56 if COLLECTION_METHODS.include? method_name.to_sym
57 Dir.chdir GENERATED_CODE_DIR do
58 filename = "#{demodulate(self.name.underscore)}.rb"
59 contents = File.open(filename).read rescue "\nclass #{self.name}\n\nend\n"
60 line = caller[1][/\:(\d+)\:/, 1]
61 contents[-5..-5] = "\n #{method_name} #{args[0..-2].inspect[1..-2]},\n #{args[-1].inspect[1..-2].gsub(" :", "\n :").gsub("=>", " => ")}\n#{ block ? " #{block.inspect.sub(/\@.*\//, '@')}\n" : ""} # called from line #{line}\n\n"
62 File.open(filename, "w") do |file|
66 # doesn't actually display block contents
67 self.send("original_#{method_name}", *args, &block)
69 self.send(:original_method_missing, method_name, *args, &block)
83 _logger_warn "ruby-debug enabled."
86 _logger_warn "method tracing enabled"
87 $debug_trace_indent = 0
88 set_trace_func (proc do |event, file, line, id, binding, classname|
89 if id.to_s =~ /instantiate/ #/IRB|Wirble|RubyLex|RubyToken|Logger|ConnectionAdapters|SQLite3|MonitorMixin|Benchmark|Inflector|Inflections/
91 puts (" " * $debug_trace_indent) + "#{event}ed #{classname}\##{id} from #{file.split('/').last}::#{line}"
92 $debug_trace_indent += 1
93 elsif event == 'return'
94 $debug_trace_indent -= 1 unless $debug_trace_indent == 0
95 puts (" " * $debug_trace_indent) + "#{event}ed #{classname}\##{id}"
101 _logger_warn "dependency activity being logged"
102 (::Dependencies.log_activity = true) rescue nil