undefined method `marshal_dump` for `ActiveModel::Errors`
wedsonlima opened this issue · 6 comments
wedsonlima commented
The marshal_dump
method was removed from ActiveModel::Errors
in this commit and it's breaking pry console with awesome_print:
output error: #<NoMethodError: undefined method marshal_dump' for #<ActiveModel::Errors:0x000055807859a148>
workaround:
module AwesomePrint
module ActiveRecord
def awesome_active_model_error(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]
data = if object.respond_to?(:marshal_dump)
object_dump = object.marshal_dump.first
if object_dump.class.column_names != object_dump.attributes.keys
object_dump.attributes
else
object_dump.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
if object_dump.has_attribute?(name) || object_dump.new_record?
value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
hash[name.to_sym] = value
end
hash
end
end
else
{}
end
data.merge!({details: object.details, messages: object.messages})
"#{object} #{awesome_hash(data)}"
end
end
end
BryanH commented
Thanks, I'll take a look!
martinstreicher commented
Ran into this today, too.
martinstreicher commented
Here is one that Rubocop is a little happier about:
# frozen_string_literal: true
module AwesomePrint
module ActiveRecord
def awesome_active_model_error(object)
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]
data = object.respond_to?(:marshal_dump) ? marshalled_errors(object) : {}
data[:details] = object.details
data[:messages] = object.messages
"#{object} #{awesome_hash(data)}"
end
private
def marshalled_errors(object)
object_dump = object.marshal_dump.first
return object_dump.attributes unless object_dump.class.column_names == object_dump.attributes.keys
object_dump.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
next unless object_dump.has_attribute?(name) || object_dump.new_record?
value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
hash[name.to_sym] = value
end
end
end
end
edipofederle commented
Hi, someone working on this patch?
edipofederle commented
Guys, I pushed a PR to fix it, someone know why no CI jobs is running for that?
zlenderja commented
I ended up replacing awesome_print with a fork amazing_print which includes the fix