Module: AmazingPrint::ActiveRecord

Included in:
Formatter
Defined in:
lib/amazing_print/ext/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
# File 'lib/amazing_print/ext/active_record.rb', line 10

def self.included(base)
  base.send :alias_method, :cast_without_active_record, :cast
  base.send :alias_method, :cast, :cast_with_active_record
end

Instance Method Details

#cast_with_active_record(object, type) ⇒ Object

Add ActiveRecord class names to the dispatcher pipeline.




17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/amazing_print/ext/active_record.rb', line 17

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