Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## master (unreleased)
- Remove usage of `marshal_dump` since this was removed from recent Rails versions [@edipofederle]


## 1.9.2
Expand Down
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