diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e57c41..9eb0340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 2.6.1 (Next) * [#45](https://github.com/mongoid/mongoid-grid_fs/pull/45): Encode stored file bytes rather than the model description in Base64 and data URIs - [@yudho](https://github.com/yudho), [@dblock](https://github.com/dblock). +* [#48](https://github.com/mongoid/mongoid-grid_fs/pull/48): Check for the specific legacy BSON classes instead of the Moped namespace - [@jeremy6d](https://github.com/jeremy6d), [@dblock](https://github.com/dblock). * [#86](https://github.com/mongoid/mongoid-grid_fs/pull/86): Expand persisted GridFS coverage for streaming, upload failures, namespaces, attributes, queries, historical records without `updateDate`, and Rails responses - [@dblock](https://github.com/dblock). * [#85](https://github.com/mongoid/mongoid-grid_fs/pull/85): Report test coverage to Coveralls and update CI badges - [@dblock](https://github.com/dblock). * [#84](https://github.com/mongoid/mongoid-grid_fs/pull/84): Migrate Danger to danger-pr-comment workflows - [@dblock](https://github.com/dblock). diff --git a/lib/mongoid/grid_fs.rb b/lib/mongoid/grid_fs.rb index 63ed8f3..1dcf1c7 100644 --- a/lib/mongoid/grid_fs.rb +++ b/lib/mongoid/grid_fs.rb @@ -152,7 +152,7 @@ def put(readable, attributes = {}) end def binary_for(*buf) - if defined?(Moped::BSON) + if defined?(Moped::BSON::Binary) Moped::BSON::Binary.new(:generic, buf.join) else BSON::Binary.new(buf.join, :generic) @@ -415,7 +415,7 @@ def self.build_chunk_model_for(namespace) store_in collection: "#{prefix}.chunks" field(:n, type: Integer, default: 0) - field(:data, type: (defined?(Moped::BSON) ? Moped::BSON::Binary : BSON::Binary)) + field(:data, type: (defined?(Moped::BSON::Binary) ? Moped::BSON::Binary : BSON::Binary)) belongs_to(:file, foreign_key: :files_id, class_name: file_model_name) diff --git a/test/bson_compatibility_test.rb b/test/bson_compatibility_test.rb new file mode 100644 index 0000000..6257af4 --- /dev/null +++ b/test/bson_compatibility_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative 'helper' +require 'open3' +require 'rbconfig' + +class BsonCompatibilityTest < Minitest::Test + def test_moped_namespace_without_legacy_binary_uses_modern_bson + script = <<~RUBY + require 'logger' + require 'mongoid' + Object.const_set(:Moped, Module.new) unless defined?(Moped) + Moped.const_set(:BSON, Module.new) unless defined?(Moped::BSON) + require 'mongoid/grid_fs' + namespace = Mongoid::GridFs.namespace_for(:bsoncompatibility) + binary = namespace.binary_for("one", "two") + abort "wrong binary type" unless binary.is_a?(BSON::Binary) + abort "wrong bytes" unless binary.data == "onetwo" + abort "wrong field type" unless namespace.chunk_model.fields.fetch("data").type == BSON::Binary + RUBY + + output, status = Open3.capture2e(RbConfig.ruby, '-Ilib', '-e', script) + assert status.success?, output + end +end diff --git a/test/mongoid-grid_fs_test.rb b/test/mongoid-grid_fs_test.rb index b6613e5..ea040d5 100644 --- a/test/mongoid-grid_fs_test.rb +++ b/test/mongoid-grid_fs_test.rb @@ -230,7 +230,7 @@ protected def object_id_re - object_id = defined?(Moped::BSON) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new + object_id = defined?(Moped::BSON::ObjectId) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new / \w{#{ object_id.to_s.size }} /iomx end