mongoid_includes 1.0.5

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +17 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.md +49 -0
  5. data/Rakefile +31 -0
  6. data/lib/config/locales/en.yml +19 -0
  7. data/lib/mongoid/includes.rb +4 -0
  8. data/lib/mongoid/includes/criteria.rb +67 -0
  9. data/lib/mongoid/includes/eager_load.rb +50 -0
  10. data/lib/mongoid/includes/errors.rb +2 -0
  11. data/lib/mongoid/includes/errors/invalid_includes.rb +30 -0
  12. data/lib/mongoid/includes/errors/invalid_polymorphic_includes.rb +16 -0
  13. data/lib/mongoid/includes/inclusion.rb +64 -0
  14. data/lib/mongoid/includes/inclusions.rb +40 -0
  15. data/lib/mongoid/includes/relations/eager.rb +19 -0
  16. data/lib/mongoid/includes/version.rb +10 -0
  17. data/lib/mongoid_includes.rb +12 -0
  18. data/spec/mongoid/includes/criteria_spec.rb +25 -0
  19. data/spec/mongoid/includes/errors/invalid_includes_spec.rb +29 -0
  20. data/spec/mongoid/includes/errors/invalid_polymorphic_includes_spec.rb +29 -0
  21. data/spec/mongoid/includes/inclusions_spec.rb +23 -0
  22. data/spec/mongoid/includes/nested_inclusions_spec.rb +52 -0
  23. data/spec/mongoid/includes/simple_inclusions_spec.rb +1010 -0
  24. data/spec/spec_helper.rb +60 -0
  25. data/spec/support/config/mongoid.yml +27 -0
  26. data/spec/support/helpers.rb +47 -0
  27. data/spec/support/models/address.rb +69 -0
  28. data/spec/support/models/album.rb +9 -0
  29. data/spec/support/models/artist.rb +8 -0
  30. data/spec/support/models/band.rb +25 -0
  31. data/spec/support/models/game.rb +18 -0
  32. data/spec/support/models/musician.rb +10 -0
  33. data/spec/support/models/person.rb +72 -0
  34. data/spec/support/models/post.rb +11 -0
  35. data/spec/support/models/preference.rb +11 -0
  36. data/spec/support/models/record.rb +50 -0
  37. data/spec/support/models/song.rb +7 -0
  38. metadata +123 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10b99b346c88aaba6c21b90999b43d202fa5e7df
4
+ data.tar.gz: 84b0ad21bf1d12b739969f02f7a852415ab2f5aa
5
+ SHA512:
6
+ metadata.gz: a04a4f1ce0b3b430049a73a180fdbcfcd5ce20c05cc54bfae92dcd8b253c369dc6ee9982f0a766abec54c00503f68dbc2da12906fe93beb98bc6a99e70649096
7
+ data.tar.gz: 81011321c771106225cf87a5401ef1970f21b663311bfa15bd383e774624998d82b7cf7b9b5008528aead0a9e73c2e28cc31318e2188fdc863faebcba5bb378d
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ## Mongoid::Includes 1.0.3 (2015-10-10) ##
2
+
3
+ * Add support for Mongoid 5.
4
+
5
+ ## Mongoid::Includes 1.0.2 (2015-10-08) ##
6
+
7
+ * Fix error when using `merge` or `merge!` with a criteria and `includes`.
8
+ * Replace the internal structure with a Set to be more robust when avoiding duplicate relations.
9
+
10
+ ## Mongoid::Includes 1.0.1 (August 5, 2015) ##
11
+
12
+ * Fix error messages for polymorphic includes.
13
+ * Add :with option that receives the criteria that will be used to include documents.
14
+
15
+ ## Mongoid::Includes 1.0.0 (July 30, 2015) ##
16
+
17
+ * Initial Version.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Máximo Mussini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Mongoid::Includes
2
+ =====================
3
+ [![Gem Version](https://badge.fury.io/rb/mongoid_includes.svg)](http://badge.fury.io/rb/mongoid_includes)
4
+ [![Build Status](https://travis-ci.org/ElMassimo/mongoid_includes.svg)](https://travis-ci.org/ElMassimo/mongoid_includes)
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)
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)
9
+
10
+ `Mongoid::Includes` improves eager loading in Mongoid, supporting polymorphic associations, and up to two-levels of eager loading.
11
+
12
+ ### Usage
13
+
14
+ ```ruby
15
+ Album.includes(:songs).includes(:musicians, from: :band)
16
+
17
+ Band.includes(:albums, with: ->(albums) { albums.gt(release: 1970) })
18
+ ```
19
+
20
+ ## Advantages
21
+
22
+ * Avoid N+1 queries and get better performance.
23
+ * No boilerplate code is required.
24
+ * Can avoid loading all the related documents if necessary.
25
+
26
+
27
+ License
28
+ --------
29
+
30
+ Copyright (c) 2015 Máximo Mussini
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ "Software"), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
46
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
47
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
48
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
49
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
8
+ require 'mongoid/includes/version'
9
+
10
+ task :gem => :build
11
+ task :build do
12
+ system 'gem build mongoid_includes.gemspec'
13
+ end
14
+
15
+ task :install => :build do
16
+ system "sudo gem install mongoid_includes-#{Mongoid::Includes::VERSION}.gem"
17
+ end
18
+
19
+ task :release => :build do
20
+ system "git tag -a v#{Mongoid::Includes::VERSION} -m 'Tagging #{Mongoid::Includes::VERSION}'"
21
+ system 'git push --tags'
22
+ system "gem push mongoid_includes-#{Mongoid::Includes::VERSION}.gem"
23
+ system "rm mongoid_includes-#{Mongoid::Includes::VERSION}.gem"
24
+ end
25
+
26
+ RSpec::Core::RakeTask.new(:spec) do |spec|
27
+ spec.rspec_opts = ['--format progress']
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ end
30
+
31
+ task :default => :spec
@@ -0,0 +1,19 @@
1
+ en:
2
+ mongoid:
3
+ errors:
4
+ messages:
5
+ invalid_includes:
6
+ message: "Invalid includes directive: %{klass}.includes(%{args})"
7
+ summary: "Eager loading in Mongoid only supports providing arguments
8
+ to %{klass}.includes that are the names of relations on the %{klass}
9
+ model."
10
+ resolution: "Ensure that each parameter passed to %{klass}.includes is
11
+ a valid name of a relation on the %{klass} model. These are: %{relations}."
12
+
13
+ invalid_polymorphic_nested_includes:
14
+ message: "Invalid nested include directive: %{klass}.includes(%{args}, from: %{from}), %{from}
15
+ is a polymorphic relation."
16
+ summary: "Given that %{from} is a polymorphic relation, you must specify
17
+ :from_class when calling the method to resolve the ambiguity."
18
+ resolution: "Ensure that the first parameter passed to %{klass}.includes is
19
+ a non-polymorphic relation in the %{klass} model, or a explicit class in :from_class"
@@ -0,0 +1,4 @@
1
+ require 'mongoid/includes/version'
2
+ require 'mongoid/includes/criteria'
3
+ require 'mongoid/includes/eager_load'
4
+ require 'mongoid/includes/relations/eager'
@@ -0,0 +1,67 @@
1
+ require 'mongoid/includes/errors'
2
+ require 'mongoid/includes/inclusions'
3
+
4
+ module Mongoid
5
+ module Includes
6
+
7
+ # Internal: Adds and overrides methods in Mongoid::Criteria to enhance eager loading.
8
+ module Criteria
9
+
10
+ # Overrides: Get a list of criteria that are to be executed for eager loading.
11
+ def inclusions
12
+ @inclusions ||= Mongoid::Includes::Inclusions.new
13
+ end
14
+
15
+ # Public: Eager loads the specified associations.
16
+ #
17
+ # relations - The relations of the two-level relations to eager load.
18
+ # from: - The relation through which two-level relations are
19
+ # loaded from.
20
+ # from_class: - Necessary to solve ambiguity when doing two-level eager
21
+ # load through a polymorphic relation.
22
+ # loader: - An optional block that specifies how to load all the
23
+ # related documents.
24
+ #
25
+ # Notes:
26
+ # Eager loading brings all the documents into memory, so there is a
27
+ # sweet spot on the performance gains. Internal benchmarks show that
28
+ # eager loading becomes slower around 100k documents, but this will
29
+ # naturally depend on the specific application.
30
+ #
31
+ # Polymorphic belongs_to relations are supported, but will trigger a
32
+ # query for each collection of the matched documents types.
33
+ #
34
+ # Example:
35
+ # Album.includes(:musicians, from: :band)
36
+ #
37
+ # Returns the cloned Mongoid::Criteria.
38
+ def includes(*relations, **options)
39
+ if options[:from]
40
+ from_metadata = add_inclusion(klass, options[:from])
41
+ if from_metadata.polymorphic_belongs_to? && !options[:from_class]
42
+ raise Mongoid::Includes::Errors::InvalidPolymorphicIncludes.new(klass, relations, options)
43
+ end
44
+ end
45
+
46
+ owner_class = options[:from_class] || from_metadata.try!(:klass) || self.klass
47
+
48
+ relations.flatten.each do |relation|
49
+ add_inclusion(owner_class, relation, options)
50
+ end
51
+ clone
52
+ end
53
+
54
+ private
55
+
56
+ # Internal: Adds a new inclusion to the criteria.
57
+ #
58
+ # Returns the Mongoid::Includes::Inclusion for the included relation.
59
+ def add_inclusion(owner_class, relation, options = {})
60
+ unless metadata = owner_class.reflect_on_association(relation)
61
+ raise Mongoid::Includes::Errors::InvalidIncludes.new(owner_class, relation, options)
62
+ end
63
+ inclusions.include?(metadata) || inclusions.push(metadata, options)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,50 @@
1
+ module Mongoid
2
+ module Includes
3
+
4
+ # Public: Adds support for polymorphic and nested eager loading.
5
+ module EagerLoad
6
+
7
+ # Override: Partitions the inclusions into the different types.
8
+ def eager_load(docs)
9
+ return false unless eager_loadable?
10
+
11
+ nested_inclusions, inclusions = criteria.inclusions.partition(&:nested?)
12
+ polymorphic_inclusions, inclusions = inclusions.partition(&:polymorphic_belongs_to?)
13
+ full_preload(docs, inclusions, polymorphic_inclusions, nested_inclusions)
14
+
15
+ self.eager_loaded = true
16
+ end
17
+
18
+ # Internal: Performs the normal inclusions first, which allows to perform
19
+ # the polymorphic eager loading. Nested inclusions are performed at the end.
20
+ def full_preload(docs, inclusions, polymorphic_inclusions, nested_inclusions)
21
+ preload(inclusions, docs)
22
+
23
+ polymorphic_inclusions.each do |metadata|
24
+ preload_polymorphic(metadata, docs)
25
+ end
26
+
27
+ preload_nested(nested_inclusions, docs)
28
+ end
29
+
30
+ # Internal: Preloads each polymorphic includes, by grouping the documents by
31
+ # concrete type of the polymorphic relation, and making a query for each type.
32
+ def preload_polymorphic(inclusion, docs)
33
+ docs.group_by do |doc|
34
+ doc.send(inclusion.inverse_type) # The {name}_type attribute in polymorphic relations.
35
+ end.each do |type, docs|
36
+ concrete_inclusion = inclusion.for_class_name(type)
37
+ preload([concrete_inclusion], docs)
38
+ end
39
+ end
40
+
41
+ # Internal: The documents are grouped by the nested property, and all the
42
+ # includes by that property are processed as usual.
43
+ def preload_nested(nested_inclusions, docs)
44
+ nested_inclusions.group_by(&:from).each do |from, inclusions|
45
+ preload(inclusions, docs.map(&from).flatten)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongoid/includes/errors/invalid_includes'
2
+ require 'mongoid/includes/errors/invalid_polymorphic_includes'
@@ -0,0 +1,30 @@
1
+ module Mongoid
2
+ module Includes
3
+ module Errors
4
+
5
+ # Public: This error is raised when an invalid include is performed.
6
+ class InvalidIncludes < Mongoid::Errors::MongoidError
7
+
8
+ # Public: Composes a message from the class the includes would be
9
+ # performed on, the relations to be included, and the options.
10
+ def initialize(klass, args, options)
11
+ super compose_message(type, options.merge(
12
+ klass: klass, relations: klass.relations.keys, args: Array.wrap(args)
13
+ ))
14
+ end
15
+
16
+ # Internal: Key of the translation message
17
+ def type
18
+ :invalid_includes
19
+ end
20
+
21
+ # Overrides: Helps to keep the templates simple by using inspect on the options.
22
+ def compose_message(type, options)
23
+ super type, options.transform_values { |value|
24
+ value.is_a?(Array) ? value.map(&:inspect).join(', ') : value.inspect
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module Mongoid
2
+ module Includes
3
+ module Errors
4
+
5
+ # Public: This error is raised when an invalid nested inclusion is
6
+ # attempted through a polymorphic relation.
7
+ class InvalidPolymorphicIncludes < InvalidIncludes
8
+
9
+ # Internal: Key of the translation message
10
+ def type
11
+ :invalid_polymorphic_nested_includes
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,64 @@
1
+ module Mongoid
2
+ module Includes
3
+
4
+ # Public: Represents a relation that needs to be eager loaded.
5
+ class Inclusion < SimpleDelegator
6
+
7
+ # Public: Convenience getter for the wrapped metadata.
8
+ alias_method :metadata, :__getobj__
9
+
10
+ def initialize(metadata, options = {})
11
+ super(metadata)
12
+ @options = options
13
+ end
14
+
15
+ # Public: Returns true if the relation is not direct.
16
+ def nested?
17
+ !!from
18
+ end
19
+
20
+ # Public: Returns true if the relation is a polymorphic belongs_to.
21
+ def polymorphic_belongs_to?
22
+ metadata.polymorphic? && metadata.relation == Mongoid::Relations::Referenced::In
23
+ end
24
+
25
+ # Public: Name of the relation from which a nested inclusion is performed.
26
+ def from
27
+ @from ||= @options[:from]
28
+ end
29
+
30
+ # Internal: Proc that will return the included documents from a set of foreign keys.
31
+ def loader
32
+ @loader ||= @options[:loader]
33
+ end
34
+
35
+ # Internal: Proc that will modify the documents to include in the relation.
36
+ def modifier
37
+ @modifier ||= @options[:with]
38
+ end
39
+
40
+ # Public: Preloads the documents for the relation. Users a custom block
41
+ # if one was provided, or fetches them using the class and the foreign key.
42
+ def load_documents_for(foreign_key, foreign_key_values)
43
+ if loader
44
+ loader.call(foreign_key, foreign_key_values)
45
+ else
46
+ docs = klass.any_in(foreign_key => foreign_key_values)
47
+ modifier ? modifier.call(docs) : docs
48
+ end
49
+ end
50
+
51
+ # Public: Clones the inclusion and changes the Mongoid::Metadata::Relation
52
+ # that it wraps to make it non polymorphic and target a particular class.
53
+ #
54
+ # Returns an Inclusion that can be eager loaded as usual.
55
+ def for_class_name(class_name)
56
+ Inclusion.new metadata.clone.instance_eval { |relation_metadata|
57
+ self[:class_name] = @class_name = class_name
58
+ self[:polymorphic], self[:as], @polymorphic, @klass = nil
59
+ self
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,40 @@
1
+ require 'mongoid/includes/inclusion'
2
+
3
+ module Mongoid
4
+ module Includes
5
+
6
+ # Public: Collection of relations that need to be eager loaded.
7
+ class Inclusions < SimpleDelegator
8
+
9
+ # Internal: By default, it wraps an empty set.
10
+ def initialize(object = [])
11
+ super Set.new(object)
12
+ end
13
+
14
+ # Public: Adds a new relation as an inclusion.
15
+ #
16
+ # Returns the added inclusion.
17
+ def push(metadata, options = {})
18
+ metadata = Inclusion.new(metadata, options) unless metadata.is_a?(Inclusion)
19
+ add(metadata)
20
+ metadata
21
+ end
22
+
23
+ # Public: Checks if the collection already has an inclusion with the
24
+ # specified metadata.
25
+ def include?(metadata)
26
+ find { |inclusion| inclusion.metadata == metadata }
27
+ end
28
+
29
+ # Public: Returns the sum of the inclusions without any duplicates.
30
+ def +(inclusions)
31
+ Inclusions.new(union(inclusions))
32
+ end
33
+
34
+ # Public: Returns a new Inclusions without any duplicates.
35
+ def uniq
36
+ dup
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ module Mongoid
2
+ module Includes
3
+ module Relations
4
+
5
+ # Internal: Patch to the base class for eager load preload functions,
6
+ # that interacts with the Mongoid::Includes::Inclusion as @medatada.
7
+ module Eager
8
+
9
+ # Internal: Performs eager load and iterates over the preloaded documents
10
+ # for the current relation.
11
+ def each_loaded_document
12
+ @metadata.load_documents_for(key, keys_from_docs).each do |doc|
13
+ yield doc
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end