mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Format ActiveRecord instances showing attributes only
This commit is contained in:
parent
af1ba40b83
commit
3258a9bc63
2 changed files with 74 additions and 2 deletions
|
@ -15,7 +15,11 @@ module AwesomePrint
|
|||
#------------------------------------------------------------------------------
|
||||
def cast_with_active_record(object, type)
|
||||
cast = cast_without_active_record(object, type)
|
||||
if defined?(::ActiveRecord) && object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
||||
return cast if !defined?(::ActiveRecord)
|
||||
|
||||
if object.is_a?(::ActiveRecord::Base)
|
||||
cast = :active_record_instance
|
||||
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
||||
cast = :active_record_class
|
||||
end
|
||||
cast
|
||||
|
@ -23,6 +27,26 @@ module AwesomePrint
|
|||
|
||||
private
|
||||
|
||||
# Format ActiveRecord instance object.
|
||||
#
|
||||
# NOTE: by default only instance attributes (i.e. columns) are shown. To format
|
||||
# ActiveRecord instance as regular object showing its with instance variables
|
||||
# and accessors use :raw => true option:
|
||||
#
|
||||
# ap record, :raw => true
|
||||
#
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_active_record_instance(object)
|
||||
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
||||
return awesome_object(object) if @options[:raw]
|
||||
|
||||
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
||||
hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
|
||||
hash
|
||||
end
|
||||
"#{object} " << awesome_hash(data)
|
||||
end
|
||||
|
||||
# Format ActiveRecord class object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_active_record_class(object)
|
||||
|
|
|
@ -55,7 +55,7 @@ begin
|
|||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord instance" do
|
||||
describe "ActiveRecord instance, attributes only (default)" do
|
||||
before do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
||||
|
@ -63,6 +63,53 @@ begin
|
|||
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
|
||||
end
|
||||
|
||||
it "display single record" do
|
||||
out = @ap.send(:awesome, @diana)
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567> {
|
||||
:admin => false,
|
||||
:created_at => 1992-10-10 12:30:00 UTC,
|
||||
:id => nil,
|
||||
:name => "Diana",
|
||||
:rank => 1
|
||||
}
|
||||
EOS
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
|
||||
it "display multiple records" do
|
||||
out = @ap.send(:awesome, [ @diana, @laura ])
|
||||
str = <<-EOS.strip
|
||||
[
|
||||
[0] #<User:0x01234567> {
|
||||
:admin => false,
|
||||
:created_at => 1992-10-10 12:30:00 UTC,
|
||||
:id => nil,
|
||||
:name => "Diana",
|
||||
:rank => 1
|
||||
},
|
||||
[1] #<User:0x01234567> {
|
||||
:admin => true,
|
||||
:created_at => 2003-05-26 14:15:00 UTC,
|
||||
:id => nil,
|
||||
:name => "Laura",
|
||||
:rank => 2
|
||||
}
|
||||
]
|
||||
EOS
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord instance (raw)" do
|
||||
before do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
||||
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true)
|
||||
end
|
||||
|
||||
it "display single record" do
|
||||
out = @ap.send(:awesome, @diana)
|
||||
|
||||
|
@ -300,6 +347,7 @@ EOS
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord class" do
|
||||
before do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue