mongoid_embed_finder 0.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 +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/mongoid_embed_finder/nested_builder.rb +28 -0
- data/lib/mongoid_embed_finder/projectors/base.rb +13 -0
- data/lib/mongoid_embed_finder/projectors/single.rb +11 -0
- data/lib/mongoid_embed_finder/relation_discovery.rb +29 -0
- data/lib/mongoid_embed_finder/runner.rb +53 -0
- data/lib/mongoid_embed_finder/version.rb +3 -0
- data/lib/mongoid_embed_finder.rb +9 -0
- data/mongoid_embed_finder.gemspec +32 -0
- data/spec/mongoid_embed_finder/nested_builder_spec.rb +68 -0
- data/spec/mongoid_embed_finder/projector_spec.rb +15 -0
- data/spec/mongoid_embed_finder/projectors/base_spec.rb +15 -0
- data/spec/mongoid_embed_finder/projectors/single_spec.rb +13 -0
- data/spec/mongoid_embed_finder/relation_discovery_spec.rb +16 -0
- data/spec/mongoid_embed_finder/runner_spec.rb +43 -0
- data/spec/spec_helper.rb +52 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ecbb38b95a0203aecd5993d79c10dbb28ee06f5c
|
4
|
+
data.tar.gz: 7fbf881de4a588a8676920cfb6625fc9e2699b10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6db36b453e52cb4b3343db6b3e19ac60e032eee512ef5c8811ca8b5a8af618e320d5e9b7454d3ee00b04471eac8f5c1aa6bb861bb1751ed5cc8b7adb0d03e98c
|
7
|
+
data.tar.gz: beeaf658226886300e6b0f0ef5b6dd69f5634db2435fa4509cbcf842f92f09e8cd5f8092f948698f23d7a577ec05902d21ad4f71b9b5d813dcb1eb03d553925d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2013 Artur Hebda
|
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/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Artur Hebda
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# MongoidEmbedFinder
|
2
|
+
|
3
|
+
This simple gem lets you find embedded documents easily. It does not instantiate parent record with a whole bunch of embedded documents. Instead it instantiates found embedded documents first and then sets parent association.
|
4
|
+
|
5
|
+
At the moment it allows you to find only one embedded document and assumes relation `embeds_many`.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mongoid_embed_finder'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mongoid_embed_finder
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Let's say that you have defined two classes as follows:
|
24
|
+
|
25
|
+
class Door
|
26
|
+
include Mongoid::Document
|
27
|
+
embedded_in :car
|
28
|
+
end
|
29
|
+
|
30
|
+
class Car
|
31
|
+
include Mongoid::Document
|
32
|
+
embeds_many :doors
|
33
|
+
end
|
34
|
+
|
35
|
+
Now you can easily retrieve instances of `Door` class using `finder`:
|
36
|
+
|
37
|
+
finder = MongoidEmbedFinder::Runner.new(Door, :car)
|
38
|
+
# first argument is embedded class
|
39
|
+
# second is name of the association to the parent
|
40
|
+
|
41
|
+
If you have `door_id` at your hands:
|
42
|
+
|
43
|
+
finder.first(id: door_id)
|
44
|
+
# => instance of Door class or nil in case not found
|
45
|
+
|
46
|
+
If you have `door_id` and `car_id` as well, you can narrow scope of cars:
|
47
|
+
|
48
|
+
finder.first(id: door_id, parent: { id: car_id })
|
49
|
+
# => instance of Door class or nil in case not found
|
50
|
+
|
51
|
+
In general you can basically pass any set of child's and parent's attributes.
|
52
|
+
Latter group should be passed under `parent` key. Those attributes are passed down to `Mongoid::Criteria`.
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.comgrowthrepublic/mongoid_embed_finder/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module MongoidEmbedFinder
|
2
|
+
class NestedBuilder
|
3
|
+
def initialize(nested_attrs, relations)
|
4
|
+
@nested_attrs = nested_attrs
|
5
|
+
@relations = relations
|
6
|
+
end
|
7
|
+
|
8
|
+
def build_child
|
9
|
+
return nil if children_attributes.empty?
|
10
|
+
|
11
|
+
@relations.child_class.new(children_attributes.first).tap do |child|
|
12
|
+
child.public_send(@relations.parent.setter, build_parent)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_parent
|
17
|
+
@relations.parent_class.new(parent_attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
def children_attributes
|
21
|
+
@children_attributes ||= @nested_attrs.fetch(@relations.children.key)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parent_attributes
|
25
|
+
@parent_attributes ||= @nested_attrs.except(@relations.children.key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module MongoidEmbedFinder
|
2
|
+
module Projectors
|
3
|
+
class Base < Struct.new(:relation, :criteria)
|
4
|
+
def projection
|
5
|
+
{ relation.key => { operator => criteria.selector }}
|
6
|
+
end
|
7
|
+
|
8
|
+
def operator
|
9
|
+
raise NotImplementedError, "operator needs to be overriden"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require "active_support/inflector"
|
3
|
+
|
4
|
+
module MongoidEmbedFinder
|
5
|
+
class RelationDiscovery < Struct.new(:child_class, :relation_name)
|
6
|
+
def relations
|
7
|
+
@relations ||= OpenStruct.new(
|
8
|
+
child_class: child_class,
|
9
|
+
parent_class: parent_class,
|
10
|
+
children: find_children_relation,
|
11
|
+
parent: find_parent_relation)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def find_parent_relation
|
17
|
+
@parent_relation ||= child_class.relations.fetch(relation_name.to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
def parent_class
|
21
|
+
@parent_class ||= find_parent_relation.class_name.constantize
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_children_relation
|
25
|
+
@children_relation ||= parent_class.relations.values
|
26
|
+
.find { |v| v.class_name == child_class.name }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "mongoid_embed_finder/projectors/base"
|
2
|
+
require "mongoid_embed_finder/projectors/single"
|
3
|
+
require "mongoid_embed_finder/nested_builder"
|
4
|
+
require "mongoid_embed_finder/relation_discovery"
|
5
|
+
|
6
|
+
module MongoidEmbedFinder
|
7
|
+
class Runner
|
8
|
+
def initialize(child_class, parent_relation_name)
|
9
|
+
@relation_discovery = RelationDiscovery.new(
|
10
|
+
child_class, parent_relation_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def first(parent: {}, **attrs)
|
14
|
+
nested_attrs = find_first(attrs, parent: parent)
|
15
|
+
return nil unless nested_attrs
|
16
|
+
build_child_with_parent(nested_attrs)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def find_first(attrs = {}, parent: {})
|
22
|
+
scope_parent(parent)
|
23
|
+
project(attrs, with: Projectors::Single)
|
24
|
+
execute_query.first
|
25
|
+
end
|
26
|
+
|
27
|
+
def scope_parent(attrs = {})
|
28
|
+
@parent_criteria ||= relations.parent_class.criteria
|
29
|
+
@parent_criteria.selector.merge!(attrs)
|
30
|
+
end
|
31
|
+
|
32
|
+
def project(attrs = {}, with: Projectors::Base)
|
33
|
+
child_criteria = relations.child_class.criteria.where(attrs)
|
34
|
+
@projector = with.new(relations.children, child_criteria)
|
35
|
+
scope_parent(@projector.projection)
|
36
|
+
end
|
37
|
+
|
38
|
+
def execute_query
|
39
|
+
relations.parent_class.collection
|
40
|
+
.find(@parent_criteria.selector)
|
41
|
+
.select(@projector.projection)
|
42
|
+
end
|
43
|
+
|
44
|
+
def build_child_with_parent(nested_attrs)
|
45
|
+
builder = NestedBuilder.new(nested_attrs, relations)
|
46
|
+
builder.build_child
|
47
|
+
end
|
48
|
+
|
49
|
+
def relations
|
50
|
+
@relation_discovery.relations
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "mongoid_embed_finder/version"
|
2
|
+
require "mongoid_embed_finder/projector"
|
3
|
+
require "mongoid_embed_finder/single_projector"
|
4
|
+
require "mongoid_embed_finder/relation_discovery"
|
5
|
+
require "mongoid_embed_finder/hierarchy_builder"
|
6
|
+
require "mongoid_embed_finder/runner"
|
7
|
+
|
8
|
+
module MongoidEmbedFinder
|
9
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_embed_finder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_embed_finder"
|
8
|
+
spec.version = MongoidEmbedFinder::VERSION
|
9
|
+
spec.authors = ["Artur Hebda"]
|
10
|
+
spec.email = ["arturhebda@gmail.com"]
|
11
|
+
spec.summary = %[Find mongoid embedded documents easily.]
|
12
|
+
spec.description = %[
|
13
|
+
This simple gem lets you find embedded documents easily. It does not instantiate
|
14
|
+
parent record with a whole bunch of embedded documents. Instead it instantiates
|
15
|
+
found embedded documents first and then sets parent association.
|
16
|
+
]
|
17
|
+
spec.homepage = "https://github.com/growthrepublic/mongoid_embed_finder"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0")
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "mongoid", "~> 4.0.0.beta1"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.3"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0.0.rc1"
|
30
|
+
spec.add_development_dependency "rspec-its", "~> 1.0"
|
31
|
+
spec.add_development_dependency "rspec-mocks", "~> 3.0.0.rc1"
|
32
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/nested_builder"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::NestedBuilder do
|
5
|
+
let(:relations) do
|
6
|
+
double(
|
7
|
+
parent_class: Car,
|
8
|
+
child_class: Door,
|
9
|
+
children: double(key: "doors"),
|
10
|
+
parent: double(key: "car", setter: "car=")
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "parent with children" do
|
15
|
+
let(:nested_attrs) do
|
16
|
+
{
|
17
|
+
"_id" => "parent_id",
|
18
|
+
"name" => "Car Name",
|
19
|
+
"doors" => 2.times.map do |n|
|
20
|
+
{
|
21
|
+
"_id" => "child_id_#{n}",
|
22
|
+
"name" => "Child Name #{n}",
|
23
|
+
}
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#build_child" do
|
29
|
+
subject { described_class.new(nested_attrs, relations).build_child }
|
30
|
+
|
31
|
+
its(:_id) { should eq "child_id_0" }
|
32
|
+
its(:name) { should eq "Child Name 0" }
|
33
|
+
its('car._id') { should eq "parent_id" }
|
34
|
+
its('car.name') { should eq "Car Name" }
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#build_parent" do
|
38
|
+
subject { described_class.new(nested_attrs, relations).build_parent }
|
39
|
+
|
40
|
+
its(:_id) { should eq "parent_id" }
|
41
|
+
its(:name) { should eq "Car Name" }
|
42
|
+
its(:doors) { should be_empty }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "parent without children" do
|
47
|
+
let(:nested_attrs) do
|
48
|
+
{
|
49
|
+
"_id" => "parent_id",
|
50
|
+
"name" => "Car Name",
|
51
|
+
"doors" => []
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#build_child" do
|
56
|
+
subject { described_class.new(nested_attrs, relations).build_child }
|
57
|
+
it { should be_nil }
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#build_parent" do
|
61
|
+
subject { described_class.new(nested_attrs, relations).build_parent }
|
62
|
+
|
63
|
+
its(:_id) { should eq "parent_id" }
|
64
|
+
its(:name) { should eq "Car Name" }
|
65
|
+
its(:doors) { should be_empty }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/projector"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::Projector do
|
5
|
+
describe "#to_projection" do
|
6
|
+
let(:relation) { double(key: "children") }
|
7
|
+
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
8
|
+
|
9
|
+
subject { described_class.new(relation, criteria) }
|
10
|
+
|
11
|
+
it "needs to be implemented in derived classes" do
|
12
|
+
expect { subject.to_projection }.to raise_error(NotImplementedError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/projectors/base"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::Projectors::Base do
|
5
|
+
describe "#projection" do
|
6
|
+
let(:relation) { double(key: "children") }
|
7
|
+
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
8
|
+
|
9
|
+
subject { described_class.new(relation, criteria) }
|
10
|
+
|
11
|
+
it "relies on methods to be implemented" do
|
12
|
+
expect { subject.projection }.to raise_error(NotImplementedError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/projectors/single"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::Projectors::Single do
|
5
|
+
describe "#projection" do
|
6
|
+
let(:relation) { double(key: "children") }
|
7
|
+
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
8
|
+
|
9
|
+
subject { described_class.new(relation, criteria) }
|
10
|
+
|
11
|
+
its(:projection) { should eq(relation.key => { "$elemMatch" => criteria.selector }) }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/relation_discovery"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::RelationDiscovery do
|
5
|
+
describe "#relations" do
|
6
|
+
subject { described_class.new(Child, :parent).relations }
|
7
|
+
|
8
|
+
its(:child_class) { should eq Child }
|
9
|
+
its(:parent_class) { should eq Parent }
|
10
|
+
|
11
|
+
its('children.key') { should eq "children" }
|
12
|
+
its('children.class_name') { should eq "Child" }
|
13
|
+
|
14
|
+
its('parent.setter') { should eq "parent=" }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mongoid_embed_finder/runner"
|
3
|
+
|
4
|
+
describe MongoidEmbedFinder::Runner do
|
5
|
+
subject { described_class.new(Door, :car) }
|
6
|
+
|
7
|
+
describe "#first" do
|
8
|
+
let!(:cars) do
|
9
|
+
2.times.map do |n|
|
10
|
+
Car.create(name: "Parent Name #{n}").tap do |car|
|
11
|
+
2.times.map do |n|
|
12
|
+
car.doors << Door.new(name: "Child Name #{n}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "by child_id and parent_id" do
|
19
|
+
it "returns the child" do
|
20
|
+
door = subject.first(
|
21
|
+
id: cars[1].doors[1].id.to_s,
|
22
|
+
parent: { id: cars[1].id.to_s })
|
23
|
+
|
24
|
+
expect(door).to eq cars[1].doors[1]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "no parent attributes" do
|
29
|
+
it "returns the child" do
|
30
|
+
door = subject.first(id: cars[1].doors[1].id.to_s)
|
31
|
+
expect(door).to eq cars[1].doors[1]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "child does not exist" do
|
36
|
+
let(:wrong_id) { "5369f2d8417274e9d8000000" }
|
37
|
+
|
38
|
+
it "returns nil" do
|
39
|
+
expect(subject.first(id: wrong_id)).to be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(__dir__, "..", "lib"))
|
2
|
+
|
3
|
+
require "mongoid"
|
4
|
+
require "rspec/its"
|
5
|
+
|
6
|
+
# These environment variables can be set if wanting to test against a database
|
7
|
+
# that is not on the local machine.
|
8
|
+
ENV["MONGOID_SPEC_HOST"] ||= "localhost"
|
9
|
+
ENV["MONGOID_SPEC_PORT"] ||= "27017"
|
10
|
+
|
11
|
+
# These are used when creating any connection in the test suite.
|
12
|
+
HOST = ENV["MONGOID_SPEC_HOST"]
|
13
|
+
PORT = ENV["MONGOID_SPEC_PORT"].to_i
|
14
|
+
|
15
|
+
def database_id
|
16
|
+
"mongoid_test"
|
17
|
+
end
|
18
|
+
|
19
|
+
CONFIG = {
|
20
|
+
sessions: {
|
21
|
+
default: {
|
22
|
+
database: database_id,
|
23
|
+
hosts: [ "#{HOST}:#{PORT}" ]
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
# Set the database that the spec suite connects to.
|
29
|
+
Mongoid.configure do |config|
|
30
|
+
config.load_configuration(CONFIG)
|
31
|
+
end
|
32
|
+
|
33
|
+
class Door
|
34
|
+
include Mongoid::Document
|
35
|
+
field :name, type: String
|
36
|
+
embedded_in :car
|
37
|
+
end unless defined?(Door)
|
38
|
+
|
39
|
+
class Car
|
40
|
+
include Mongoid::Document
|
41
|
+
field :name, type: String
|
42
|
+
embeds_many :doors
|
43
|
+
end unless defined?(Car)
|
44
|
+
|
45
|
+
RSpec.configure do |config|
|
46
|
+
config.raise_errors_for_deprecations!
|
47
|
+
|
48
|
+
# Drop all collections and clear the identity map before each spec.
|
49
|
+
config.before(:each) do
|
50
|
+
Mongoid.purge!
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_embed_finder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artur Hebda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0.beta1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0.beta1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0.rc1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0.rc1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-mocks
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.0.0.rc1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.0.0.rc1
|
97
|
+
description: "\n This simple gem lets you find embedded documents easily. It does
|
98
|
+
not instantiate\n parent record with a whole bunch of embedded documents. Instead
|
99
|
+
it instantiates\n found embedded documents first and then sets parent association.\n
|
100
|
+
\ "
|
101
|
+
email:
|
102
|
+
- arturhebda@gmail.com
|
103
|
+
executables: []
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- lib/mongoid_embed_finder.rb
|
114
|
+
- lib/mongoid_embed_finder/nested_builder.rb
|
115
|
+
- lib/mongoid_embed_finder/projectors/base.rb
|
116
|
+
- lib/mongoid_embed_finder/projectors/single.rb
|
117
|
+
- lib/mongoid_embed_finder/relation_discovery.rb
|
118
|
+
- lib/mongoid_embed_finder/runner.rb
|
119
|
+
- lib/mongoid_embed_finder/version.rb
|
120
|
+
- mongoid_embed_finder.gemspec
|
121
|
+
- spec/mongoid_embed_finder/nested_builder_spec.rb
|
122
|
+
- spec/mongoid_embed_finder/projector_spec.rb
|
123
|
+
- spec/mongoid_embed_finder/projectors/base_spec.rb
|
124
|
+
- spec/mongoid_embed_finder/projectors/single_spec.rb
|
125
|
+
- spec/mongoid_embed_finder/relation_discovery_spec.rb
|
126
|
+
- spec/mongoid_embed_finder/runner_spec.rb
|
127
|
+
- spec/spec_helper.rb
|
128
|
+
homepage: https://github.com/growthrepublic/mongoid_embed_finder
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata: {}
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.2.2
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Find mongoid embedded documents easily.
|
152
|
+
test_files:
|
153
|
+
- spec/mongoid_embed_finder/nested_builder_spec.rb
|
154
|
+
- spec/mongoid_embed_finder/projector_spec.rb
|
155
|
+
- spec/mongoid_embed_finder/projectors/base_spec.rb
|
156
|
+
- spec/mongoid_embed_finder/projectors/single_spec.rb
|
157
|
+
- spec/mongoid_embed_finder/relation_discovery_spec.rb
|
158
|
+
- spec/mongoid_embed_finder/runner_spec.rb
|
159
|
+
- spec/spec_helper.rb
|
160
|
+
has_rdoc:
|