1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Actually fixed AR::Base class detection this time, and added support for classes that inherit from AR::Base anywhere up the chain.

This commit is contained in:
Tobias Crawley 2010-04-22 01:24:17 -04:00
parent 6dc67a2cff
commit d60f1d8719
2 changed files with 37 additions and 5 deletions

View file

@ -18,7 +18,7 @@ module AwesomePrintActiveRecord
if object.is_a?(ActiveRecord::Base)
printable = :active_record_instance
end
elsif printable == :class && object.class.is_a?(ActiveRecord::Base)
elsif printable == :class and class_inherits_from(object, ActiveRecord::Base)
printable = :active_record_class
end
printable
@ -47,7 +47,14 @@ module AwesomePrintActiveRecord
object.inspect
end
end
private
def class_inherits_from(klass, parent_class)
while klass do
return true if klass == parent_class
klass = klass.superclass
end
end
end
AwesomePrint.send(:include, AwesomePrintActiveRecord)

View file

@ -22,8 +22,18 @@ if defined?(::ActiveRecord)
column :rank, :integer
column :admin, :boolean
column :created_at, :datetime
end
def self.table_exists?
true
end
end
class SubUser < User
def self.columns
User.columns
end
end
describe "AwesomePrint/ActiveRecord" do
before(:each) do
stub_dotfile!
@ -75,7 +85,7 @@ EOS
#------------------------------------------------------------------------------
describe "ActiveRecord class" do
it "should" do
it "should print the class" do
@ap = AwesomePrint.new(:plain => true)
@ap.send(:awesome, User).should == <<-EOS.strip
class User < ActiveRecord::Base {
@ -85,7 +95,22 @@ class User < ActiveRecord::Base {
:admin => :boolean,
:created_at => :datetime
}
EOS
EOS
end
it "should print the class for non-direct subclasses of AR::Base" do
@ap = AwesomePrint.new(:plain => true)
@ap.send(:awesome, SubUser).should == <<-EOS.strip
class SubUser < User {
:id => :integer,
:name => :string,
:rank => :integer,
:admin => :boolean,
:created_at => :datetime
}
EOS
end
end
end