mongoid_monkey 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3993c5f5fbdf5f1f30b9a824ffbd3447772bf68d
4
- data.tar.gz: 77026f83baf29fd6ebbcc63c7487818d3b0e081c
3
+ metadata.gz: f08602871ad1f23178fbcdb229ec18d40f385e70
4
+ data.tar.gz: 65f0267dc96fdec8fb505754097e9fd0a447df63
5
5
  SHA512:
6
- metadata.gz: a64eee10db5554f9f068522d8d571a7d42ffeabf1b14b84df8b8052eb1730311cc6f4964dd4bbfead35bbd24f078f68f8cd597a56d328993e26f11563a01bb6f
7
- data.tar.gz: 935a41fc23448476f511e49fba9fea2fd7e546625758bb26e78c6f7f2c784fedaae1392158dd2417701e0afd7551fa535264ff7d84d425e478b8d0ac9cebd77a
6
+ metadata.gz: 69ae75a8d969b6a215b65f51b1a42cc32712d3a80a86b605d1c1040190702240f78be792488774864e02c389814d4daf7f347e66cc10d9965c269d88820cd3ab
7
+ data.tar.gz: 6999c691a45888d763881006dc1d2674521671fac1fee6fa0d95390f4bc7f5807f671a601b45e7033ecef1ac4aa3fdf5f05b45faa72e42dae68ee333c31be33e
data/README.md CHANGED
@@ -31,7 +31,8 @@ If you would only like some of the patches, please copy and paste the code to yo
31
31
 
32
32
  | File | Description | 3 | 4 | 5 |
33
33
  | --- | --- | --- | --- | --- |
34
- | `atomic.rb` | Backport syntax change of atomic query methods. | ● | ○ | ○ |
34
+ | `aggregate_cursor.rb` | Aggregate methods (`max`, `sum`, etc) must use cursor in MongoDB 3.6+ | ● | ○ | ○ |
35
+ | `atomic.rb` | Backport syntax change of atomic query methods. Replace `$pushAll` with `$push + $each`. | ● | ○ | ○ |
35
36
  | `big_decimal.rb` | Fixes buggy BigDecimal behavior. | ● | ● | ● |
36
37
  | `db_commands.rb` | Use MongoDB 3.0+ command syntax; required for WiredTiger. | ● | ● | ○ |
37
38
  | `instrument.rb` | Backport instrumentation change to Moped 1. | ● | ○ | ○ |
@@ -40,6 +41,7 @@ If you would only like some of the patches, please copy and paste the code to yo
40
41
  | `embedded_touch.rb` (1) | Backport [Issue #3310](https://github.com/mongodb/mongoid/commit/a94c2f43573e58f973913c881ad9d11d62bf857c) from Mongoid 4 to add `:touch` option to `embedded_in`. | ● | ○ | ○ |
41
42
  | `embedded_touch.rb` (2) | Backport [PR #4392](https://github.com/mongodb/mongoid/pull/4392) from Mongoid 6 to fix an infinite loop issue related to `:touch` callbacks. | ● | ● | ● |
42
43
  | `index_options.rb` | Backport latest index valid index options from Mongoid 6. | ● | ● | ○ |
44
+ | `write_concern.rb` | Config `safe: true` should send WriteConcern: Acknowledged `w: 1` to database (and not `safe: true`). | ● | ○ | ○ |
43
45
 
44
46
  ### License
45
47
 
@@ -1,5 +1,6 @@
1
1
  require 'version'
2
2
 
3
+ require 'patches/aggregate_cursor'
3
4
  require 'patches/atomic'
4
5
  require 'patches/big_decimal'
5
6
  require 'patches/db_commands'
@@ -8,3 +9,4 @@ require 'patches/instrument'
8
9
  require 'patches/reorder'
9
10
  require 'patches/only_pluck_localized'
10
11
  require 'patches/embedded_touch'
12
+ require 'patches/write_concern'
@@ -0,0 +1,16 @@
1
+ # Use cursor for aggregation methods in Mongoid 3 in order to support MongoDB 3.6+
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ module Moped
6
+ class Collection
7
+ def aggregate(*pipeline)
8
+ pipeline.flatten!
9
+ command = { aggregate: name.to_s, pipeline: pipeline, cursor: {} }
10
+ result = database.session.command(command)['cursor']
11
+ result = result['firstBatch'] if result
12
+ result
13
+ end
14
+ end
15
+ end
16
+ end
@@ -69,14 +69,14 @@ module Atomic
69
69
  end
70
70
  alias_method_chain :push, :mongoid4
71
71
 
72
- def push_all_with_mongoid4(*args)
72
+ # Uses $push + $each rather than $pushAll
73
+ def push_all(*args)
73
74
  if args.length == 1 && args.first.is_a?(Hash)
74
- query.update_all("$pushAll" => collect_operations(args.first))
75
+ query.update_all("$push" => collect_operations(args.first, true))
75
76
  else
76
- push_all_without_mongoid4(*args)
77
+ query.update_all("$push" => { database_field_name(args[0]) => { "$each" => args[1] } })
77
78
  end
78
79
  end
79
- alias_method_chain :push_all, :mongoid4
80
80
 
81
81
  def rename_with_mongoid4(*args)
82
82
  if args.length == 1 && args.first.is_a?(Hash)
@@ -102,9 +102,9 @@ module Atomic
102
102
 
103
103
  private
104
104
 
105
- def collect_operations(ops)
105
+ def collect_operations(ops, use_each = false)
106
106
  ops.inject({}) do |operations, (field, value)|
107
- operations[database_field_name(field)] = value.mongoize
107
+ operations[database_field_name(field)] = use_each ? { '$each' => value.mongoize } : value.mongoize
108
108
  operations
109
109
  end
110
110
  end
@@ -116,7 +116,16 @@ module Mongoid
116
116
  module Persistence
117
117
  module Atomic
118
118
 
119
- # push_all is deprecated so not supported
119
+ # Replace usage of $pushAll with $push + $each
120
+ class PushAll
121
+ def persist
122
+ append_with("$push")
123
+ end
124
+
125
+ def operation(modifier)
126
+ { modifier => { path => value.is_a?(Array) ? { "$each" => value } : value}}
127
+ end
128
+ end
120
129
 
121
130
  def add_to_set_with_mongoid4(*args)
122
131
  if args.length == 1 && args.first.is_a?(Hash)
@@ -335,4 +344,30 @@ end
335
344
  end
336
345
  end
337
346
 
347
+ # Replace usage of $pushAll with $push + $each
348
+ module Mongoid
349
+ module Relations
350
+ module Embedded
351
+ module Batchable
352
+
353
+ def batch_insert(docs)
354
+ execute_batch_insert(docs, "$push", true)
355
+ end
356
+
357
+ def execute_batch_insert(docs, operation, use_each = false)
358
+ self.inserts_valid = true
359
+ inserts = pre_process_batch_insert(docs)
360
+ if insertable?
361
+ collection.find(selector).update(
362
+ positionally(selector, operation => { path => use_each ? { '$each' => inserts } : inserts })
363
+ )
364
+ post_process_batch_insert(docs)
365
+ end
366
+ inserts
367
+ end
368
+ end
369
+ end
370
+ end
371
+ end
372
+
338
373
  end
@@ -0,0 +1,10 @@
1
+ # Safe mode should set WriteConcern: Acknowledged in order to support MongoDB 3.4+
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ class TrueClass
6
+ def __safe_options__
7
+ { w: 1 }
8
+ end
9
+ end
10
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MongoidMonkey
2
- VERSION = '0.2.5'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -21,4 +21,5 @@ class Address
21
21
  field :name, localize: true
22
22
 
23
23
  embedded_in :addressable, polymorphic: true
24
+ embeds_many :locations, validate: false
24
25
  end
@@ -0,0 +1,14 @@
1
+ class Album
2
+ include Mongoid::Document
3
+
4
+ belongs_to :artist
5
+ before_destroy :set_parent_name
6
+
7
+ attr_accessor :before_add_called
8
+
9
+ private
10
+
11
+ def set_parent_name
12
+ artist.name = "destroyed"
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ class Animal
2
+ include Mongoid::Document
3
+
4
+ field :_id, type: String, default: ->{ name.try(:parameterize) }
5
+
6
+ field :name
7
+ field :height, type: Integer
8
+ field :weight, type: Integer
9
+ field :tags, type: Array
10
+
11
+ embedded_in :person
12
+ embedded_in :circus
13
+
14
+ validates_format_of :name, without: /\$\$\$/
15
+
16
+ accepts_nested_attributes_for :person
17
+
18
+ def tag_list
19
+ tags.join(", ")
20
+ end
21
+
22
+ def tag_list=(_tag_list)
23
+ self.tags = _tag_list.split(",").map(&:strip)
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ class Appointment
2
+ include Mongoid::Document
3
+ field :active, type: Boolean, default: true
4
+ field :timed, type: Boolean, default: true
5
+ embedded_in :person
6
+ default_scope where(active: true)
7
+ end
@@ -0,0 +1,66 @@
1
+ class Artist
2
+ include Mongoid::Document
3
+
4
+ attr_accessor :before_add_called, :after_add_called, :before_add_referenced_called, :after_add_referenced_called, :before_remove_embedded_called, :after_remove_embedded_called, :before_remove_referenced_called, :after_remove_referenced_called
5
+
6
+ field :name, type: String
7
+
8
+ embeds_many :songs, before_add: [ :before_add_song, Proc.new { |artist, song| song.before_add_called = true } ], before_remove: :before_remove_song
9
+ embeds_many :labels, after_add: :after_add_label, after_remove: :after_remove_label
10
+ has_many :albums, dependent: :destroy, before_add: [:before_add_album, Proc.new { |artist, album| album.before_add_called = true} ], after_add: :after_add_album, before_remove: :before_remove_album, after_remove: :after_remove_album
11
+
12
+ before_create :before_create_stub
13
+ after_create :create_songs
14
+ before_save :before_save_stub
15
+ before_destroy :before_destroy_stub
16
+
17
+ protected
18
+ def before_create_stub
19
+ true
20
+ end
21
+
22
+ def before_save_stub
23
+ true
24
+ end
25
+
26
+ def before_destroy_stub
27
+ true
28
+ end
29
+
30
+ def create_songs
31
+ 2.times { |n| songs.create!(title: "#{n}") }
32
+ end
33
+
34
+ def before_add_song(song)
35
+ @before_add_called = true
36
+ end
37
+
38
+ def after_add_label(label)
39
+ @after_add_called = true
40
+ end
41
+
42
+ def before_add_album(album)
43
+ @before_add_referenced_called = true
44
+ end
45
+
46
+ def after_add_album(album)
47
+ @after_add_referenced_called = true
48
+ end
49
+
50
+ def before_remove_song(song)
51
+ @before_remove_embedded_called = true
52
+ end
53
+
54
+ def after_remove_label(label)
55
+ @after_remove_embedded_called = true
56
+ end
57
+
58
+ def before_remove_album(album)
59
+ @before_remove_referenced_called = true
60
+ end
61
+
62
+ def after_remove_album(album)
63
+ @after_remove_referenced_called = true
64
+ end
65
+
66
+ end
@@ -0,0 +1,7 @@
1
+ class Circus
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ embeds_many :animals
7
+ end
@@ -0,0 +1,8 @@
1
+ class CountryCode
2
+ include Mongoid::Document
3
+
4
+ field :_id, type: Integer, default: ->{ code }
5
+
6
+ field :code, type: Integer
7
+ embedded_in :phone_number, class_name: "Phone"
8
+ end
@@ -0,0 +1,39 @@
1
+ class Label
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Updated::Short
4
+
5
+ field :name, type: String
6
+ field :after_create_called, type: Boolean, default: false
7
+ field :after_save_called, type: Boolean, default: false
8
+ field :after_update_called, type: Boolean, default: false
9
+ field :after_validation_called, type: Boolean, default: false
10
+
11
+ embedded_in :artist
12
+
13
+ after_create :after_create_stub
14
+ after_save :after_save_stub
15
+ after_update :after_update_stub
16
+ after_validation :after_validation_stub
17
+ before_validation :cleanup
18
+
19
+ def after_create_stub
20
+ self.after_create_called = true
21
+ end
22
+
23
+ def after_save_stub
24
+ self.after_save_called = true
25
+ end
26
+
27
+ def after_update_stub
28
+ self.after_update_called = true
29
+ end
30
+
31
+ def after_validation_stub
32
+ self.after_validation_called = true
33
+ end
34
+
35
+ private
36
+ def cleanup
37
+ self.name = self.name.downcase.capitalize
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ class Location
2
+ include Mongoid::Document
3
+ field :name
4
+ field :info, type: Hash
5
+ field :occupants, type: Array
6
+ field :number, type: Integer
7
+ embedded_in :address
8
+ end
@@ -1,6 +1,7 @@
1
1
  class Page
2
2
  include Mongoid::Document
3
-
3
+ embedded_in :quiz
4
+ embeds_many :page_questions
4
5
  embedded_in :book, touch: true
5
6
  field :content, :type => String
6
7
  end
@@ -0,0 +1,4 @@
1
+ class PageQuestion
2
+ include Mongoid::Document
3
+ embedded_in :page
4
+ end
@@ -39,6 +39,11 @@ class Person
39
39
  field :range, type: Range
40
40
 
41
41
  embeds_many :addresses, as: :addressable, validate: false
42
+ embeds_many :videos, order: [[ :title, :asc ]], validate: false
43
+ embeds_many :appointments, validate: false
44
+ embeds_many :symptoms, validate: false
45
+ embeds_many :phone_numbers, class_name: "Phone", validate: false
46
+ embeds_many :phones, store_as: :mobile_phones, validate: false
42
47
  embeds_one :name, as: :namable, validate: false do
43
48
  def extension
44
49
  "Testing"
@@ -0,0 +1,11 @@
1
+ class Phone
2
+ include Mongoid::Document
3
+
4
+ attr_accessor :number_in_observer
5
+
6
+ field :_id, type: String, default: ->{ number }
7
+
8
+ field :number
9
+ embeds_one :country_code
10
+ embedded_in :person
11
+ end
@@ -0,0 +1,10 @@
1
+ class Quiz
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Created
4
+ field :name, type: String
5
+ field :topic, type: String
6
+ embeds_many :pages
7
+
8
+ attr_accessible :topic, as: [ :default, :admin ]
9
+ attr_accessible :name, as: :default
10
+ end
@@ -0,0 +1,7 @@
1
+ class Role
2
+ include Mongoid::Document
3
+ field :name, type: String
4
+ belongs_to :user
5
+ belongs_to :post
6
+ recursively_embeds_many
7
+ end
@@ -0,0 +1,8 @@
1
+ class Song
2
+ include Mongoid::Document
3
+ field :title
4
+ embedded_in :artist
5
+
6
+ attr_accessor :before_add_called
7
+
8
+ end
@@ -0,0 +1,6 @@
1
+ class Symptom
2
+ include Mongoid::Document
3
+ field :name, type: String
4
+ embedded_in :person
5
+ default_scope asc(:name)
6
+ end
@@ -3,4 +3,5 @@ class User
3
3
  include Mongoid::Document
4
4
  field :name
5
5
  index name: 1
6
+ has_one :role, validate: false
6
7
  end