mongoid_includes 2.1.0 → 3.0.1

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
- SHA1:
3
- metadata.gz: e4f825eeb6529863b5cc3f92d4c1a633c082b4d3
4
- data.tar.gz: fa203f04024bc39551badc6b1980ab08aa9ed91a
2
+ SHA256:
3
+ metadata.gz: 1fe48fd0ac7d46fb5fe158bac336bfff3a8dde974f10629e3f632fcb3e475e18
4
+ data.tar.gz: db7fb5e32b06be95c8e133d48e8882249d623528a4bba05220f762b9a436adc2
5
5
  SHA512:
6
- metadata.gz: cc3ded0ad5fece9fbeff8fd87a36be9abbaeb09cd1e4892b669f3fadeb338bb9634df7a265e958fdc469c132c72bd5cee5c2242fa3a486284f5efdd9dc421b3d
7
- data.tar.gz: 1b430caed763a18e25b6c0896e385af0821f1ef8dde44b5dd0d0d7db4c4c138100d7329b87f5a82eb802ee08386273243fe7d5210482a47e49a33f58c9b4a2a4
6
+ metadata.gz: 0b2e350afb5abc0488ce860052522147cf97f15bd2bbbbda207094e00b2ec0807f281bd44af5f4991eca80139d7d23946a749bf40b3ded0e6ce5d40e870e8243
7
+ data.tar.gz: e8bd1062e78e5ed04c1a3c9d1377c218f40c06c4d7f85a917c5df90a588516168ac670c01b8f8f86fc8c7addc89bf84c4443248a3f0c3ba0af719a799ed64936
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Mongoid::Includes 3.0.0 (2020-10-29) ##
2
+
3
+ * Add support for Mongoid `7.1`.
4
+ * Avoid making a query when the foreign keys are empty.
5
+
1
6
  ## Mongoid::Includes 2.1.0 (2017-07-03) ##
2
7
 
3
8
  * Fix bug where `includes` ignores the `:with` option if the association is polymorphic. Thanks @nickcherry for the bug report!
data/README.md CHANGED
@@ -3,9 +3,9 @@ Mongoid::Includes
3
3
  [![Gem Version](https://badge.fury.io/rb/mongoid_includes.svg)](http://badge.fury.io/rb/mongoid_includes)
4
4
  [![Build Status](https://travis-ci.org/ElMassimo/mongoid_includes.svg)](https://travis-ci.org/ElMassimo/mongoid_includes)
5
5
  [![Test Coverage](https://codeclimate.com/github/ElMassimo/mongoid_includes/badges/coverage.svg)](https://codeclimate.com/github/ElMassimo/mongoid_includes)
6
- [![Code Climate](https://codeclimate.com/github/ElMassimo/mongoid_includes.png)](https://codeclimate.com/github/ElMassimo/mongoid_includes)
6
+ [![Code Climate](https://codeclimate.com/github/ElMassimo/mongoid_includes.svg)](https://codeclimate.com/github/ElMassimo/mongoid_includes)
7
7
  [![Inline docs](http://inch-ci.org/github/ElMassimo/mongoid_includes.svg)](http://inch-ci.org/github/ElMassimo/mongoid_includes)
8
- [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ElMassimo/mongoid_includes/blob/master/LICENSE.txt)
8
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ElMassimo/mongoid_includes/blob/main/LICENSE.txt)
9
9
 
10
10
  `Mongoid::Includes` improves eager loading in Mongoid, supporting polymorphic associations, and nested eager loading.
11
11
 
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  module Includes
3
- module Relations
3
+ module Association
4
4
 
5
5
  # Internal: Patch to the base class for eager load preload functions,
6
6
  # that interacts with the Mongoid::Includes::Inclusion as @medatada.
@@ -9,7 +9,7 @@ module Mongoid
9
9
  # Internal: Performs eager load and iterates over the preloaded documents
10
10
  # for the current relation.
11
11
  def each_loaded_document
12
- @metadata.load_documents_for(key, keys_from_docs).each do |doc|
12
+ @association.load_documents_for(key, keys_from_docs).each do |doc|
13
13
  yield doc
14
14
  end
15
15
  end
@@ -19,7 +19,7 @@ module Mongoid
19
19
  # loaded from.
20
20
  # from_class: - Necessary to solve ambiguity when doing two-level eager
21
21
  # load through a polymorphic relation.
22
- # loader: - An optional block that specifies how to load all the
22
+ # with: - An optional block that specifies how to load all the
23
23
  # related documents.
24
24
  #
25
25
  # Notes:
@@ -25,7 +25,7 @@ module Mongoid
25
25
 
26
26
  # Public: Returns true if the relation is a polymorphic belongs_to.
27
27
  def polymorphic_belongs_to?
28
- metadata.polymorphic? && metadata.relation == Mongoid::Relations::Referenced::In
28
+ metadata.polymorphic? && metadata.relation == Mongoid::Association::Referenced::BelongsTo::Proxy
29
29
  end
30
30
 
31
31
  # Public: Name of the relation from which a nested inclusion is performed.
@@ -33,11 +33,6 @@ module Mongoid
33
33
  @from ||= @options[:from]
34
34
  end
35
35
 
36
- # Internal: Proc that will return the included documents from a set of foreign keys.
37
- def loader
38
- @loader ||= @options[:loader]
39
- end
40
-
41
36
  # Internal: Proc that will modify the documents to include in the relation.
42
37
  def modifier
43
38
  @modifier ||= @options[:with]
@@ -46,12 +41,11 @@ module Mongoid
46
41
  # Public: Preloads the documents for the relation. Uses a custom block
47
42
  # if one was provided, or fetches them using the class and the foreign key.
48
43
  def load_documents_for(foreign_key, foreign_key_values)
49
- if loader
50
- loader.call(foreign_key, foreign_key_values)
51
- else
52
- docs = klass.any_in(foreign_key => foreign_key_values)
53
- modifier ? modifier.call(docs) : docs
54
- end
44
+ # NOTE: We assume the upstream code in Mongoid is removing nil keys.
45
+ return klass.none if foreign_key_values.empty?
46
+
47
+ docs = klass.any_in(foreign_key => foreign_key_values)
48
+ modifier ? modifier.call(docs) : docs
55
49
  end
56
50
 
57
51
  # Public: Clones the inclusion and changes the Mongoid::Metadata::Relation
@@ -60,10 +54,10 @@ module Mongoid
60
54
  # Returns an Inclusion that can be eager loaded as usual.
61
55
  def for_class_name(class_name)
62
56
  Inclusion.new metadata.clone.instance_eval { |relation_metadata|
63
- self[:class_name] = @class_name = class_name
64
- self[:polymorphic], self[:as], @polymorphic, @klass = nil
57
+ @options[:class_name] = @class_name = class_name
58
+ @options[:polymorphic], @options[:as], @polymorphic, @klass = nil
65
59
  self
66
- }, with: @modifier, loader: @loader
60
+ }, with: @modifier
67
61
  end
68
62
  end
69
63
  end
@@ -11,15 +11,20 @@ module Mongoid
11
11
  super Set.new(object)
12
12
  end
13
13
 
14
+ # Override: Avoid replacing existing inclusions.
15
+ def add(metadata)
16
+ include?(metadata) ? metadata : super
17
+ end
18
+
14
19
  # Public: Adds a new relation as an inclusion.
15
20
  #
16
21
  # Returns the added inclusion.
17
22
  def push(metadata, options = {})
18
23
  metadata = Inclusion.new(metadata, options) unless metadata.is_a?(Inclusion)
19
24
 
20
- # Ensure that an inclusion with a specified loader or modifier replaces
21
- # a previously specified loader.
22
- delete(metadata) if metadata.loader || metadata.modifier
25
+ # Ensure that an inclusion with a specified modifier replaces a
26
+ # previously specified one.
27
+ delete(metadata) if metadata.modifier
23
28
 
24
29
  # Internally it's a set so it won't add it twice.
25
30
  add(metadata)
@@ -5,6 +5,6 @@ module Mongoid
5
5
  module Includes
6
6
 
7
7
  # Public: This library will attempt to follow semantic versioning (whatever that's supposed to be).
8
- VERSION = '2.1.0'
8
+ VERSION = '3.0.1'
9
9
  end
10
10
  end
@@ -1,4 +1,4 @@
1
1
  require 'mongoid/includes/version'
2
2
  require 'mongoid/includes/criteria'
3
3
  require 'mongoid/includes/eager_load'
4
- require 'mongoid/includes/relations/eager'
4
+ require 'mongoid/includes/association/eager'
@@ -9,4 +9,4 @@ Mongoid::Contextual::Mongo.send :prepend, Mongoid::Includes::EagerLoad
9
9
  Mongoid::Contextual::Memory.send :prepend, Mongoid::Includes::EagerLoad
10
10
 
11
11
  Mongoid::Criteria.send :prepend, Mongoid::Includes::Criteria
12
- Mongoid::Relations::Eager::Base.send :prepend, Mongoid::Includes::Relations::Eager
12
+ Mongoid::Association::Referenced::Eager::Base.send :prepend, Mongoid::Includes::Association::Eager