Module: AwesomePrintActiveRecord
- Defined in:
- lib/ap/mixin/active_record.rb
Overview
Copyright © 2010 Michael Dvorkin
Awesome Print is freely distributable under the terms of MIT license. See LICENSE file or www.opensource.org/licenses/mit-license.php
Class Method Summary collapse
Instance Method Summary collapse
-
#awesome_active_record_class(object) ⇒ Object
Format ActiveRecord class object.
-
#awesome_active_record_instance(object) ⇒ Object
Format ActiveRecord instance object.
-
#printable_with_active_record(object) ⇒ Object
Add ActiveRecord class names to the dispatcher pipeline.
Class Method Details
.included(base) ⇒ Object
8 9 10 11 |
# File 'lib/ap/mixin/active_record.rb', line 8 def self.included(base) base.send :alias_method, :printable_without_active_record, :printable base.send :alias_method, :printable, :printable_with_active_record end |
Instance Method Details
#awesome_active_record_class(object) ⇒ Object
Format ActiveRecord class object.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ap/mixin/active_record.rb', line 39 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 |
#awesome_active_record_instance(object) ⇒ Object
Format ActiveRecord instance object.
29 30 31 32 33 34 35 |
# File 'lib/ap/mixin/active_record.rb', line 29 def awesome_active_record_instance(object) 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 |
#printable_with_active_record(object) ⇒ Object
Add ActiveRecord class names to the dispatcher pipeline.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ap/mixin/active_record.rb', line 15 def printable_with_active_record(object) printable = printable_without_active_record(object) if printable == :self if object.is_a?(ActiveRecord::Base) printable = :active_record_instance end elsif printable == :class and object.ancestors.include?(ActiveRecord::Base) printable = :active_record_class end printable end |