Skip to content
Merged
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/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches: [master]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: ['3.0', '3.2']
mongoid: ['7_4', '8_0', '8_1']
env:
BUNDLE_GEMFILE: gemfiles/mongoid_${{ matrix.mongoid }}.gemfile
CI: 'true'
services:
mongo:
image: mongo:7.0
ports:
- 27017:27017
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rspec
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tmp
*.o
*.a
mkmf.log
gemfiles/*.gemfile.lock
6 changes: 6 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.3.0
- Added GitHub Actions CI: Mongoid 7.4 / 8.0 / 8.1 x Ruby 3.0 / 3.2 against mongo:7.0
- Verified compatibility with Mongoid 8.0 and 8.1
- Fixed BulkSavable#bulk_assign NameError when no embeds are given
- Fixed Nesting#nested_save! argument forwarding on Ruby 3
- Loosened dev dependencies (mongoid >= 7.4, < 8.2; dropped bundler ~> 1.6)
# 0.2.6
- Upgraded Mongoid to 7.4.x
# 0.2.5
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/mongoid_7_4.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'mongoid', '~> 7.4.0'

gemspec path: '..'
5 changes: 5 additions & 0 deletions gemfiles/mongoid_8_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'mongoid', '~> 8.0.0'

gemspec path: '..'
5 changes: 5 additions & 0 deletions gemfiles/mongoid_8_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'mongoid', '~> 8.1.0'

gemspec path: '..'
2 changes: 1 addition & 1 deletion lib/mongoidal/bulk_savable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module BulkSavable
# If a specific set of embed relations are not provided then all embeds_many relations will be used
def bulk_assign(attributes, *embeds)
if embeds.blank?
embeds = self.class.relations.select {|k, v| v.is_a?(Mongoid::Association::Referenced::EmbesMany) }.keys
embeds = self.class.relations.select {|k, v| v.is_a?(Mongoid::Association::Embedded::EmbedsMany) }.keys
end

root_attrs = attributes.except(*embeds)
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoidal/nesting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def nested_assign(attributes, position: nil)
# and insert/update/delete them one by one based off of the existing data,
# instead of just replacing the entire array like a normal save would do.
# Any items not present within the new array will be destroyed
def nested_save!(attributes, *embeds, &block)
to_delete = nested_assign(attributes, *embeds)
def nested_save!(attributes, **options, &block)
Comment thread
roloand marked this conversation as resolved.
Resolved
to_delete = nested_assign(attributes, **options)
if valid?
block.call(to_delete) if block_given?
to_delete.each(&:destroy)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoidal/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Mongoidal
VERSION = "0.2.6"
VERSION = "0.3.0"
end
5 changes: 3 additions & 2 deletions mongoidal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "mongoid-slug"
spec.add_development_dependency "mongoid", '~> 7.4.0'
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "mongoid", ">= 7.4", "< 8.2"
spec.add_development_dependency "sidekiq"
spec.add_development_dependency "globalid"
# json 3 rejects the quirks_mode option that ActiveSupport 7.x passes to JSON.generate
spec.add_development_dependency "json", "< 3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-its"
Expand Down
41 changes: 41 additions & 0 deletions spec/bulk_savable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

class BulkParent
include Mongoid::Document
include Mongoidal::BulkSavable
field :name, type: String
embeds_many :kids, class_name: 'BulkKid'
end

class BulkKid
include Mongoid::Document
embedded_in :bulk_parent
field :name, type: String
end

describe Mongoidal::BulkSavable do
let(:parent) { BulkParent.create(name: 'Parent') }
let!(:kid1) { parent.kids.create(name: 'Kid 1') }
let!(:kid2) { parent.kids.create(name: 'Kid 2') }

# with no embeds given, every embeds_many relation is used
it 'updates, adds and flags deletes across embeds_many relations' do
to_delete = parent.bulk_assign(
'name' => 'Renamed',
'kids' => [
{ 'id' => kid1.id.to_s, 'name' => 'Kid 1b' },
{ 'id' => kid2.id.to_s, 'deleted' => true },
{ 'name' => 'Kid 3' }
]
)

expect(parent.name).to eq 'Renamed'
expect(parent.kids.map(&:name)).to match_array ['Kid 1b', 'Kid 2', 'Kid 3']
expect(to_delete).to eq [kid2]
end

it 'accepts an explicit list of embeds' do
parent.bulk_assign({ kids: [{ id: kid1.id, name: 'Kid 1b' }] }, :kids)
expect(parent.kids.find(kid1.id).name).to eq 'Kid 1b'
end
end
42 changes: 42 additions & 0 deletions spec/global_id_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

class GidParent
include Mongoid::Document
include Mongoidal::Identification
field :name, type: String
embeds_many :kids, class_name: 'GidKid'
end

class GidKid
include Mongoid::Document
include Mongoidal::Identification
embedded_in :gid_parent
field :name, type: String
end

GlobalID.app = 'mongoidal'

describe Mongoidal::Identification do
let(:parent) { GidParent.create(name: 'Parent') }
let(:kid) { parent.kids.create(name: 'Kid') }

def locate(doc)
Mongoidal::Locator.new(GlobalID.parse(doc.to_global_id.to_s)).locate
end

it 'round trips a root document' do
expect(locate(parent)).to eq parent
end

it 'round trips an embedded document through its root' do
expect(locate(kid)).to eq kid
end

it 'rebuilds a destroyed embedded document from its attributes' do
kid.destroy
gid = kid.to_global_id.to_s
found = Mongoidal::Locator.new(GlobalID.parse(gid)).locate
expect(found.name).to eq 'Kid'
expect(found).to be_destroyed
end
end
4 changes: 2 additions & 2 deletions spec/nesting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def self.find_within_collection(collection, attrs)
end

it 'should match using find_within_collection' do
parent.nested_save!(kids:[{name: 'Kid 1', age: 4}])
parent.nested_save!({kids: [{name: "Kid 1", age: 4}]})
expect(kid1.age).to eq 4
end

Expand Down Expand Up @@ -107,7 +107,7 @@ def self.find_within_collection(collection, attrs)
# this is standard functionality
it 'should update existing favorite' do
expect(parent.favorite.name).to eq 'Kid 1'
parent.nested_assign(favorite: {name: 'Kid 2'})
parent.nested_assign({favorite: {name: 'Kid 2'}})
expect(parent.favorite.id).to eq favorite.id
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end

config.after(:suite) do
Mongoid::Threaded.sessions[:default].drop if ENV['CI']
Mongoid.default_client.database.drop if ENV["CI"]
end

# rspec-expectations config goes here. You can use an alternate
Expand Down
Loading