awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

undefined method `marshal_dump` for `ActiveModel::Errors` #413

Open wedsonlima opened 3 years ago

wedsonlima commented 3 years ago

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:

https://github.com/awesome-print/awesome_print/blob/9ae56ba413b9159c826285f31e45fe5002af253a/lib/awesome_print/ext/active_record.rb#L84

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 3 years ago

Thanks, I'll take a look!

martinstreicher commented 3 years ago

Ran into this today, too.

martinstreicher commented 3 years ago

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 3 years ago

Hi, someone working on this patch?

edipofederle commented 3 years ago

Guys, I pushed a PR to fix it, someone know why no CI jobs is running for that?

zlenderja commented 1 year ago

I ended up replacing awesome_print with a fork amazing_print which includes the fix

https://github.com/amazing-print/amazing_print