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 3, 2021
1 parent 8a7ff0a commit 2829fd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 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
14 changes: 13 additions & 1 deletion spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@
@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)

Expand Down Expand Up @@ -257,4 +270,3 @@ class SubUser < User {
end
end
end

0 comments on commit 2829fd0

Please sign in to comment.