module AmazingPrint::ActiveRecord
Public Class Methods
included(base)
click to toggle source
# File lib/amazing_print/ext/active_record.rb, line 8 def self.included(base) base.send :alias_method, :cast_without_active_record, :cast base.send :alias_method, :cast, :cast_with_active_record end
Public Instance Methods
cast_with_active_record(object, type)
click to toggle source
Add ActiveRecord
class names to the dispatcher pipeline.
# File lib/amazing_print/ext/active_record.rb, line 15 def cast_with_active_record(object, type) cast = cast_without_active_record(object, type) return cast unless defined?(::ActiveRecord::Base) if object.is_a?(::ActiveRecord::Base) cast = :active_record_instance elsif object.is_a?(::ActiveModel::Errors) cast = :active_model_error elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base) cast = :active_record_class elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation) cast = :array end cast end
Private Instance Methods
awesome_active_model_error(object)
click to toggle source
Format ActiveModel error object.
# File lib/amazing_print/ext/active_record.rb, line 81 def awesome_active_model_error(object) return object.inspect unless 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.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash| 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 end end data.merge!({ details: object.details, messages: object.messages }) "#{object} " << awesome_hash(data) end
awesome_active_record_class(object)
click to toggle source
Format ActiveRecord
class object.
# File lib/amazing_print/ext/active_record.rb, line 61 def awesome_active_record_class(object) if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base' return object.inspect end if object.respond_to?(:abstract_class?) && object.abstract_class? return awesome_class(object) end data = object.columns.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash| hash[c.name.to_sym] = c.type end name = "class #{awesome_simple(object.to_s, :class)}" base = "< #{awesome_simple(object.superclass.to_s, :class)}" [name, base, awesome_hash(data)].join(' ') end
awesome_active_record_instance(object)
click to toggle source
Format ActiveRecord
instance object.
NOTE: by default only instance attributes (i.e. columns) are shown. To format ActiveRecord
instance as regular object showing its instance variables and accessors use :raw => true option:
ap record, :raw => true
# File lib/amazing_print/ext/active_record.rb, line 42 def awesome_active_record_instance(object) return object.inspect unless defined?(::ActiveSupport::OrderedHash) return awesome_object(object) if @options[:raw] data = if object.class.column_names != object.attributes.keys object.attributes else object.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash| if object.has_attribute?(name) || object.new_record? value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name) hash[name.to_sym] = value end end end "#{object} " << awesome_hash(data) end