Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spec/reports
test/tmp
test/version_tmp
tmp
.tool-versions

# YARD artifacts
.yardoc
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.1.4
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ruby.rubocop.onSave": false
}
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
source 'https://rubygems.org'
gemspec

gem 'mongoid', "~> 7.3.5"
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions gemfiles/Mongoid_5.gemfile

This file was deleted.

6 changes: 0 additions & 6 deletions gemfiles/Mongoid_6.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/Mongoid_4.gemfile → gemfiles/Mongoid_73.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
gemspec path: '../'

gem 'mongoid', '~> 4.0'
gem 'mongoid', '~> 7.3.5'
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mongoid
module Relations
module Association
module Embedded
module Batchable
# Pre process the batch removal.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down
16 changes: 16 additions & 0 deletions lib/mongoid/core_ext/association/macros.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions lib/mongoid/core_ext/association/options.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions lib/mongoid/core_ext/association/relatable.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 0 additions & 39 deletions lib/mongoid/core_ext/relations/macros.rb

This file was deleted.

63 changes: 0 additions & 63 deletions lib/mongoid/core_ext/relations/metadata.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/mongoid/core_ext/relations/options.rb

This file was deleted.

27 changes: 8 additions & 19 deletions lib/mongoid/core_ext/versioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading