Created
April 10, 2012 19:09
-
-
Save mathieuravaux/2353739 to your computer and use it in GitHub Desktop.
Airbrake monkeypatch to serialize correctly Mongoid documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ::Airbrake::Notice | |
def xml_vars_for(builder, hash) | |
hash.each do |key, value| | |
value = value.to_airbrake if value.respond_to?(:to_airbrake) | |
if value.respond_to?(:to_hash) | |
builder.var(:key => key){|b| xml_vars_for(b, value.to_hash) } | |
else | |
builder.var(value.to_s, :key => key) | |
end | |
end | |
end | |
def clean_unserializable_data(data, stack = []) | |
return "[possible infinite recursion halted]" if stack.any?{|item| item == data.object_id } | |
data = data.to_airbrake if data.respond_to?(:to_airbrake) | |
if data.respond_to?(:to_hash) | |
data.to_hash.inject({}) do |result, (key, value)| | |
result.merge(key => clean_unserializable_data(value, stack + [data.object_id])) | |
end | |
elsif data.respond_to?(:to_ary) | |
data.to_ary.collect do |value| | |
clean_unserializable_data(value, stack + [data.object_id]) | |
end | |
else | |
data.to_s | |
end | |
end | |
end | |
module Mongoid::Document | |
def to_airbrake | |
attributes | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment