mongoid_auto_increment 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/FUNDING.yml +13 -0
- data/.travis.yml +34 -18
- data/Gemfile +2 -2
- data/README.md +88 -0
- data/Rakefile +19 -21
- data/VERSION +1 -1
- data/lib/mongoid_auto_increment/incrementor.rb +36 -18
- data/lib/mongoid_auto_increment.rb +3 -3
- data/mongoid_auto_increment.gemspec +28 -49
- data/spec/models/ingredient.rb +12 -0
- data/spec/models/invoice.rb +1 -2
- data/spec/models/order.rb +1 -1
- data/spec/models/post.rb +1 -1
- data/spec/models/recipe.rb +11 -0
- data/spec/mongoid_auto_increment_spec.rb +75 -19
- data/spec/spec_helper.rb +13 -15
- metadata +35 -27
- data/README.rdoc +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c73073d5d05e77517252042ad3a63bb484d5660d5e87cfe4b5ac1a58489d4f7
|
4
|
+
data.tar.gz: a8656612fed0486a237e94eef56c367d16b993dd485397dd32b4901b4eb65fbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 680eb520bbfe7602d7ba364584d781f495bc78c13415205edb9e3f49b51592131e79dc63a723fbe0b179f4bca5acde0b827585d2bcc9713c2cc73848b1d7af65
|
7
|
+
data.tar.gz: a2afacf125b79de0feb111c87835a18773905a1c1c17f7e95b3245fe8f18eccaf3844454e70886185ad5c44e3fb90173067c2f2c14470e42f966285688c117cb
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# These are supported funding model platforms
|
2
|
+
|
3
|
+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
4
|
+
patreon: _proton
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
7
|
+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
11
|
+
otechie: # Replace with a single Otechie username
|
12
|
+
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
13
|
+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
data/.travis.yml
CHANGED
@@ -5,24 +5,40 @@ language: ruby
|
|
5
5
|
|
6
6
|
cache: bundler
|
7
7
|
|
8
|
-
rvm:
|
9
|
-
- 2.
|
10
|
-
-
|
11
|
-
-
|
12
|
-
-
|
8
|
+
rvm:
|
9
|
+
- 3.2.0
|
10
|
+
- 3.1.0
|
11
|
+
- 3.0.0
|
12
|
+
- 2.7.0
|
13
|
+
- 2.3
|
14
|
+
- 2.2
|
15
|
+
- 2.1.1
|
16
|
+
- 2.0.0
|
13
17
|
|
14
|
-
env:
|
15
|
-
- MONGOID_VERSION=2
|
16
|
-
- MONGOID_VERSION=3
|
17
|
-
- MONGOID_VERSION=4
|
18
|
-
- MONGOID_VERSION=5
|
19
|
-
- MONGOID_VERSION=6
|
18
|
+
env:
|
19
|
+
- MONGOID_VERSION=2
|
20
|
+
- MONGOID_VERSION=3
|
21
|
+
- MONGOID_VERSION=4
|
22
|
+
- MONGOID_VERSION=5
|
23
|
+
- MONGOID_VERSION=6
|
24
|
+
- MONGOID_VERSION=7
|
25
|
+
- MONGOID_VERSION=8
|
20
26
|
|
21
27
|
matrix:
|
22
|
-
exclude:
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
exclude:
|
29
|
+
- rvm: 2.0.0
|
30
|
+
env: MONGOID_VERSION=6
|
31
|
+
- rvm: 2.1.1
|
32
|
+
env: MONGOID_VERSION=6
|
33
|
+
- rvm: 2.0.0
|
34
|
+
env: MONGOID_VERSION=7
|
35
|
+
- rvm: 2.1.1
|
36
|
+
env: MONGOID_VERSION=7
|
37
|
+
- rvm: 2.0.0
|
38
|
+
env: MONGOID_VERSION=8
|
39
|
+
- rvm: 2.1.1
|
40
|
+
env: MONGOID_VERSION=8
|
41
|
+
- rvm: 2.2
|
42
|
+
env: MONGOID_VERSION=8
|
43
|
+
- rvm: 2.3
|
44
|
+
env: MONGOID_VERSION=8
|
data/Gemfile
CHANGED
@@ -8,11 +8,11 @@ gem 'mongoid', '>= 2.0'
|
|
8
8
|
# Include everything needed to run rake, tests, features, etc.
|
9
9
|
group :development do
|
10
10
|
gem 'bundler'
|
11
|
+
gem 'database_cleaner-mongoid', '~> 2.0', '>= 2.0.1'
|
11
12
|
gem 'jeweler', '~> 2.1.1'
|
12
|
-
gem 'rake'
|
13
13
|
gem 'rack', '~> 1.6.4'
|
14
|
+
gem 'rake'
|
14
15
|
gem 'rdoc'
|
15
16
|
gem 'rspec', '>= 2.0.0'
|
16
17
|
gem 'simplecov', '>= 0.4.0', require: false
|
17
|
-
gem 'database_cleaner', '>= 0.8.0'
|
18
18
|
end
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Mongoid::AutoIncrement
|
2
|
+
|
3
|
+
Add SQL like auto-incrementing fields to your Mongoid documents. This gem is
|
4
|
+
inspired by http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/
|
5
|
+
and the [mongomapper_id2](https://github.com/phstc/mongomapper_id2) gem.
|
6
|
+
|
7
|
+
[![travis CI](https://secure.travis-ci.org/proton/mongoid_auto_increment.png)](http://travis-ci.org/proton/mongoid_auto_increment)
|
8
|
+
[![codetriage](https://www.codetriage.com/proton/mongoid_auto_increment/badges/users.svg)](https://www.codetriage.com/proton/mongoid_auto_increment)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add to Gemfile:
|
13
|
+
|
14
|
+
gem 'mongoid_auto_increment'
|
15
|
+
|
16
|
+
## Getting Started
|
17
|
+
|
18
|
+
Just add `auto_increment :field` to your Mongoid model where `:field` is the
|
19
|
+
name of the auto-incremented field you want to create. Example:
|
20
|
+
|
21
|
+
class Book
|
22
|
+
include Mongoid::Document
|
23
|
+
|
24
|
+
field :title
|
25
|
+
field :author
|
26
|
+
|
27
|
+
auto_increment :sequence
|
28
|
+
end
|
29
|
+
|
30
|
+
`auto_increment :sequence` will create a field of type `Integer` named
|
31
|
+
`sequence` for `Book`. Whenever an instance of the model is created (intially
|
32
|
+
saved to mongoDB), the `auto_increment` field will automatically be set to the
|
33
|
+
next number in the sequence.
|
34
|
+
|
35
|
+
You can add more than one auto-incremented field per model.
|
36
|
+
|
37
|
+
### Options
|
38
|
+
|
39
|
+
auto_increment :sequence, collection: :some_collection
|
40
|
+
|
41
|
+
`mongoid_auto_inc` keeps track of the current number in the sequence by
|
42
|
+
creating a separate document mongoDB to query and update. By default
|
43
|
+
`auto_increment` will save this document to a mongoDB collection called
|
44
|
+
`sequences`. If you wish to save to a different collection use the
|
45
|
+
`:collection` option to specify its name.
|
46
|
+
|
47
|
+
auto_increment :sequence, step: 5
|
48
|
+
|
49
|
+
If desired, you can override the `:step`, or increment amount (default is 1).
|
50
|
+
|
51
|
+
auto_increment :sequence, seed: 3333
|
52
|
+
|
53
|
+
Use the `:seed` option to set the initial value of the auto-incremented field.
|
54
|
+
The first number assigned from the sequence will be the next number after the
|
55
|
+
seed value (including the step - so for `seed: 1000, step: 5`, first value
|
56
|
+
will be 1005).
|
57
|
+
|
58
|
+
auto_increment :sequence, scope: :some_field_or_relation
|
59
|
+
auto_increment :sequence, scope: [ :some_field_or_relation, :another_field_or_relation ]
|
60
|
+
|
61
|
+
Use the `:scope` option to scope an auto-incremented field to a query.
|
62
|
+
Similar to Mongoid, the scope may contain one or many fields or relations.
|
63
|
+
Each unique scope will result in it's own stream of incremented values.
|
64
|
+
|
65
|
+
## Contributing to Mongoid::AutoIncrement
|
66
|
+
|
67
|
+
* Check out the latest master to make sure the feature hasn't been
|
68
|
+
implemented or the bug hasn't been fixed yet
|
69
|
+
* Check out the issue tracker to make sure someone already hasn't requested
|
70
|
+
it and/or contributed it
|
71
|
+
* Fork the project
|
72
|
+
* Start a feature/bugfix branch
|
73
|
+
* Commit and push until you are happy with your contribution
|
74
|
+
* Make sure to add tests for it. This is important so I don't break it in a
|
75
|
+
future version unintentionally.
|
76
|
+
* Please try not to mess with the Rakefile, version, or history. If you want
|
77
|
+
to have your own version, or is otherwise necessary, that is fine, but
|
78
|
+
please isolate to its own commit so I can cherry-pick around it.
|
79
|
+
|
80
|
+
## Support us
|
81
|
+
|
82
|
+
https://www.patreon.com/_proton
|
83
|
+
|
84
|
+
## Copyright
|
85
|
+
|
86
|
+
(c) 2011-2023 Peter Savichev (proton) (c) 2010-2011 Jeff Smith
|
87
|
+
|
88
|
+
See LICENSE.txt for further details.
|
data/Rakefile
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'rubygems'
|
4
2
|
require 'rake'
|
5
3
|
require 'bundler'
|
@@ -8,21 +6,21 @@ Bundler::GemHelper.install_tasks
|
|
8
6
|
begin
|
9
7
|
Bundler.setup(:default, :development)
|
10
8
|
rescue Bundler::BundlerError => e
|
11
|
-
|
12
|
-
|
9
|
+
warn e.message
|
10
|
+
warn 'Run `bundle install` to install missing gems'
|
13
11
|
exit e.status_code
|
14
12
|
end
|
15
13
|
|
16
14
|
require 'jeweler'
|
17
15
|
Jeweler::Tasks.new do |gem|
|
18
16
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
19
|
-
gem.name =
|
20
|
-
gem.homepage =
|
21
|
-
gem.license =
|
22
|
-
gem.summary =
|
23
|
-
gem.description =
|
24
|
-
gem.email =
|
25
|
-
gem.authors = [
|
17
|
+
gem.name = 'mongoid_auto_increment'
|
18
|
+
gem.homepage = 'http://github.com/proton/mongoid_auto_increment'
|
19
|
+
gem.license = 'MIT'
|
20
|
+
gem.summary = 'Auto-incrementing fields for Mongoid documents'
|
21
|
+
gem.description = 'Add SQL like auto-incrementing fields to your Mongoid documents.'
|
22
|
+
gem.email = 'psavichev@gmail.com'
|
23
|
+
gem.authors = ['Peter Savichev (proton)']
|
26
24
|
# dependencies defined in Gemfile
|
27
25
|
end
|
28
26
|
Jeweler::RubygemsDotOrgTasks.new
|
@@ -33,11 +31,11 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
33
31
|
spec.pattern = 'spec/**/*_spec.rb'
|
34
32
|
end
|
35
33
|
|
36
|
-
task :
|
34
|
+
task default: :spec
|
37
35
|
|
38
36
|
require 'rdoc/task'
|
39
37
|
RDoc::Task.new do |rdoc|
|
40
|
-
version = File.exist?('VERSION') ? File.read('VERSION') :
|
38
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
41
39
|
|
42
40
|
rdoc.rdoc_dir = 'rdoc'
|
43
41
|
rdoc.title = "mongoid_auto_increment #{version}"
|
@@ -45,15 +43,15 @@ RDoc::Task.new do |rdoc|
|
|
45
43
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
44
|
end
|
47
45
|
|
48
|
-
desc
|
46
|
+
desc 'Build gem'
|
49
47
|
task :build do
|
50
|
-
|
51
|
-
system
|
52
|
-
|
53
|
-
system
|
48
|
+
puts 'Regenerating gemspec'
|
49
|
+
system 'rake gemspec'
|
50
|
+
puts 'Building'
|
51
|
+
system 'gem build mongoid_auto_increment.gemspec'
|
54
52
|
end
|
55
53
|
|
56
|
-
desc
|
57
|
-
task :
|
58
|
-
|
54
|
+
desc 'Release gem'
|
55
|
+
task release: :build do
|
56
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
59
57
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
@@ -1,35 +1,36 @@
|
|
1
1
|
# This is a modified version of the code found on this blog post:
|
2
|
-
#
|
2
|
+
# http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/
|
3
3
|
module MongoidAutoIncrement
|
4
4
|
class Incrementor
|
5
5
|
class Sequence
|
6
|
-
def initialize(sequence, collection_name, seed, step)
|
6
|
+
def initialize(sequence, collection_name, seed, step, scope)
|
7
7
|
@sequence = sequence.to_s
|
8
8
|
@collection = collection_name.to_s
|
9
|
+
@scope = scope || {}
|
9
10
|
exists? || create(seed)
|
10
11
|
@step = step.to_i
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
+
def inc
|
14
15
|
if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '5'
|
15
16
|
collection.find(query).find_one_and_update({ '$inc' => { number: @step } }, new: true, upsert: true, return_document: :after)['number']
|
16
17
|
elsif defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3'
|
17
18
|
collection.find(query).modify({ '$inc' => { number: @step } }, new: true, upsert: true)['number']
|
18
19
|
else
|
19
20
|
opts = {
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
'query' => query,
|
22
|
+
'update' => { '$inc' => { 'number' => @step } },
|
23
|
+
'new' => true # return the modified document
|
23
24
|
}
|
24
|
-
collection.find_and_modify(opts)[
|
25
|
+
collection.find_and_modify(opts)['number']
|
25
26
|
end
|
26
|
-
|
27
|
+
end
|
27
28
|
|
28
29
|
def current
|
29
30
|
if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3'
|
30
|
-
collection.find(query).one[
|
31
|
+
collection.find(query).one['number']
|
31
32
|
else
|
32
|
-
collection.find_one(query)[
|
33
|
+
collection.find_one(query)['number']
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
@@ -41,9 +42,9 @@ module MongoidAutoIncrement
|
|
41
42
|
|
42
43
|
def create(number)
|
43
44
|
if ::Mongoid::VERSION >= '5'
|
44
|
-
collection.insert_one(query.merge(
|
45
|
+
collection.insert_one(query.merge('number' => number))
|
45
46
|
else
|
46
|
-
collection.insert(query.merge(
|
47
|
+
collection.insert(query.merge('number' => number))
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
@@ -58,18 +59,35 @@ module MongoidAutoIncrement
|
|
58
59
|
end
|
59
60
|
|
60
61
|
def query
|
61
|
-
|
62
|
+
@scope.merge('seq_name' => @sequence)
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
|
-
def initialize(options=nil)
|
66
|
-
end
|
66
|
+
def initialize(options = nil); end
|
67
67
|
|
68
|
-
def inc(sequence, options)
|
69
|
-
collection = options[:collection] ||
|
68
|
+
def inc(sequence, options, record)
|
69
|
+
collection = options[:collection] || 'sequences'
|
70
70
|
seed = options[:seed].to_i
|
71
71
|
step = options[:step] || 1
|
72
|
-
|
72
|
+
scope = resolve_scope(record, options[:scope])
|
73
|
+
|
74
|
+
Sequence.new(sequence, collection, seed, step, scope).inc
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def resolve_scope(record, scope)
|
80
|
+
Array(scope).each_with_object({}) do |scope_item, query|
|
81
|
+
reflection = record.class.reflect_on_association(scope_item)
|
82
|
+
|
83
|
+
if reflection
|
84
|
+
scope_value = record.send(reflection.foreign_key)
|
85
|
+
scope_item = reflection.foreign_key
|
86
|
+
query[scope_item] = scope_value
|
87
|
+
else
|
88
|
+
query[scope_item] = record.read_attribute(scope_item)
|
89
|
+
end
|
90
|
+
end
|
73
91
|
end
|
74
92
|
end
|
75
93
|
end
|
@@ -4,8 +4,8 @@ module MongoidAutoIncrement
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def auto_increment(name, options={})
|
8
|
-
field name, :
|
7
|
+
def auto_increment(name, options = {})
|
8
|
+
field name, type: Integer
|
9
9
|
|
10
10
|
unless defined? @@incrementor
|
11
11
|
@@incrementor = MongoidAutoIncrement::Incrementor.new
|
@@ -15,7 +15,7 @@ module MongoidAutoIncrement
|
|
15
15
|
seq_name = options[:name] || "#{self.name.downcase}_#{name}"
|
16
16
|
|
17
17
|
before_create do
|
18
|
-
send("#{name}=", @@incrementor.inc(seq_name, options))
|
18
|
+
send("#{name}=", @@incrementor.inc(seq_name, options, self))
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -2,28 +2,29 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: mongoid_auto_increment 0.1.
|
5
|
+
# stub: mongoid_auto_increment 0.1.6 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
|
-
s.name = "mongoid_auto_increment"
|
9
|
-
s.version = "0.1.
|
8
|
+
s.name = "mongoid_auto_increment".freeze
|
9
|
+
s.version = "0.1.6"
|
10
10
|
|
11
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
-
s.require_paths = ["lib"]
|
13
|
-
s.authors = ["Peter Savichev (proton)"]
|
14
|
-
s.date = "
|
15
|
-
s.description = "Add SQL like auto-incrementing fields to your Mongoid documents."
|
16
|
-
s.email = "psavichev@gmail.com"
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Peter Savichev (proton)".freeze]
|
14
|
+
s.date = "2023-03-08"
|
15
|
+
s.description = "Add SQL like auto-incrementing fields to your Mongoid documents.".freeze
|
16
|
+
s.email = "psavichev@gmail.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE",
|
19
|
-
"README.
|
19
|
+
"README.md"
|
20
20
|
]
|
21
21
|
s.files = [
|
22
|
+
".github/FUNDING.yml",
|
22
23
|
".rspec",
|
23
24
|
".travis.yml",
|
24
25
|
"Gemfile",
|
25
26
|
"LICENSE",
|
26
|
-
"README.
|
27
|
+
"README.md",
|
27
28
|
"Rakefile",
|
28
29
|
"VERSION",
|
29
30
|
"lib/mongoid_auto_increment.rb",
|
@@ -31,52 +32,30 @@ Gem::Specification.new do |s|
|
|
31
32
|
"mongoid_auto_increment.gemspec",
|
32
33
|
"spec/models/book.rb",
|
33
34
|
"spec/models/comment.rb",
|
35
|
+
"spec/models/ingredient.rb",
|
34
36
|
"spec/models/invoice.rb",
|
35
37
|
"spec/models/order.rb",
|
36
38
|
"spec/models/post.rb",
|
39
|
+
"spec/models/recipe.rb",
|
37
40
|
"spec/models/record.rb",
|
38
41
|
"spec/mongoid_auto_increment_spec.rb",
|
39
42
|
"spec/spec_helper.rb"
|
40
43
|
]
|
41
|
-
s.homepage = "http://github.com/proton/mongoid_auto_increment"
|
42
|
-
s.licenses = ["MIT"]
|
43
|
-
s.rubygems_version = "
|
44
|
-
s.summary = "Auto-incrementing fields for Mongoid documents"
|
44
|
+
s.homepage = "http://github.com/proton/mongoid_auto_increment".freeze
|
45
|
+
s.licenses = ["MIT".freeze]
|
46
|
+
s.rubygems_version = "3.4.6".freeze
|
47
|
+
s.summary = "Auto-incrementing fields for Mongoid documents".freeze
|
45
48
|
|
46
|
-
|
47
|
-
s.specification_version = 4
|
49
|
+
s.specification_version = 4
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
s.add_development_dependency(%q<database_cleaner>, [">= 0.8.0"])
|
59
|
-
else
|
60
|
-
s.add_dependency(%q<mongoid>, [">= 2.0"])
|
61
|
-
s.add_dependency(%q<bundler>, [">= 0"])
|
62
|
-
s.add_dependency(%q<jeweler>, ["~> 2.1.1"])
|
63
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
64
|
-
s.add_dependency(%q<rack>, ["~> 1.6.4"])
|
65
|
-
s.add_dependency(%q<rdoc>, [">= 0"])
|
66
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
67
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
68
|
-
s.add_dependency(%q<database_cleaner>, [">= 0.8.0"])
|
69
|
-
end
|
70
|
-
else
|
71
|
-
s.add_dependency(%q<mongoid>, [">= 2.0"])
|
72
|
-
s.add_dependency(%q<bundler>, [">= 0"])
|
73
|
-
s.add_dependency(%q<jeweler>, ["~> 2.1.1"])
|
74
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
75
|
-
s.add_dependency(%q<rack>, ["~> 1.6.4"])
|
76
|
-
s.add_dependency(%q<rdoc>, [">= 0"])
|
77
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
78
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
79
|
-
s.add_dependency(%q<database_cleaner>, [">= 0.8.0"])
|
80
|
-
end
|
51
|
+
s.add_runtime_dependency(%q<mongoid>.freeze, [">= 2.0"])
|
52
|
+
s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<database_cleaner-mongoid>.freeze, ["~> 2.0", ">= 2.0.1"])
|
54
|
+
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.1.1"])
|
55
|
+
s.add_development_dependency(%q<rack>.freeze, ["~> 1.6.4"])
|
56
|
+
s.add_development_dependency(%q<rake>.freeze, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<rdoc>.freeze, [">= 0"])
|
58
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 2.0.0"])
|
59
|
+
s.add_development_dependency(%q<simplecov>.freeze, [">= 0.4.0"])
|
81
60
|
end
|
82
61
|
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Ingredient
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Attributes::Dynamic if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '4'
|
4
|
+
|
5
|
+
field :name
|
6
|
+
field :type
|
7
|
+
|
8
|
+
belongs_to :recipe
|
9
|
+
|
10
|
+
auto_increment :order, scope: :recipe
|
11
|
+
auto_increment :cost, scope: %i[recipe type]
|
12
|
+
end
|
data/spec/models/invoice.rb
CHANGED
data/spec/models/order.rb
CHANGED
data/spec/models/post.rb
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'mongoid_auto_increment' do
|
4
|
-
|
5
4
|
before(:each) do
|
6
|
-
@book1 = Book.create :
|
7
|
-
@book2 = Book.create :
|
8
|
-
@book3 = Book.create :
|
9
|
-
@order1 = Order.create :
|
10
|
-
@order2 = Order.create :
|
11
|
-
@order3 = Order.create :
|
12
|
-
@post1 = Post.create :
|
13
|
-
@post2 = Post.create :
|
14
|
-
@post3 = Post.create :
|
15
|
-
@comment1 = @post1.comments.create :
|
16
|
-
@comment2 = @post1.comments.create :
|
17
|
-
@invoice1 = Invoice.create :
|
18
|
-
@invoice2 = Invoice.create :
|
19
|
-
@invoice3 = Invoice.create :
|
20
|
-
@record1 = Record.create :
|
21
|
-
@record2 = Record.create :
|
22
|
-
@record3 = Record.create :
|
5
|
+
@book1 = Book.create name: 'The Rails Way'
|
6
|
+
@book2 = Book.create name: 'The Ruby Programming Language '
|
7
|
+
@book3 = Book.create name: 'Metaprogramming Ruby'
|
8
|
+
@order1 = Order.create name: 'First Order'
|
9
|
+
@order2 = Order.create name: 'Second Order'
|
10
|
+
@order3 = Order.create name: 'Last Order'
|
11
|
+
@post1 = Post.create name: 'First Post'
|
12
|
+
@post2 = Post.create name: 'Second Post'
|
13
|
+
@post3 = Post.create name: 'Last Post'
|
14
|
+
@comment1 = @post1.comments.create name: 'First Comment'
|
15
|
+
@comment2 = @post1.comments.create name: 'Second Comment'
|
16
|
+
@invoice1 = Invoice.create name: 'First invoice'
|
17
|
+
@invoice2 = Invoice.create name: 'Second invoice'
|
18
|
+
@invoice3 = Invoice.create name: 'Third invoice'
|
19
|
+
@record1 = Record.create name: 'First record'
|
20
|
+
@record2 = Record.create name: 'Second record'
|
21
|
+
@record3 = Record.create name: 'Third record'
|
23
22
|
end
|
24
23
|
|
25
24
|
describe 'single auto-increment field' do
|
@@ -120,7 +119,64 @@ describe 'mongoid_auto_increment' do
|
|
120
119
|
end
|
121
120
|
|
122
121
|
it 'should have a sequence name of "Record"' do
|
123
|
-
expect(collection.find(:
|
122
|
+
expect(collection.find(seq_name: 'Record').first).not_to eq nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe 'auto-increment with scope' do
|
127
|
+
context 'when scope is a relation' do
|
128
|
+
before do
|
129
|
+
@recipe1 = Recipe.create
|
130
|
+
@recipe2 = Recipe.create
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should have multiple with the same value' do
|
134
|
+
expect(@recipe1.ingredients.create.order).to eq 1
|
135
|
+
expect(@recipe2.ingredients.create.order).to eq 1
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should increment each scope separately' do
|
139
|
+
3.times.each { |n| @recipe1.ingredients.create(name: n + 1) }
|
140
|
+
9.times.each { |n| @recipe2.ingredients.create(name: n + 1) }
|
141
|
+
expect(Ingredient.find_by(recipe: @recipe2, name: 3).order).to eq 3
|
142
|
+
expect(Ingredient.find_by(recipe: @recipe2, name: 9).order).to eq 9
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when scope is a field' do
|
147
|
+
it 'should have multiple with the same value' do
|
148
|
+
expect(Recipe.create(chef: 'Jack').rank).to eq 1
|
149
|
+
expect(Recipe.create(chef: 'Jill').rank).to eq 1
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should increment each scope separately' do
|
153
|
+
3.times.each { |n| Recipe.create(chef: 'Jack', name: n + 1) }
|
154
|
+
9.times.each { |n| Recipe.create(chef: 'Jill', name: n + 1) }
|
155
|
+
expect(Recipe.find_by(chef: 'Jack', name: 3).rank).to eq 3
|
156
|
+
expect(Recipe.find_by(chef: 'Jill', name: 9).rank).to eq 9
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context 'when scope is an array' do
|
161
|
+
before do
|
162
|
+
@recipe1 = Recipe.create
|
163
|
+
@recipe2 = Recipe.create
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'should have multiple with the same value' do
|
167
|
+
expect(Ingredient.create(recipe: @recipe1, type: 'greens').cost).to eq 1
|
168
|
+
expect(Ingredient.create(recipe: @recipe1, type: 'meats').cost).to eq 1
|
169
|
+
expect(Ingredient.create(recipe: @recipe2, type: 'greens').cost).to eq 1
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should increment each scope separately' do
|
173
|
+
2.times.each { |n| Ingredient.create(recipe: @recipe1, type: 'greens', name: n + 1) }
|
174
|
+
3.times.each { |n| Ingredient.create(recipe: @recipe1, type: 'meats', name: n + 1) }
|
175
|
+
5.times.each { |n| Ingredient.create(recipe: @recipe2, type: 'greens', name: n + 1) }
|
176
|
+
expect(Ingredient.find_by(recipe: @recipe1, type: 'greens', name: 2).cost).to eq 2
|
177
|
+
expect(Ingredient.find_by(recipe: @recipe1, type: 'meats', name: 3).cost).to eq 3
|
178
|
+
expect(Ingredient.find_by(recipe: @recipe2, type: 'greens', name: 5).cost).to eq 5
|
179
|
+
end
|
124
180
|
end
|
125
181
|
end
|
126
182
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
3
|
|
4
|
-
MODELS = File.join(File.dirname(__FILE__),
|
4
|
+
MODELS = File.join(File.dirname(__FILE__), 'models')
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
6
|
+
require 'rubygems'
|
7
|
+
require 'mongoid'
|
8
|
+
require 'mongoid_auto_increment'
|
9
|
+
require 'database_cleaner/mongoid'
|
10
|
+
require 'simplecov'
|
11
11
|
|
12
12
|
SimpleCov.start
|
13
13
|
|
@@ -15,25 +15,23 @@ Dir["#{MODELS}/*.rb"].each { |f| require f }
|
|
15
15
|
|
16
16
|
if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION > '3'
|
17
17
|
Mongoid.configure do |config|
|
18
|
-
config.connect_to
|
18
|
+
config.connect_to 'mongoid_auto_increment_test'
|
19
19
|
end
|
20
20
|
else
|
21
|
-
Mongoid.config.master = Mongo::Connection.new.db(
|
21
|
+
Mongoid.config.master = Mongo::Connection.new.db('mongoid_auto_increment_test')
|
22
22
|
end
|
23
23
|
Mongoid.logger = Logger.new($stdout)
|
24
24
|
|
25
|
-
DatabaseCleaner.orm = "mongoid"
|
26
|
-
|
27
25
|
RSpec.configure do |config|
|
28
26
|
config.before(:all) do
|
29
|
-
DatabaseCleaner.strategy = :
|
27
|
+
DatabaseCleaner[:mongoid].strategy = :deletion
|
30
28
|
end
|
31
29
|
|
32
30
|
config.before(:each) do
|
33
|
-
DatabaseCleaner.start
|
31
|
+
DatabaseCleaner[:mongoid].start
|
34
32
|
end
|
35
33
|
|
36
34
|
config.after(:each) do
|
37
|
-
DatabaseCleaner.clean
|
35
|
+
DatabaseCleaner[:mongoid].clean
|
38
36
|
end
|
39
|
-
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_auto_increment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Savichev (proton)
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -39,33 +39,39 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: database_cleaner-mongoid
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 2.
|
47
|
+
version: '2.0'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.0.1
|
48
51
|
type: :development
|
49
52
|
prerelease: false
|
50
53
|
version_requirements: !ruby/object:Gem::Requirement
|
51
54
|
requirements:
|
52
55
|
- - "~>"
|
53
56
|
- !ruby/object:Gem::Version
|
54
|
-
version: 2.
|
57
|
+
version: '2.0'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.0.1
|
55
61
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
62
|
+
name: jeweler
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
58
64
|
requirements:
|
59
|
-
- - "
|
65
|
+
- - "~>"
|
60
66
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
67
|
+
version: 2.1.1
|
62
68
|
type: :development
|
63
69
|
prerelease: false
|
64
70
|
version_requirements: !ruby/object:Gem::Requirement
|
65
71
|
requirements:
|
66
|
-
- - "
|
72
|
+
- - "~>"
|
67
73
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
74
|
+
version: 2.1.1
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
76
|
name: rack
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,7 +87,7 @@ dependencies:
|
|
81
87
|
- !ruby/object:Gem::Version
|
82
88
|
version: 1.6.4
|
83
89
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
90
|
+
name: rake
|
85
91
|
requirement: !ruby/object:Gem::Requirement
|
86
92
|
requirements:
|
87
93
|
- - ">="
|
@@ -95,60 +101,61 @@ dependencies:
|
|
95
101
|
- !ruby/object:Gem::Version
|
96
102
|
version: '0'
|
97
103
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
104
|
+
name: rdoc
|
99
105
|
requirement: !ruby/object:Gem::Requirement
|
100
106
|
requirements:
|
101
107
|
- - ">="
|
102
108
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
109
|
+
version: '0'
|
104
110
|
type: :development
|
105
111
|
prerelease: false
|
106
112
|
version_requirements: !ruby/object:Gem::Requirement
|
107
113
|
requirements:
|
108
114
|
- - ">="
|
109
115
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
116
|
+
version: '0'
|
111
117
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
118
|
+
name: rspec
|
113
119
|
requirement: !ruby/object:Gem::Requirement
|
114
120
|
requirements:
|
115
121
|
- - ">="
|
116
122
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.
|
123
|
+
version: 2.0.0
|
118
124
|
type: :development
|
119
125
|
prerelease: false
|
120
126
|
version_requirements: !ruby/object:Gem::Requirement
|
121
127
|
requirements:
|
122
128
|
- - ">="
|
123
129
|
- !ruby/object:Gem::Version
|
124
|
-
version: 0.
|
130
|
+
version: 2.0.0
|
125
131
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
132
|
+
name: simplecov
|
127
133
|
requirement: !ruby/object:Gem::Requirement
|
128
134
|
requirements:
|
129
135
|
- - ">="
|
130
136
|
- !ruby/object:Gem::Version
|
131
|
-
version: 0.
|
137
|
+
version: 0.4.0
|
132
138
|
type: :development
|
133
139
|
prerelease: false
|
134
140
|
version_requirements: !ruby/object:Gem::Requirement
|
135
141
|
requirements:
|
136
142
|
- - ">="
|
137
143
|
- !ruby/object:Gem::Version
|
138
|
-
version: 0.
|
144
|
+
version: 0.4.0
|
139
145
|
description: Add SQL like auto-incrementing fields to your Mongoid documents.
|
140
146
|
email: psavichev@gmail.com
|
141
147
|
executables: []
|
142
148
|
extensions: []
|
143
149
|
extra_rdoc_files:
|
144
150
|
- LICENSE
|
145
|
-
- README.
|
151
|
+
- README.md
|
146
152
|
files:
|
153
|
+
- ".github/FUNDING.yml"
|
147
154
|
- ".rspec"
|
148
155
|
- ".travis.yml"
|
149
156
|
- Gemfile
|
150
157
|
- LICENSE
|
151
|
-
- README.
|
158
|
+
- README.md
|
152
159
|
- Rakefile
|
153
160
|
- VERSION
|
154
161
|
- lib/mongoid_auto_increment.rb
|
@@ -156,9 +163,11 @@ files:
|
|
156
163
|
- mongoid_auto_increment.gemspec
|
157
164
|
- spec/models/book.rb
|
158
165
|
- spec/models/comment.rb
|
166
|
+
- spec/models/ingredient.rb
|
159
167
|
- spec/models/invoice.rb
|
160
168
|
- spec/models/order.rb
|
161
169
|
- spec/models/post.rb
|
170
|
+
- spec/models/recipe.rb
|
162
171
|
- spec/models/record.rb
|
163
172
|
- spec/mongoid_auto_increment_spec.rb
|
164
173
|
- spec/spec_helper.rb
|
@@ -166,7 +175,7 @@ homepage: http://github.com/proton/mongoid_auto_increment
|
|
166
175
|
licenses:
|
167
176
|
- MIT
|
168
177
|
metadata: {}
|
169
|
-
post_install_message:
|
178
|
+
post_install_message:
|
170
179
|
rdoc_options: []
|
171
180
|
require_paths:
|
172
181
|
- lib
|
@@ -181,9 +190,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
190
|
- !ruby/object:Gem::Version
|
182
191
|
version: '0'
|
183
192
|
requirements: []
|
184
|
-
|
185
|
-
|
186
|
-
signing_key:
|
193
|
+
rubygems_version: 3.4.6
|
194
|
+
signing_key:
|
187
195
|
specification_version: 4
|
188
196
|
summary: Auto-incrementing fields for Mongoid documents
|
189
197
|
test_files: []
|
data/README.rdoc
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
= Mongoid::AutoIncrement
|
2
|
-
|
3
|
-
Add SQL like auto-incrementing fields to your Mongoid documents.
|
4
|
-
This gem is inspired by http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/ and the mongomapper_id2[https://github.com/phstc/mongomapper_id2] gem.
|
5
|
-
|
6
|
-
{<img src="https://secure.travis-ci.org/proton/mongoid_auto_increment.png" />}[http://travis-ci.org/proton/mongoid_auto_increment]
|
7
|
-
|
8
|
-
== Installation
|
9
|
-
|
10
|
-
Add to Gemfile:
|
11
|
-
|
12
|
-
gem 'mongoid_auto_increment'
|
13
|
-
|
14
|
-
== Getting Started
|
15
|
-
|
16
|
-
Just add <tt>auto_increment :field</tt> to your Mongoid model where <tt>:field</tt> is the name of the auto-incremented field you want to create.
|
17
|
-
Example:
|
18
|
-
|
19
|
-
class Book
|
20
|
-
include Mongoid::Document
|
21
|
-
|
22
|
-
field :title
|
23
|
-
field :author
|
24
|
-
|
25
|
-
auto_increment :sequence
|
26
|
-
end
|
27
|
-
|
28
|
-
<tt>auto_increment :sequence</tt> will create a field of type <tt>Integer</tt> named <tt>sequence</tt> for <tt>Book</tt>. Whenever an instance of the model is created (intially saved to mongoDB), the <tt>auto_increment</tt> field will automatically be set to the next number in the sequence.
|
29
|
-
|
30
|
-
You can add more than one auto-incremented field per model.
|
31
|
-
|
32
|
-
=== Options
|
33
|
-
|
34
|
-
auto_increment :sequence, :collection => :some_collection
|
35
|
-
|
36
|
-
<tt>mongoid_auto_inc</tt> keeps track of the current number in the sequence by creating a separate document mongoDB to query and update. By default <tt>auto_increment</tt> will save this document to a mongoDB collection called <tt>sequences</tt>. If you wish to save to a different collection use the <tt>:collection</tt> option to specify its name.
|
37
|
-
|
38
|
-
auto_increment :sequence, :step => 5
|
39
|
-
|
40
|
-
If desired, you can override the <tt>:step</tt>, or increment amount (default is 1).
|
41
|
-
|
42
|
-
auto_increment :sequence, :seed => 3333
|
43
|
-
|
44
|
-
Use the <tt>:seed</tt> option to set the initial value of the auto-incremented field. The first number assigned from the sequence will be the next number after the seed value (including the step - so for <tt>:seed => 1000, :step => 5</tt>, first value will be 1005).
|
45
|
-
|
46
|
-
== Contributing to Mongoid::AutoIncrement
|
47
|
-
|
48
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
49
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
50
|
-
* Fork the project
|
51
|
-
* Start a feature/bugfix branch
|
52
|
-
* Commit and push until you are happy with your contribution
|
53
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
54
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
55
|
-
|
56
|
-
== Support us
|
57
|
-
|
58
|
-
|
59
|
-
{<img src="http://api.flattr.com/button/flattr-badge-large.png" />}[https://flattr.com/submit/auto?user_id=proton&url=https://github.com/proton/mongoid_auto_increment/&title=MongoidAutoIncrement&language=&tags=github&category=software]
|
60
|
-
|
61
|
-
|
62
|
-
== Copyright
|
63
|
-
|
64
|
-
(c) 2011 Peter Savichev (proton)
|
65
|
-
(c) 2010-2011 Jeff Smith
|
66
|
-
|
67
|
-
See LICENSE.txt for further details.
|