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

Check if Rails classes are loaded before using them in Rails mixins

This commit is contained in:
Mike Dvorkin 2011-02-02 22:35:17 -08:00
parent f7fb11ed85
commit 05265cb4e5
3 changed files with 13 additions and 10 deletions

View file

@ -35,4 +35,4 @@ module AwesomePrintActionView
end
ActionView::Base.send(:include, AwesomePrintActionView) if defined?(::ActionView)
ActionView::Base.send(:include, AwesomePrintActionView) if defined?(ActionView)

View file

@ -14,6 +14,8 @@ module AwesomePrintActiveRecord
#------------------------------------------------------------------------------
def printable_with_active_record(object)
printable = printable_without_active_record(object)
return printable if !defined?(ActiveRecord::Base)
if printable == :self
if object.is_a?(ActiveRecord::Base)
printable = :active_record_instance
@ -27,6 +29,8 @@ module AwesomePrintActiveRecord
# Format ActiveRecord instance object.
#------------------------------------------------------------------------------
def awesome_active_record_instance(object)
return object.inspect if !defined?(ActiveSupport::OrderedHash)
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
@ -37,17 +41,14 @@ module AwesomePrintActiveRecord
# Format ActiveRecord class object.
#------------------------------------------------------------------------------
def awesome_active_record_class(object)
if object.respond_to?(:columns)
data = object.columns.inject(ActiveSupport::OrderedHash.new) do |hash, c|
hash[c.name.to_sym] = c.type
hash
end
"class #{object} < #{object.superclass} " << awesome_hash(data)
else
object.inspect
end
end
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:columns)
data = object.columns.inject(ActiveSupport::OrderedHash.new) do |hash, c|
hash[c.name.to_sym] = c.type
hash
end
"class #{object} < #{object.superclass} " << awesome_hash(data)
end
end
AwesomePrint.send(:include, AwesomePrintActiveRecord)

View file

@ -14,6 +14,8 @@ module AwesomePrintActiveSupport
#------------------------------------------------------------------------------
def printable_with_active_support(object)
printable = printable_without_active_support(object)
return printable if !defined?(ActiveSupport::TimeWithZone) || !defined?(HashWithIndifferentAccess)
if printable == :self
if object.is_a?(ActiveSupport::TimeWithZone)
printable = :active_support_time