Skip to content

Commit

Permalink
Merge pull request #106 from jclusso/add_mongoid_field_aliases
Browse files Browse the repository at this point in the history
Add Mongoid field aliases
  • Loading branch information
HarlemSquirrel authored Feb 12, 2024
2 parents 91be233 + a66a7bb commit 0b860b7
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 49 deletions.
8 changes: 8 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ if RUBY_VERSION >= '2.7'
gem 'rails', '~> 7.0.0'
gem 'sqlite3', platform: :mri
end

appraise 'mongoid-7.0' do
gem 'mongoid', '~> 7.0.0'
end

appraise 'mongoid-8.0' do
gem 'mongoid', '~> 8.0.0'
end
end

appraise 'rails-6.1' do
Expand Down
16 changes: 14 additions & 2 deletions lib/amazing_print/ext/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ def cast_with_mongoid(object, type)
def awesome_mongoid_class(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)

aliases = object.aliased_fields.invert
data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
name = c[1].name
alias_name = aliases[name] unless name == '_id'
printed_name = alias_name ? "#{alias_name}(#{name})" : name

hash[printed_name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
hash
end

name = "class #{awesome_simple(object.to_s, :class)}"
Expand All @@ -48,8 +54,14 @@ def awesome_mongoid_class(object)
def awesome_mongoid_document(object)
return object.inspect unless defined?(::ActiveSupport::OrderedHash)

aliases = object.aliased_fields.invert
data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
hash[c[0].to_sym] = c[1]
name = c[0]
alias_name = aliases[name] unless name == '_id'
printed_name = alias_name ? "#{alias_name}(#{name})" : name

hash[printed_name.to_sym] = c[1]
hash
end
data = { errors: object.errors, attributes: data } unless object.errors.empty?
"#{object} #{awesome_hash(data)}"
Expand Down
157 changes: 110 additions & 47 deletions spec/ext/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,130 @@
require 'spec_helper'

RSpec.describe 'AmazingPrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.call do
if ExtVerifier.has_mongoid?
before :all do
class MongoUser
include Mongoid::Document
before do
@ap = AmazingPrint::Inspector.new plain: true, sort_keys: true
end

field :first_name, type: String
field :last_name, type: String
describe 'Document' do
if ExtVerifier.has_mongoid?
before :all do
class MongoUser
include Mongoid::Document

field :first_name, type: String
field :last_name, type: String
end
end

after :all do
Object.instance_eval { remove_const :MongoUser }
Object.instance_eval { remove_const :Chamelion }
end
end

after :all do
Object.instance_eval { remove_const :MongoUser }
Object.instance_eval { remove_const :Chamelion }
it 'prints class instance' do
user = MongoUser.new first_name: 'Al', last_name: 'Capone'
out = @ap.send :awesome, user

object_id = user.id.inspect
str = <<~EOS.strip
#<MongoUser:placeholder_id> {
:_id => #{object_id},
:first_name => "Al",
:last_name => "Capone"
}
EOS
expect(out).to be_similar_to(str, { skip_bson: true })
end
end

before do
@ap = AmazingPrint::Inspector.new plain: true, sort_keys: true
end
it 'prints the class' do
class_spec = <<~EOS.strip
class MongoUser < Object {
:_id => :"bson/object_id",
:first_name => :string,
:last_name => :string
}
EOS

it 'prints class instance' do
user = MongoUser.new first_name: 'Al', last_name: 'Capone'
out = @ap.send :awesome, user

object_id = user.id.inspect
str = <<~EOS.strip
#<MongoUser:placeholder_id> {
:_id => #{object_id},
:first_name => "Al",
:last_name => "Capone"
}
EOS
expect(out).to be_similar_to(str, { skip_bson: true })
end
expect(@ap.send(:awesome, MongoUser)).to eq class_spec
end

it 'prints the class when type is undefined' do
class Chamelion
include Mongoid::Document
field :last_attribute
end

it 'prints the class' do
class_spec = <<~EOS.strip
class MongoUser < Object {
:_id => :"bson/object_id",
:first_name => :string,
:last_name => :string
}
EOS
class_spec = <<~EOS.strip
class Chamelion < Object {
:_id => :"bson/object_id",
:last_attribute => :object
}
EOS

expect(@ap.send(:awesome, MongoUser)).to eq class_spec
expect(@ap.send(:awesome, Chamelion)).to eq class_spec
end
end

it 'prints the class when type is undefined' do
class Chamelion
include Mongoid::Document
field :last_attribute
describe 'Document with aliased fields' do
if ExtVerifier.has_mongoid?
before :all do
class MongoUser
include Mongoid::Document

field :fn, as: :first_name, type: String
field :ln, as: :last_name, type: String
end
end

after :all do
Object.instance_eval { remove_const :MongoUser }
Object.instance_eval { remove_const :Chamelion }
end
end

it 'prints class instance' do
user = MongoUser.new first_name: 'Al', last_name: 'Capone'
out = @ap.send :awesome, user

object_id = user.id.inspect
str = <<~EOS.strip
#<MongoUser:placeholder_id> {
:_id => #{object_id},
:"first_name(fn)" => "Al",
:"last_name(ln)" => "Capone"
}
EOS
expect(out).to be_similar_to(str, { skip_bson: true })
end

it 'prints the class' do
class_spec = <<~EOS.strip
class MongoUser < Object {
:_id => :"bson/object_id",
:"first_name(fn)" => :string,
:"last_name(ln)" => :string
}
EOS

expect(@ap.send(:awesome, MongoUser)).to eq class_spec
end

class_spec = <<~EOS.strip
class Chamelion < Object {
:_id => :"bson/object_id",
:last_attribute => :object
}
EOS
it 'prints the class when type is undefined' do
class Chamelion
include Mongoid::Document
field :la, as: :last_attribute
end

expect(@ap.send(:awesome, Chamelion)).to eq class_spec
class_spec = <<~EOS.strip
class Chamelion < Object {
:_id => :"bson/object_id",
:"last_attribute(la)" => :object
}
EOS

expect(@ap.send(:awesome, Chamelion)).to eq class_spec
end
end
end

Expand Down

0 comments on commit 0b860b7

Please sign in to comment.