From 1ff12067cbc29a83c3b14856ef54fd6e368edb5c Mon Sep 17 00:00:00 2001 From: James Cox Date: Wed, 23 Jan 2019 14:58:51 -0500 Subject: [PATCH] Fix mongoid concerns --- .../active_record_attributeset_formatter.rb | 4 +- .../ext/mongoid_document_formatter.rb | 45 ++++++++++--------- lib/awesome_print/inspector.rb | 2 + lib/awesome_print/version.rb | 2 +- spec/formatters/ext/mongoid_spec.rb | 4 +- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb b/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb index d586773..a14b183 100644 --- a/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +++ b/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb @@ -11,7 +11,9 @@ module AwesomePrint end def self.formattable?(object) - defined?(::ActiveRecord) && object.is_a?(ActiveRecord::AttributeSet) + defined?(::ActiveRecord) && + defined?(::ActiveRecord::AttributeSet) && + object.is_a?(ActiveRecord::AttributeSet) end def format(object) diff --git a/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb b/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb index af39749..72ecc36 100644 --- a/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +++ b/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb @@ -8,7 +8,8 @@ module AwesomePrint def self.formattable?(object) defined?(::Mongoid::Document) && - object.class.ancestors.include?(::Mongoid::Document) + (object.class.ancestors.include?(::Mongoid::Document) || + (object.respond_to?(:ancestors) && object.ancestors.include?(::Mongoid::Document))) end def format(object) @@ -16,40 +17,42 @@ module AwesomePrint return Formatters::SimpleFormatter.new(@inspector).format(object.inspect) end - if object.respond_to?(:fields) + if object.is_a?(Class) + puts "[MGD] formatting #{object} as class..." if AwesomePrint.debug # mongoid class format_as_class(object) else + puts "[MGD] formatting #{object} as instance..." if AwesomePrint.debug format_as_instance(object) end end - end + private - private + def format_as_class(object) + data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.to_sym + hash + end - def format_as_class(object) - data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.to_sym - hash + [ + "class #{colorize(object.to_s, :class)}", + "< #{colorize(object.superclass.to_s, :class)}", + Formatters::HashFormatter.new(@inspector).format(data) + ].join(' ') end - [ - "class #{colorize(object.to_s, :class)}", - "< #{colorize(object.superclass.to_s, :class)}", - Formatters::HashFormatter.new(@inspector).format(data) - ].join(' ') - end + def format_as_instance(object) + data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[0].to_sym] = c[1] + hash + end - def format_as_instance(object) - data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c[0].to_sym] = c[1] - hash + data = { errors: object.errors, attributes: data } if !object.errors.empty? + + "#{object} #{Formatters::HashFormatter.new(@inspector).format(data)}" end - data = { errors: object.errors, attributes: data } if !object.errors.empty? - - "#{object} #{Formatters::HashFormatter.new(@inspector).format(data)}" end end end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index d3ed6b2..8ef906d 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -127,6 +127,8 @@ module AwesomePrint # awesome_print-rails binding lib or something. if defined?(::ActiveRecord) && object.respond_to?(:ancestors) && object.ancestors.to_a.include?(::ActiveRecord::Base) :activerecord_base_class + elsif defined?(::Mongoid) && object.respond_to?(:ancestors) && object.ancestors.to_a.include?(::Mongoid::Document) + :mongoid_document else object.class.to_s.gsub(/:+/, '_').downcase.to_sym end diff --git a/lib/awesome_print/version.rb b/lib/awesome_print/version.rb index adb4285..37c7c1f 100644 --- a/lib/awesome_print/version.rb +++ b/lib/awesome_print/version.rb @@ -1,7 +1,7 @@ module AwesomePrint def self.debug - false + false #true end def self.version diff --git a/spec/formatters/ext/mongoid_spec.rb b/spec/formatters/ext/mongoid_spec.rb index cb8e7f3..af4cb6b 100644 --- a/spec/formatters/ext/mongoid_spec.rb +++ b/spec/formatters/ext/mongoid_spec.rb @@ -40,7 +40,7 @@ RSpec.describe 'AwesomePrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.ca it 'should print the class' do class_spec = <<-EOS.strip class MongoUser < Object { - :_id => :"bson/object_id", + :_id => :bson/object_id, :first_name => :string, :last_name => :string } @@ -57,7 +57,7 @@ class MongoUser < Object { class_spec = <<-EOS.strip class Chamelion < Object { - :_id => :"bson/object_id", + :_id => :bson/object_id, :last_attribute => :object } EOS