aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorEthan <ethan@unth>2014-05-18 17:27:02 -0400
committerEthan <ethan@unth>2014-05-29 08:06:03 -0400
commit35983ea0ce580107c19e2cf4db8c0d8bf89eb79b (patch)
tree2fdd07f3588aa87c3456f5a008db8a1db1e2ec11 /activerecord/lib/active_record/core.rb
parent29f8eae3faf96cbe46e7eb949c7f674c5860c1cf (diff)
downloadrails-35983ea0ce580107c19e2cf4db8c0d8bf89eb79b.tar.gz
rails-35983ea0ce580107c19e2cf4db8c0d8bf89eb79b.tar.bz2
rails-35983ea0ce580107c19e2cf4db8c0d8bf89eb79b.zip
implement ActiveRecord::Base#pretty_print + changelog
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 07eafef788..942cca8e62 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -432,6 +432,29 @@ module ActiveRecord
"#<#{self.class} #{inspection}>"
end
+ # Takes a PP and prettily prints this record to it, allowing you to get a nice result from `pp record`
+ # when pp is required.
+ def pretty_print(pp)
+ pp.object_address_group(self) do
+ if defined?(@attributes) && @attributes
+ column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? }
+ pp.seplist(column_names, proc { pp.text ',' }) do |column_name|
+ column_value = read_attribute(column_name)
+ pp.breakable ' '
+ pp.group(1) do
+ pp.text column_name
+ pp.text ':'
+ pp.breakable
+ pp.pp column_value
+ end
+ end
+ else
+ pp.breakable ' '
+ pp.text 'not initialized'
+ end
+ end
+ end
+
# Returns a hash of the given methods with their names as keys and returned values as values.
def slice(*methods)
Hash[methods.map! { |method| [method, public_send(method)] }].with_indifferent_access