diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..d8ccaca --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: Build +on: [push] +jobs: + test: + strategy: + fail-fast: false + matrix: + ruby: ['3.0', '3.1'] + mongoid: ['73'] + mongodb: ['4.4', '5.0'] + + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} / Mongo ${{ matrix.mongodb }} / Mongoid ${{ matrix.mongoid }} + + steps: + - uses: actions/checkout@v3 + - name: Start MongoDB + uses: supercharge/mongodb-github-action@1.7.0 + with: + mongodb-version: ${{ matrix.mongodb }} + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + + - name: Install gems + run: | + export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/Mongoid_${{ matrix.mongoid }}.gemfile" + bundle install --jobs 4 --retry 3 + - run: bundle exec rspec diff --git a/.gitignore b/.gitignore index e12ddcc..b920c30 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ spec/reports test/tmp test/version_tmp tmp +.tool-versions # YARD artifacts .yardoc diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..306ab33 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.1.4 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..781fd4e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "ruby.rubocop.onSave": false +} diff --git a/Gemfile b/Gemfile index 851fabc..4182c03 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ source 'https://rubygems.org' gemspec + +gem 'mongoid', "~> 7.3.5" diff --git a/README.md b/README.md index aee9948..d7baa83 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# mongoid-versioning [![Build Status](https://travis-ci.org/haihappen/mongoid-versioning.png)](https://travis-ci.org/haihappen/mongoid-versioning) +# mongoid-versioning ![Build](https://github.com/fullhealthmedical/mongoid-versioning/actions/workflows/build.yml/badge.svg?branch=mongoid6) **Important:** This gem is an extraction of [Mongoid::Versioning](http://mongoid.github.io/en/mongoid/docs/extras.html#versioning) from the official [mongoid](http://mongoid.org) gem. Since Mongoid::Versioning was removed in the `4.0.0` release of Mongoid, this gem re-enables the functionality of versioned documents. @@ -11,10 +11,21 @@ Mongoid supports simple versioning through inclusion of the `Mongoid::Versioning ## Installation +### Mongoid/Ruby compatibility + +This fork has additional changes to support the following mongoid/ruby versions: + +| | branch | mongoid-paranoid support | Tested ruby version(s) | Build | +|-----------------|-----------------------------------------------------------------------------------|--------------------------|------------------------|-------| +| mongoid <5 | [master](https://github.com/fullhealthmedical/mongoid-versioning/tree/master) | Yes | 2.5, 2.6 | ![Build](https://github.com/fullhealthmedical/mongoid-versioning/actions/workflows/build.yml/badge.svg?branch=master) | +| mongoid 6 | [mongoid6](https://github.com/fullhealthmedical/mongoid-versioning/tree/mongoid6) | No | 2.7 | ![Build](https://github.com/fullhealthmedical/mongoid-versioning/actions/workflows/build.yml/badge.svg?branch=mongoid6) | +| mongoid >= 7.0 < 7.3.5 | [mongoid7](https://github.com/fullhealthmedical/mongoid-versioning/tree/mongoid7) | No | 2.7 | ![Build](https://github.com/fullhealthmedical/mongoid-versioning/actions/workflows/build.yml/badge.svg?branch=mongoid7) | +| mongoid >= 7.0 < 7.3.5 | [mongoid7](https://github.com/fullhealthmedical/mongoid-versioning/tree/mongoid73) | No | 2.7 | ![Build](https://github.com/fullhealthmedical/mongoid-versioning/actions/workflows/build.yml/badge.svg?branch=mongoid73) | + In your Gemfile: ```ruby -gem 'mongoid-versioning' +gem 'mongoid-versioning', git: 'https://github.com/fullhealthmedical/mongoid-versioning', branch: 'master' ``` ## Usage diff --git a/gemfiles/Mongoid_5.gemfile b/gemfiles/Mongoid_5.gemfile deleted file mode 100644 index dca0e76..0000000 --- a/gemfiles/Mongoid_5.gemfile +++ /dev/null @@ -1,4 +0,0 @@ -source 'https://rubygems.org' -gemspec path: '../' - -gem 'mongoid', '~> 5.0' diff --git a/gemfiles/Mongoid_6.gemfile b/gemfiles/Mongoid_6.gemfile deleted file mode 100644 index 210252d..0000000 --- a/gemfiles/Mongoid_6.gemfile +++ /dev/null @@ -1,6 +0,0 @@ -source 'https://rubygems.org' -gemspec path: '../' - -gem 'activesupport', '~> 5.0' -gem 'mongoid-paranoia', github: 'ream88/mongoid-paranoia' # Fix chicken or the egg -gem 'mongoid', '~> 6.0' diff --git a/gemfiles/Mongoid_4.gemfile b/gemfiles/Mongoid_73.gemfile similarity index 66% rename from gemfiles/Mongoid_4.gemfile rename to gemfiles/Mongoid_73.gemfile index 55c894a..7612014 100644 --- a/gemfiles/Mongoid_4.gemfile +++ b/gemfiles/Mongoid_73.gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' gemspec path: '../' -gem 'mongoid', '~> 4.0' +gem 'mongoid', '~> 7.3.5' diff --git a/lib/mongoid/core_ext/relations/cascading.rb b/lib/mongoid/core_ext/association/cascading.rb similarity index 96% rename from lib/mongoid/core_ext/relations/cascading.rb rename to lib/mongoid/core_ext/association/cascading.rb index 86042fe..3bbb546 100755 --- a/lib/mongoid/core_ext/relations/cascading.rb +++ b/lib/mongoid/core_ext/association/cascading.rb @@ -1,5 +1,5 @@ module Mongoid - module Relations + module Association module Cascading # Perform all cascading deletes, destroys, or nullifies. Will delegate to # the appropriate strategy to perform the operation. diff --git a/lib/mongoid/core_ext/relations/embedded/batchable.rb b/lib/mongoid/core_ext/association/embedded/batchable.rb similarity index 82% rename from lib/mongoid/core_ext/relations/embedded/batchable.rb rename to lib/mongoid/core_ext/association/embedded/batchable.rb index 2387b82..a905d02 100755 --- a/lib/mongoid/core_ext/relations/embedded/batchable.rb +++ b/lib/mongoid/core_ext/association/embedded/batchable.rb @@ -1,5 +1,5 @@ module Mongoid - module Relations + module Association module Embedded module Batchable # Pre process the batch removal. @@ -19,15 +19,15 @@ def pre_process_batch_remove(docs, method) docs.map do |doc| self.path = doc.atomic_path unless path execute_callback :before_remove, doc - if !_assigning? && !metadata.versioned? - doc.cascade! + if !_assigning? && !association.versioned? + doc.apply_destroy_dependencies! doc.run_before_callbacks(:destroy) if method == :destroy end - target.delete_one(doc) + _target.delete_one(doc) _unscoped.delete_one(doc) unbind_one(doc) execute_callback :after_remove, doc - doc.as_document + doc.send(:as_attributes) end end end diff --git a/lib/mongoid/core_ext/relations/bindings/embedded/many.rb b/lib/mongoid/core_ext/association/embedded/embeds_many/binding.rb similarity index 77% rename from lib/mongoid/core_ext/relations/bindings/embedded/many.rb rename to lib/mongoid/core_ext/association/embedded/embeds_many/binding.rb index c18bcc0..4757988 100755 --- a/lib/mongoid/core_ext/relations/bindings/embedded/many.rb +++ b/lib/mongoid/core_ext/association/embedded/embeds_many/binding.rb @@ -1,8 +1,10 @@ module Mongoid - module Relations - module Bindings - module Embedded - class Many < Binding + module Association + module Embedded + class EmbedMany + class Binding + include Bindable + # Binds a single document with the inverse relation. Used # specifically when appending to the proxy. # @@ -17,10 +19,10 @@ class Many < Binding # # @since 2.0.0.rc.1 def bind_one(doc) - doc.parentize(base) + doc.parentize(_base) binding do unless metadata.versioned? - doc.do_or_do_not(metadata.inverse_setter(target), base) + doc.do_or_do_not(_association.inverse_setter(_target), base) end end end diff --git a/lib/mongoid/core_ext/relations/embedded/many.rb b/lib/mongoid/core_ext/association/embedded/many.rb similarity index 90% rename from lib/mongoid/core_ext/relations/embedded/many.rb rename to lib/mongoid/core_ext/association/embedded/many.rb index cacd94a..49ab0a0 100755 --- a/lib/mongoid/core_ext/relations/embedded/many.rb +++ b/lib/mongoid/core_ext/association/embedded/many.rb @@ -1,7 +1,7 @@ module Mongoid - module Relations + module Association module Embedded - class Many < Relations::Many + class Many < Association::Many class << self # Get the valid options allowed with this relation. # diff --git a/lib/mongoid/core_ext/association/macros.rb b/lib/mongoid/core_ext/association/macros.rb new file mode 100755 index 0000000..77f8776 --- /dev/null +++ b/lib/mongoid/core_ext/association/macros.rb @@ -0,0 +1,16 @@ +module Mongoid + module Association + module Macros + module ClassMethods + alias_method :embedded_in_without_versioning_validation, :embedded_in + + def embedded_in(name, options = {}, &block) + if ancestors.include?(Mongoid::Versioning) + raise Errors::VersioningNotOnRoot, self + end + define_association!(__method__, name, options, &block) + end + end + end + end +end diff --git a/lib/mongoid/core_ext/association/options.rb b/lib/mongoid/core_ext/association/options.rb new file mode 100644 index 0000000..00a86e6 --- /dev/null +++ b/lib/mongoid/core_ext/association/options.rb @@ -0,0 +1,17 @@ +module Mongoid + module Association + module Options + # Is this relation using Mongoid's internal versioning system? + # + # @example Is this relation versioned? + # association.versioned? + # + # @return [ true, false ] If the relation uses Mongoid versioning. + # + # @since 2.1.0 + def versioned? + !!self[:versioned] + end + end + end +end diff --git a/lib/mongoid/core_ext/association/relatable.rb b/lib/mongoid/core_ext/association/relatable.rb new file mode 100644 index 0000000..49d2384 --- /dev/null +++ b/lib/mongoid/core_ext/association/relatable.rb @@ -0,0 +1,30 @@ +module Mongoid + module Association + module Relatable + alias_method :validate_without_versioned!, :validate! + + # Monkey patching the validate method to removes the :versioned option + # from the options hash. + # + # @param [ Hash ] options The options to validate. + def validate! + @orginal_options = @options.dup + @options = @options.except(:versioned) + validate_without_versioned! + @options = @orginal_options + end + + # Is this relation using Mongoid's internal versioning system? + # + # @example Is this relation versioned? + # metadata.versioned? + # + # @return [ true, false ] If the relation uses Mongoid versioning. + # + # @since 2.1.0 + def versioned? + !!@options[:versioned] + end + end + end +end diff --git a/lib/mongoid/core_ext/relations/macros.rb b/lib/mongoid/core_ext/relations/macros.rb deleted file mode 100755 index 34c45a5..0000000 --- a/lib/mongoid/core_ext/relations/macros.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Mongoid - module Relations - module Macros - module ClassMethods - # Adds the relation back to the parent document. This macro is - # necessary to set the references from the child back to the parent - # document. If a child does not define this relation calling - # persistence methods on the child object will cause a save to fail. - # - # @example Define the relation. - # - # class Person - # include Mongoid::Document - # embeds_many :addresses - # end - # - # class Address - # include Mongoid::Document - # embedded_in :person - # end - # - # @param [ Symbol ] name The name of the relation. - # @param [ Hash ] options The relation options. - # @param [ Proc ] block Optional block for defining extensions. - def embedded_in(name, options = {}, &block) - if ancestors.include?(Mongoid::Versioning) - raise Errors::VersioningNotOnRoot, self - end - meta = characterize(name, Embedded::In, options, &block) - self.embedded = true - relate(name, meta) - builder(name, meta).creator(name, meta) - add_counter_cache_callbacks(meta) if meta.counter_cached? - meta - end - end - end - end -end diff --git a/lib/mongoid/core_ext/relations/metadata.rb b/lib/mongoid/core_ext/relations/metadata.rb deleted file mode 100755 index 113da68..0000000 --- a/lib/mongoid/core_ext/relations/metadata.rb +++ /dev/null @@ -1,63 +0,0 @@ -module Mongoid - module Relations - class Metadata < Hash - # Since a lot of the information from the metadata is inferred and not - # explicitly stored in the hash, the inspection needs to be much more - # detailed. - # - # @example Inspect the metadata. - # metadata.inspect - # - # @return [ String ] Oodles of information in a nice format. - # - # @since 2.0.0.rc.1 - def inspect - %(# - ) - end - - # Is this relation using Mongoid's internal versioning system? - # - # @example Is this relation versioned? - # metadata.versioned? - # - # @return [ true, false ] If the relation uses Mongoid versioning. - # - # @since 2.1.0 - def versioned? - !!self[:versioned] - end - - # Get the inverse relation candidates. - # - # @api private - # - # @example Get the inverse relation candidates. - # metadata.inverse_relation_candidates - # - # @return [ Array ] The candidates. - # - # @since 3.0.0 - def inverse_relation_candidates - relations_metadata.select do |meta| - next if meta.versioned? || meta.name == name - meta.class_name == inverse_class_name - end - end - end - end -end diff --git a/lib/mongoid/core_ext/relations/options.rb b/lib/mongoid/core_ext/relations/options.rb deleted file mode 100644 index 46753e5..0000000 --- a/lib/mongoid/core_ext/relations/options.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Mongoid - module Relations - module Options - COMMON << :versioned - end - end -end diff --git a/lib/mongoid/core_ext/versioning.rb b/lib/mongoid/core_ext/versioning.rb index 3e07ba6..f1eede9 100644 --- a/lib/mongoid/core_ext/versioning.rb +++ b/lib/mongoid/core_ext/versioning.rb @@ -31,7 +31,6 @@ module Versioning # @example Revise the document. # person.revise # - # @todo Remove Mongoid 4 support. # @since 1.0.0 def revise previous = previous_revision @@ -43,16 +42,8 @@ def revise if version_max.present? && versions.length > version_max to_delete = versions.first version_to_delete = to_delete.version - if to_delete.respond_to?(:paranoid?) && to_delete.paranoid? - versions.delete_one(to_delete) - - query = collection.find(atomic_selector) - query.respond_to?(:update_one) ? - query.update_one('$pull' => { 'versions' => { 'version' => version_to_delete } }) : - query.update('$pull' => { 'versions' => { 'version' => version_to_delete } }) - else - versions.where(version: version_to_delete).delete_all - end + + versions.where(version: version_to_delete).delete_all end self.version = (version || 1) + 1 end @@ -131,18 +122,16 @@ def versionless # # @return [ Document, nil ] The previously saved document. # - # @todo Remove Mongoid 4 support. # @since 2.0.0 def previous_revision - options = respond_to?(:mongo_client) ? - mongo_client.options.symbolize_keys : - mongo_session.options + options = mongo_client.options.symbolize_keys _loading_revision do - self.class.unscoped - .with(options) - .where(_id: id) - .any_of({ version: version }, version: nil).first + self.class.with(options) do |m| + m.unscoped + .where(_id: id) + .any_of({ version: version }, version: nil).first + end end end diff --git a/lib/mongoid/versioning.rb b/lib/mongoid/versioning.rb index a7abdc0..f935b93 100644 --- a/lib/mongoid/versioning.rb +++ b/lib/mongoid/versioning.rb @@ -2,13 +2,13 @@ require 'mongoid/core_ext/fields/standard.rb' require 'mongoid/core_ext/fields/validators/macro.rb' require 'mongoid/core_ext/hierarchy.rb' -require 'mongoid/core_ext/relations/bindings/embedded/many.rb' -require 'mongoid/core_ext/relations/cascading.rb' -require 'mongoid/core_ext/relations/embedded/batchable.rb' -require 'mongoid/core_ext/relations/embedded/many.rb' -require 'mongoid/core_ext/relations/macros.rb' -require 'mongoid/core_ext/relations/metadata.rb' -require 'mongoid/core_ext/relations/options.rb' +require 'mongoid/core_ext/association/embedded/embeds_many/binding.rb' +require 'mongoid/core_ext/association/cascading.rb' +require 'mongoid/core_ext/association/embedded/batchable.rb' +require 'mongoid/core_ext/association/embedded/many.rb' +require 'mongoid/core_ext/association/macros.rb' +require 'mongoid/core_ext/association/options.rb' +require 'mongoid/core_ext/association/relatable.rb' require 'mongoid/core_ext/threaded/lifecycle.rb' require 'mongoid/core_ext/versioning.rb' diff --git a/lib/mongoid/versioning/version.rb b/lib/mongoid/versioning/version.rb index 9551128..cd40d92 100644 --- a/lib/mongoid/versioning/version.rb +++ b/lib/mongoid/versioning/version.rb @@ -1,5 +1,5 @@ module Mongoid module Versioning - VERSION = '2.1.0'.freeze + VERSION = '3.0.0'.freeze end end diff --git a/mongoid-versioning.code-workspace b/mongoid-versioning.code-workspace new file mode 100644 index 0000000..2cd5744 --- /dev/null +++ b/mongoid-versioning.code-workspace @@ -0,0 +1,13 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../mongoid-test" + } + ], + "settings": { + "ruby.rubocop.onSave": false + } +} diff --git a/mongoid-versioning.gemspec b/mongoid-versioning.gemspec index 121a493..c0462fb 100644 --- a/mongoid-versioning.gemspec +++ b/mongoid-versioning.gemspec @@ -15,8 +15,7 @@ Gem::Specification.new do |gem| gem.require_path = 'lib' gem.add_dependency 'activesupport', '>= 4.0' - gem.add_dependency 'mongoid', '>= 4.0.0', '< 7.0.0' - gem.add_development_dependency 'mongoid-paranoia', '>= 1.1.0', '< 3.0.0' + gem.add_dependency 'mongoid', '~> 7.3.5' gem.add_development_dependency 'rake', '~> 10.0' gem.add_development_dependency 'rspec', '~> 3' end diff --git a/spec/app/models/author.rb b/spec/app/models/author.rb index 9144111..8da12f4 100644 --- a/spec/app/models/author.rb +++ b/spec/app/models/author.rb @@ -1,6 +1,4 @@ class Author include Mongoid::Document field :name, type: String - - belongs_to :paranoid_post end diff --git a/spec/app/models/comment.rb b/spec/app/models/comment.rb index 3da7c8d..21f1901 100644 --- a/spec/app/models/comment.rb +++ b/spec/app/models/comment.rb @@ -7,9 +7,9 @@ class Comment # belongs_to :account # belongs_to :movie # belongs_to :rating - belongs_to :wiki_page + # belongs_to :wiki_page - belongs_to :commentable, polymorphic: true + belongs_to :commentable, polymorphic: true # wiki_page validates :title, presence: true # validates :movie, :rating, associated: true diff --git a/spec/app/models/paranoid_post.rb b/spec/app/models/paranoid_post.rb deleted file mode 100644 index 823e985..0000000 --- a/spec/app/models/paranoid_post.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'mongoid/paranoia' - -class ParanoidPost - include Mongoid::Document - include Mongoid::Versioning - include Mongoid::Paranoia - - max_versions 2 - - field :title, type: String - - attr_accessor :after_destroy_called, :before_destroy_called - - belongs_to :person - - has_and_belongs_to_many :tags - has_many :authors, dependent: :delete - has_many :titles, dependent: :restrict - - scope :recent, -> { where(created_at: { '$lt' => Time.now, '$gt' => 30.days.ago }) } - - before_destroy :before_destroy_stub - after_destroy :after_destroy_stub - - def before_destroy_stub - self.before_destroy_called = true - end - - def after_destroy_stub - self.after_destroy_called = true - end - - class << self - def old - where(created_at: { '$lt' => 30.days.ago }) - end - end -end diff --git a/spec/app/models/wiki_page.rb b/spec/app/models/wiki_page.rb index a010da7..e00fbe5 100644 --- a/spec/app/models/wiki_page.rb +++ b/spec/app/models/wiki_page.rb @@ -9,7 +9,7 @@ class WikiPage field :description, type: String, localize: true max_versions 5 - has_many :comments, dependent: :destroy, validate: false - has_many :child_pages, class_name: 'WikiPage', dependent: :delete, inverse_of: :parent_pages - belongs_to :parent_pages, class_name: 'WikiPage', inverse_of: :child_pages + has_many :comments, dependent: :destroy, as: :commentable, validate: false + has_many :child_pages, class_name: 'WikiPage', dependent: :delete_all, inverse_of: :parent_pages + belongs_to :parent_pages, class_name: 'WikiPage', inverse_of: :child_pages, optional: true end diff --git a/spec/mongoid/relations/macros_spec.rb b/spec/mongoid/association/macros_spec.rb similarity index 90% rename from spec/mongoid/relations/macros_spec.rb rename to spec/mongoid/association/macros_spec.rb index 8736bd5..1027d6d 100755 --- a/spec/mongoid/relations/macros_spec.rb +++ b/spec/mongoid/association/macros_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Mongoid::Relations::Macros do +describe Mongoid::Association::Macros do describe '.embedded_in' do context 'when the document is versioned' do it 'raises an error' do diff --git a/spec/mongoid/association/relatable_spec.rb b/spec/mongoid/association/relatable_spec.rb new file mode 100755 index 0000000..3c57b3c --- /dev/null +++ b/spec/mongoid/association/relatable_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe Mongoid::Association::Relatable do + describe '#versioned?' do + context 'when versioned is true' do + let(:association) do + Mongoid::Association::Embedded::EmbedsMany.new( + Address, + :versions, + versioned: true + ) + end + + it 'returns true' do + expect(association).to be_versioned + end + end + + context 'when versioned is false' do + let(:association) do + Mongoid::Association::Embedded::EmbedsMany.new( + Address, + :versions, + versioned: false + ) + end + + it 'returns false' do + expect(association).not_to be_versioned + end + end + + context 'when versioned is nil' do + let(:association) do + Mongoid::Association::Embedded::EmbedsMany.new( + Address, + :versions + ) + end + + it 'returns false' do + expect(association).not_to be_versioned + end + end + end +end diff --git a/spec/mongoid/relations/metadata_spec.rb b/spec/mongoid/relations/metadata_spec.rb deleted file mode 100755 index 5831fa4..0000000 --- a/spec/mongoid/relations/metadata_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'spec_helper' - -describe Mongoid::Relations::Metadata do - describe '#versioned?' do - context 'when versioned is true' do - let(:metadata) do - described_class.new( - name: :versions, - relation: Mongoid::Relations::Embedded::Many, - versioned: true - ) - end - - it 'returns true' do - expect(metadata).to be_versioned - end - end - - context 'when versioned is false' do - let(:metadata) do - described_class.new( - name: :versions, - relation: Mongoid::Relations::Embedded::Many, - versioned: false - ) - end - - it 'returns false' do - expect(metadata).not_to be_versioned - end - end - - context 'when versioned is nil' do - let(:metadata) do - described_class.new( - name: :versions, - relation: Mongoid::Relations::Embedded::Many - ) - end - - it 'returns false' do - expect(metadata).not_to be_versioned - end - end - end -end diff --git a/spec/mongoid/versioning_spec.rb b/spec/mongoid/versioning_spec.rb index 039b062..b40adee 100644 --- a/spec/mongoid/versioning_spec.rb +++ b/spec/mongoid/versioning_spec.rb @@ -174,7 +174,11 @@ class WikiPage let(:title) { 'my new wiki' } let!(:page) do - WikiPage.with(database: database_id_alt).create!(description: '1', title: title) + WikiPage.new(description: '1', title: title).tap do |page| + page.with(database: database_id_alt) do |page_alt| + page_alt.save! + end + end end context 'when the document is persisted once' do @@ -189,21 +193,28 @@ class WikiPage end it 'persists to specified database' do - expect(WikiPage.with(database: database_id_alt).find_by(title: title)).not_to be_nil + expect(WikiPage.with(database: database_id_alt) { |w| w.find_by(title: title) }).not_to be_nil end end context 'when the document is persisted more than once' do before do - 3.times { |n| page.with(database: database_id_alt).update_attribute(:description, n.to_s) } + skip "Mongoid 7 bug: https://jira.mongodb.org/browse/MONGOID-5379" + 3.times do |n| + page.with(database: database_id_alt) do |page_alt| + page_alt.update_attribute(:description, n.to_s) + end + end end it 'returns the number of versions' do - expect(page.version).to eq(4) + page.with(database: database_id_alt) do |page_alt| + expect(page_alt.version).to eq(4) + end end it 'persists to specified database' do - expect(WikiPage.with(database: database_id_alt).find_by(title: title)).not_to be_nil + expect(WikiPage.with(database: database_id_alt) { |w| w.find_by(title: title) }).not_to be_nil end it 'persists the versions to specified database' do @@ -212,7 +223,9 @@ class WikiPage end after do - WikiPage.with(database: database_id_alt).delete_all + WikiPage.with(database: database_id_alt) do |wiki_clazz| + wiki_clazz.delete_all + end end end end @@ -240,9 +253,10 @@ class WikiPage describe '#versions' do let(:page) do - WikiPage.create(title: '1', description: 'test') do |wiki| + pg = WikiPage.create!(title: '1', description: 'test') do |wiki| wiki.author = 'woodchuck' end + pg end context 'when saving the document ' do @@ -323,30 +337,10 @@ class WikiPage end context 'when saving over the number of maximum versions' do - context 'when the document is paranoid' do - let!(:post) do - ParanoidPost.create(title: 'test') - end - - before do - 3.times do |n| - post.update_attribute(:title, n.to_s) - end - end - - it 'only versions the maximum amount' do - expect(post.versions.target.size).to eq(2) - end - - it 'persists the changes' do - expect(post.reload.versions.target.size).to eq(2) - end - end - context 'when saving in succession' do before do 10.times do |n| - page.update_attribute(:title, n.to_s) + page.update(title: n.to_s) end end @@ -402,10 +396,6 @@ class WikiPage Comment.new(title: "Don't delete me!") end - let!(:orphaned) do - Comment.create(title: 'Annie') - end - before do page.comments << comment page.update_attribute(:title, '5') @@ -424,10 +414,6 @@ class WikiPage expect(from_db).to eq(comment) end - it 'does not delete related orphans' do - expect(Comment.find(orphaned.id)).to eq(orphaned) - end - it 'deletes the version' do expect(page.versions).to be_empty end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3fa8fd4..8e182ed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,9 +18,9 @@ HOST = ENV['MONGOID_SPEC_HOST'] PORT = ENV['MONGOID_SPEC_PORT'].to_i -# Moped.logger.level = Logger::DEBUG # TODO Remove Mongoid 4 support. # Mongoid.logger.level = Logger::DEBUG Mongo::Logger.logger.level = Logger::WARN if defined?(Mongo) +# Mongo::Monitoring::CommandLogSubscriber::LOG_STRING_LIMIT = 10_000 # When testing locally we use the database named mongoid_test. However when # tests are running in parallel on Travis we need to use different database @@ -53,6 +53,8 @@ def mongohq_connectable? module Rails class Application end + + def self.logger; nil; end end module MyApp