mongoid_embed_finder 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70d886a2e30dc89447b1e95b6b2dc1384ef80e9a
4
- data.tar.gz: 69221aca597bfe5f984551a0dec804b72895ddd4
3
+ metadata.gz: a4b0eab4a0e78f65604f3782019c1f8a96c2366d
4
+ data.tar.gz: bda098c893739bf97833edd34afff02f1f98f804
5
5
  SHA512:
6
- metadata.gz: 12b5c6df2b73f7781571f55cc55c07fd80293856732af0e771b15519387c17bb821b35e4c05e7c599f1f9b465db88d3d9ebfe2dcb7fbb8921613b629672eae23
7
- data.tar.gz: b687bdd8a2631c16c134f8f0ed14d533d70a25c87e5c0ce83e6fb06c02c61fc876d7c639714da209a30b4f7d5b54749cbebc7e2c01a1df4922753872bd097ffd
6
+ metadata.gz: a30f6b29f97ee61c425509401d150be9a66925b985c8bc1a4eb8a2646b50810c1ffa3695fe0b0736d5cb3d15c16f0a81c4af9c8adfdd050d7e905873cb52a42a
7
+ data.tar.gz: 4ad9f506675098f170a4eb290075518635c40be55af464c98c702168ee5b512bae392d91a5eba639285e7bb29e30664524424ae84989c0d644528606a27c7d30
data/README.md CHANGED
@@ -28,10 +28,11 @@ Let's say that you have defined two classes as follows:
28
28
  include Mongoid::Document
29
29
  embedded_in :car
30
30
  end
31
-
31
+
32
32
  class Car
33
33
  include Mongoid::Document
34
34
  embeds_many :doors
35
+ field :name, type: String
35
36
  end
36
37
 
37
38
  Now you can easily retrieve instances of `Door` class using `finder`:
@@ -53,6 +54,16 @@ If you have `door_id` and `car_id` as well, you can narrow scope of cars:
53
54
  In general you can basically pass any set of child's and parent's attributes.
54
55
  Latter group should be passed under `parent` key. Those attributes are passed down to `Mongoid::Criteria`.
55
56
 
57
+ ### Warning!
58
+
59
+ If your embedded object has other referenced entities they will not be loaded.
60
+
61
+ By default no parent's attributes are set except its primary key. It is deliberate to limit
62
+ amount of returned data.
63
+ To retrieve parent's attributes list them under `parent.include_fields` as follows:
64
+
65
+ finder.first(id: door_id, parent: { id: car_id, include_fields: [:name] })
66
+
56
67
  ## Contributing
57
68
 
58
69
  1. Fork it ( https://github.com/growthrepublic/mongoid_embed_finder/fork )
@@ -5,14 +5,23 @@ module MongoidEmbedFinder
5
5
  { relation.key => { operator => query.child_criteria.selector }}
6
6
  end
7
7
 
8
- def project
8
+ def project(fields = [])
9
+ projection_with_fields = projection.merge(include_fields(fields))
9
10
  query.scope_parent(projection)
10
- query.execute.select(projection)
11
+ query.execute.select(projection_with_fields)
11
12
  end
12
13
 
13
14
  def operator
14
15
  raise NotImplementedError, "operator needs to be overriden"
15
16
  end
17
+
18
+ private
19
+
20
+ def include_fields(fields)
21
+ fields.inject({}) do
22
+ |acc, name| acc.merge(name => 1)
23
+ end
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -20,7 +20,8 @@ module MongoidEmbedFinder
20
20
 
21
21
  def find_first(attrs = {}, parent: {})
22
22
  query = build_nested_query(attrs, parent: parent)
23
- project_query(query, Projectors::Single).first
23
+ project_query(query, Projectors::Single,
24
+ parent_fields: parent.fetch(:include_fields, [])).first
24
25
  end
25
26
 
26
27
  def build_nested_query(attrs = {}, parent: {})
@@ -28,13 +29,13 @@ module MongoidEmbedFinder
28
29
  child_criteria = relations.child_class.criteria
29
30
 
30
31
  NestedQuery.new(parent_criteria, child_criteria).tap do |query|
31
- query.scope_parent(parent)
32
+ query.scope_parent(parent.except(:include_fields))
32
33
  query.scope_child(attrs)
33
34
  end
34
35
  end
35
36
 
36
- def project_query(query, projector_class)
37
- projector_class.new(query, relations.children).project
37
+ def project_query(query, projector_class, parent_fields: [])
38
+ projector_class.new(query, relations.children).project(parent_fields)
38
39
  end
39
40
 
40
41
  def build_child_with_parent(nested_attrs)
@@ -1,3 +1,3 @@
1
1
  module MongoidEmbedFinder
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -19,19 +19,38 @@ describe MongoidEmbedFinder::Projectors::Single do
19
19
  end
20
20
 
21
21
  describe "#project" do
22
- it "extend parent's criteria" do
23
- subject.project
24
- expect(query).to have_received(:scope_parent).with(subject.projection)
25
- end
22
+ context "without fields to include" do
23
+ it "extend parent's criteria" do
24
+ subject.project
25
+ expect(query).to have_received(:scope_parent).with(subject.projection)
26
+ end
27
+
28
+ it "projects query" do
29
+ subject.project
30
+ expect(query).to have_received(:execute)
31
+ expect(query.execute).to have_received(:select).with(subject.projection)
32
+ end
26
33
 
27
- it "projects query" do
28
- subject.project
29
- expect(query).to have_received(:execute)
30
- expect(query.execute).to have_received(:select).with(subject.projection)
34
+ it "returns projection result" do
35
+ expect(subject.project).to eq projection_result
36
+ end
31
37
  end
32
38
 
33
- it "returns projection result" do
34
- expect(subject.project).to eq projection_result
39
+ context "with fields to include" do
40
+ it "extend parent's criteria" do
41
+ subject.project
42
+ expect(query).to have_received(:scope_parent).with(subject.projection)
43
+ end
44
+
45
+ it "projects query with parent's fields" do
46
+ subject.project([:name])
47
+ expect(query).to have_received(:execute)
48
+ expect(query.execute).to have_received(:select).with(subject.projection.merge(name: 1))
49
+ end
50
+
51
+ it "returns projection result" do
52
+ expect(subject.project).to eq projection_result
53
+ end
35
54
  end
36
55
  end
37
56
  end
@@ -17,18 +17,58 @@ describe MongoidEmbedFinder::Runner do
17
17
 
18
18
  context "by child_id and parent_id" do
19
19
  it "returns the child" do
20
- door = subject.first(
20
+ result = subject.first(
21
21
  id: cars[1].doors[1].id.to_s,
22
22
  parent: { id: cars[1].id.to_s })
23
23
 
24
- expect(door).to eq cars[1].doors[1]
24
+ expect(result).to eq cars[1].doors[1]
25
+ end
26
+
27
+ context "not listed parent's attributes" do
28
+ it "sets only id" do
29
+ result = subject.first(
30
+ id: cars[1].doors[1].id.to_s,
31
+ parent: { id: cars[1].id.to_s })
32
+
33
+ expect(result.car.id).to eq cars[1].id
34
+ expect(result.car.name).to be_nil
35
+ end
36
+ end
37
+
38
+ context "listed parent's attributes" do
39
+ it "sets listed attributes" do
40
+ result = subject.first(
41
+ id: cars[1].doors[1].id.to_s,
42
+ parent: { id: cars[1].id.to_s, include_fields: [:name] })
43
+
44
+ expect(result.car.id).to eq cars[1].id
45
+ expect(result.car.name).to eq cars[1].name
46
+ end
25
47
  end
26
48
  end
27
49
 
28
50
  context "no parent attributes" do
29
51
  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]
52
+ result = subject.first(id: cars[1].doors[1].id.to_s)
53
+ expect(result).to eq cars[1].doors[1]
54
+ end
55
+
56
+ context "not listed parent's attributes" do
57
+ it "sets only id" do
58
+ result = subject.first(id: cars[1].doors[1].id.to_s)
59
+ expect(result.car.id).to eq cars[1].id
60
+ expect(result.car.name).to be_nil
61
+ end
62
+ end
63
+
64
+ context "listed parent's attributes" do
65
+ it "sets listed attributes" do
66
+ result = subject.first(id: cars[1].doors[1].id.to_s,
67
+ parent: { include_fields: [:name] })
68
+
69
+ expect(result.car.id).to eq cars[1].id
70
+ expect(result.car.name).to eq cars[1].name
71
+ end
32
72
  end
33
73
  end
34
74
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_embed_finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Hebda