Skip to content

Commit

Permalink
Remove usage of marshal_dump since this was removed from Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
edipofederle committed Jul 2, 2021
1 parent 8a7ff0a commit 648ab3d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
19 changes: 5 additions & 14 deletions lib/awesome_print/ext/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,12 @@ def awesome_active_model_error(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]

object_dump = object.marshal_dump.first
data = 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
data = object.instance_variable_get('@base')
.attributes
.merge(details: object.details.to_h,
messages: object.messages.to_h.transform_values(&:to_a))

data.merge!({details: object.details, messages: object.messages})
"#{object} #{awesome_hash(data)}"
[object.to_s, awesome_hash(data)].join(' ')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.env
end

# Create models
class User < ActiveRecord::Base; has_many :emails; end
class User < ActiveRecord::Base; validates :name, presence: true; has_many :emails; end
class SubUser < User; end
class Email < ActiveRecord::Base; belongs_to :user; end
end
18 changes: 16 additions & 2 deletions spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'
require 'active_record_helper'

RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }.call do
RSpec.describe 'AwesomePrint/ActiveRecord' do
describe 'ActiveRecord instance, attributes only (default)' do
before do
ActiveRecord::Base.default_timezone = :utc
Expand Down Expand Up @@ -116,12 +116,27 @@
@ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true)
end

context 'when validations errors' do
before do
@ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: false)
end

it 'with validation errors' do
@diana.name = nil
@diana.valid?
out = @ap.awesome(@diana.errors)
expect(out).to match("can't be blank")
end
end

it 'display single record' do
out = @ap.awesome(@diana)

raw_object_string =
if activerecord_6_1?
ActiveRecordData.raw_6_1_diana
elsif activerecord_6_1_3?
ActiveRecordData.raw_6_1_diana
elsif activerecord_6_0?
ActiveRecordData.raw_6_0_diana
elsif activerecord_5_2?
Expand Down Expand Up @@ -257,4 +272,3 @@ class SubUser < User {
end
end
end

0 comments on commit 648ab3d

Please sign in to comment.