Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method marshal_dump for ActiveModel::Errors #413

Open
wedsonlima opened this issue Apr 16, 2021 · 6 comments · May be fixed by #415
Open

undefined method marshal_dump for ActiveModel::Errors #413

wedsonlima opened this issue Apr 16, 2021 · 6 comments · May be fixed by #415

Comments

@wedsonlima
Copy link

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:

object_dump = object.marshal_dump.first

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
@wedsonlima wedsonlima changed the title output error: #<NoMethodError: undefined method marshal_dump' for #<ActiveModel::Errors:0x000055807859a148> undefined method marshal_dump for ActiveModel::Errors Apr 16, 2021
@BryanH
Copy link
Collaborator

BryanH commented Apr 19, 2021

Thanks, I'll take a look!

@martinstreicher
Copy link

Ran into this today, too.

@martinstreicher
Copy link

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
Copy link

Hi, someone working on this patch?

@edipofederle
Copy link

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

@zlenderja
Copy link

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

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

vassalloandrea added a commit to vassalloandrea/dev_tools that referenced this issue Aug 1, 2023
Amazing Print is a fork of Awesome Print that has already resolved
the undefined method marshal_dump for ActiveModel::Errors issue [1].

---

[1]: awesome-print/awesome_print#413
vassalloandrea added a commit to vassalloandrea/dev_tools that referenced this issue Aug 1, 2023
Amazing Print is a fork of Awesome Print that has already resolved
the undefined method marshal_dump for ActiveModel::Errors issue [1].

---

[1]: awesome-print/awesome_print#413
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants