populate-me 0.2.0 → 0.3.0

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: 10a0d904a55bc88de4e01af6c15816cc3c943a78
4
- data.tar.gz: cc36c361bd647861681d153f5bf2e9d9046a7e73
3
+ metadata.gz: b5d6a2a8ba2877bb7de857c1befde67845b87fd6
4
+ data.tar.gz: 38ff9b163189c90109d325815ffd2cde81ca55ac
5
5
  SHA512:
6
- metadata.gz: 34f55a27c047c8e0204eaf10a9a8c4d62b3ab06e9672812f4522f6b474b48261bf6521a538383770a3be5d1106651d48c5d4502c74d3dcd282bb87bcba7c075b
7
- data.tar.gz: 901ce3866a5c98053a4b92f5f53fac015d7b4424f0c32c89b0a4418104b904083c1901f0a5e44606aa8c7677ac24e45aa20921a470c6548774c8be0ca67d1b61
6
+ metadata.gz: c2d852eeff231cbb2d85865ba5f097d63fd8ed9d65279820189ea234bb2721bd610b53ec3b64b11de006d826aeb87fde5f0e2572c9d38b57f768ec2190372485
7
+ data.tar.gz: d2af5a7f0e800f73c126fff0329aea00ba5c6b47727c36de2de84b1c476141cd38062de253eb37a4294fb3b2a9e7116989cfe17ad3c241704422b8159f9f1e49
@@ -90,6 +90,10 @@ module PopulateMe
90
90
  docs
91
91
  end
92
92
 
93
+ def admin_find_first o={}
94
+ self.admin_find(o)[0]
95
+ end
96
+
93
97
  def admin_distinct field, o={}
94
98
  self.admin_find(o).map{|d| d.__send__ field}.compact.uniq
95
99
  end
@@ -89,6 +89,16 @@ module PopulateMe
89
89
  o[:foreign_key] = o[:foreign_key].to_sym
90
90
  WebUtils.ensure_key! o, :dependent, true
91
91
  self.relationships[name] = o
92
+
93
+ define_method(name) do
94
+ var = "@cached_#{name}"
95
+ instance_variable_set(var, instance_variable_get(var)||WebUtils.resolve_class_name(o[:class_name]).admin_find(query: {o[:foreign_key]=>self.id}))
96
+ end
97
+
98
+ define_method("#{name}_first".to_sym) do
99
+ var = "@cached_#{name}_first"
100
+ instance_variable_set(var, instance_variable_get(var)||WebUtils.resolve_class_name(o[:class_name]).admin_find_first(query: {o[:foreign_key]=>self.id}))
101
+ end
92
102
  end
93
103
 
94
104
  end
@@ -76,6 +76,10 @@ module PopulateMe
76
76
  self.cast{ collection.find(query, o) }
77
77
  end
78
78
 
79
+ def admin_find_first o={}
80
+ self.admin_find(o.merge({limit: 1}))[0]
81
+ end
82
+
79
83
  def admin_distinct field, o={}
80
84
  query = o.delete(:query) || {}
81
85
  self.collection.distinct field, query, o
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
4
4
 
@@ -65,6 +65,46 @@ describe PopulateMe::Document, 'AdminAdapter' do
65
65
  end
66
66
  end
67
67
 
68
+ describe '::admin_find ::admin_find_first' do
69
+
70
+ class FindablePerson < PopulateMe::Document
71
+ field :first_name
72
+ field :last_name
73
+ end
74
+
75
+ before do
76
+ FindablePerson.documents = []
77
+ FindablePerson.new(first_name: 'Bobby', last_name: 'Peru').save
78
+ FindablePerson.new(first_name: 'John', last_name: 'Doe').save
79
+ FindablePerson.new(first_name: 'John', last_name: 'Turturo').save
80
+ end
81
+
82
+ it 'Finds everything' do
83
+ people = FindablePerson.admin_find
84
+ assert_equal Array, people.class
85
+ assert_equal 3, people.size
86
+ assert_equal FindablePerson.documents, people
87
+ end
88
+
89
+ it 'Finds with query' do
90
+ people = FindablePerson.admin_find query: {first_name: 'John'}
91
+ assert_equal Array, people.class
92
+ assert_equal 2, people.size
93
+ assert_equal 'Doe', people[0].last_name
94
+ end
95
+
96
+ it 'Finds first' do
97
+ person = FindablePerson.admin_find_first
98
+ assert_equal 'Bobby', person.first_name
99
+ end
100
+
101
+ it 'Finds first with query' do
102
+ person = FindablePerson.admin_find_first query: {first_name: 'John'}
103
+ assert_equal 'Doe', person.last_name
104
+ end
105
+
106
+ end
107
+
68
108
  describe '::admin_distinct' do
69
109
 
70
110
  class Distinction < PopulateMe::Document
@@ -13,6 +13,16 @@ describe PopulateMe::Document, 'Schema' do
13
13
  relationship :friends, label: 'Budies', class_name: 'Budy', foreign_key: :budy_id, dependent: false
14
14
  end
15
15
 
16
+ class Relative::Sibling < PopulateMe::Document
17
+ field :size
18
+ field :relative_id, type: :parent
19
+ end
20
+
21
+ before do
22
+ Relative.documents = []
23
+ Relative::Sibling.documents = []
24
+ end
25
+
16
26
  it "Defaults class name" do
17
27
  assert_equal "Relative::Sibling", Relative.relationships[:siblings][:class_name]
18
28
  assert_equal "Relative::HomeRemedy", Relative.relationships[:home_remedies][:class_name]
@@ -39,6 +49,32 @@ describe PopulateMe::Document, 'Schema' do
39
49
  refute Relative.relationships[:friends][:dependent]
40
50
  end
41
51
 
52
+ it "Creates a getter for cached items" do
53
+ relative = Relative.new(id: 10)
54
+ relative.save
55
+ Relative::Sibling.new(relative_id: 10, size: 'S').save
56
+ Relative::Sibling.new(relative_id: 10, size: 'M').save
57
+ Relative::Sibling.new(relative_id: 10, size: 'L').save
58
+ assert relative.respond_to? :siblings
59
+ assert_nil relative.instance_variable_get('@cached_siblings')
60
+ siblings = relative.siblings
61
+ assert_equal 3, siblings.size
62
+ assert_equal siblings, relative.instance_variable_get('@cached_siblings')
63
+ end
64
+
65
+ it "Creates a getter for cached first item" do
66
+ relative = Relative.new(id: 10)
67
+ relative.save
68
+ Relative::Sibling.new(relative_id: 10, size: 'S').save
69
+ Relative::Sibling.new(relative_id: 10, size: 'M').save
70
+ Relative::Sibling.new(relative_id: 10, size: 'L').save
71
+ assert relative.respond_to? :siblings_first
72
+ assert_nil relative.instance_variable_get('@cached_siblings_first')
73
+ sibling = relative.siblings_first
74
+ assert_equal 'S', sibling.size
75
+ assert_equal sibling, relative.instance_variable_get('@cached_siblings_first')
76
+ end
77
+
42
78
  end
43
79
 
44
80
  end
data/test/test_mongo.rb CHANGED
@@ -125,10 +125,29 @@ describe 'PopulateMe::Mongo' do
125
125
  end
126
126
 
127
127
  it 'Should admin_find correctly' do
128
- LowFish.collection.insert_one(_id: 42, name: "H2G2")
129
- assert LowFish.admin_find.is_a?(Array)
130
- assert_equal LowFish.collection.find(name: 'H2G2').count, LowFish.admin_find(query: {name: 'H2G2'}).size
131
- assert_equal 42, LowFish.admin_find(query: {name: 'H2G2'})[0].id
128
+ LowFish.collection.insert_one(_id: 10, name: "Arya")
129
+ LowFish.collection.insert_one(_id: 20, name: "Bran")
130
+ LowFish.collection.insert_one(_id: 30, name: "Arya")
131
+ LowFish.collection.insert_one(_id: 40, name: "Bran")
132
+ items = LowFish.admin_find
133
+ assert items.is_a?(Array)
134
+ assert 4, items.count
135
+ assert_equal 10, items[0].id
136
+ items = LowFish.admin_find query: {name: 'Bran'}
137
+ assert items.is_a?(Array)
138
+ assert 2, items.count
139
+ assert_equal 20, items[0].id
140
+ end
141
+
142
+ it 'Should admin_find_first correctly' do
143
+ LowFish.collection.insert_one(_id: 10, name: "Arya")
144
+ LowFish.collection.insert_one(_id: 20, name: "Bran")
145
+ LowFish.collection.insert_one(_id: 30, name: "Arya")
146
+ LowFish.collection.insert_one(_id: 40, name: "Bran")
147
+ item = LowFish.admin_find_first
148
+ assert_equal 10, item.id
149
+ item = LowFish.admin_find_first query: {name: "Bran"}
150
+ assert_equal 20, item.id
132
151
  end
133
152
 
134
153
  it 'Should admin_find while turning fields option into a projection option' do
@@ -10,6 +10,14 @@ PopulateMe::S3Attachment.set :bucket, s3_bucket
10
10
  describe 'PopulateMe::S3Attachment' do
11
11
  # parallelize_me!
12
12
 
13
+ def check_if_public_read(object)
14
+ grant = object.acl.grants.find do |g|
15
+ g.grantee.uri=="http://acs.amazonaws.com/groups/global/AllUsers"
16
+ end
17
+ raise "grant not found" if grant.nil?
18
+ ['READ','FULL_CONTROL'].include? grant.permission
19
+ end
20
+
13
21
  class S3Book < PopulateMe::Document
14
22
  field :cover, type: :attachment, variations: [
15
23
  PopulateMe::Variation.new_image_magick_job(:thumb, :gif, "-resize '300x'")
@@ -83,7 +91,7 @@ describe 'PopulateMe::S3Attachment' do
83
91
 
84
92
  vars3file = s3_bucket.object('public/s-3-book/story.upcase.txt')
85
93
  assert_equal 'text/plain', vars3file.content_type
86
- # assert_equal 'public-read', vars3file.acl
94
+ assert check_if_public_read(vars3file)
87
95
  assert_equal 's-3-book', vars3file.metadata['parent_collection']
88
96
  assert_equal 'HELLO', vars3file.get.body.read
89
97
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: populate-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-30 00:00:00.000000000 Z
11
+ date: 2018-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: web-utils