populate-me 0.1.5 → 0.1.6
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 +4 -4
- data/lib/populate_me/document_mixins/admin_adapter.rb +4 -0
- data/lib/populate_me/document_mixins/callbacks.rb +19 -0
- data/lib/populate_me/mongo.rb +5 -0
- data/lib/populate_me/version.rb +1 -1
- data/test/test_document_admin_adapter.rb +40 -0
- data/test/test_document_callbacks.rb +44 -0
- data/test/test_mongo.rb +39 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e6c7bf4a8f1d514b164c6089193db8fce6bd1f2
|
4
|
+
data.tar.gz: 4881c632f52cd21d084884b6904f4172aee316b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 964bc86b0186495bb6cfe0d61461a912dd4b5403ce87cd31855573198bcec269dba26705d6c00df2fedd1e650a6e5149729587b6699ebb061c7401d5ffb104f7
|
7
|
+
data.tar.gz: dc7696a1e6fda80ab3965ac0d67d6205abe9939159a2c79bc0152b9f7152527d049b62d19428ff803ff7361f1e18a0c6299e8b9d363eed5822c6e8cfde730cfc
|
@@ -31,6 +31,24 @@ module PopulateMe
|
|
31
31
|
|
32
32
|
def ensure_not_new; self._is_new = false; end
|
33
33
|
|
34
|
+
def ensure_position
|
35
|
+
self.class.fields.each do |k,v|
|
36
|
+
if v[:type]==:position
|
37
|
+
return unless self.__send__(k).nil?
|
38
|
+
values = if v[:scope].nil?
|
39
|
+
self.class.admin_distinct k
|
40
|
+
else
|
41
|
+
self.class.admin_distinct(k, query: {
|
42
|
+
v[:scope] => self.__send__(v[:scope])
|
43
|
+
})
|
44
|
+
end
|
45
|
+
val = values.empty? ? 0 : (values.max + 1)
|
46
|
+
self.set k => val
|
47
|
+
end
|
48
|
+
end
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
34
52
|
def ensure_delete_related
|
35
53
|
self.class.relationships.each do |k,v|
|
36
54
|
if v[:dependent]
|
@@ -61,6 +79,7 @@ module PopulateMe
|
|
61
79
|
after cb, :recurse_callback
|
62
80
|
end
|
63
81
|
before :create, :ensure_id
|
82
|
+
before :create, :ensure_position
|
64
83
|
after :create, :ensure_not_new
|
65
84
|
after :save, :snapshot
|
66
85
|
before :delete, :ensure_delete_related
|
data/lib/populate_me/mongo.rb
CHANGED
data/lib/populate_me/version.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
require 'populate_me/document'
|
3
|
+
require 'populate_me/attachment'
|
3
4
|
|
4
5
|
describe PopulateMe::Document, 'AdminAdapter' do
|
5
6
|
|
@@ -64,4 +65,43 @@ describe PopulateMe::Document, 'AdminAdapter' do
|
|
64
65
|
end
|
65
66
|
end
|
66
67
|
|
68
|
+
describe '::admin_distinct' do
|
69
|
+
|
70
|
+
class Distinction < PopulateMe::Document
|
71
|
+
attr_accessor :title, :age
|
72
|
+
end
|
73
|
+
|
74
|
+
before do
|
75
|
+
Distinction.documents = []
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'Can list all distinct values' do
|
79
|
+
Distinction.new(title: 'Lord').save
|
80
|
+
Distinction.new(title: 'Lord').save
|
81
|
+
Distinction.new.save
|
82
|
+
Distinction.new(title: 'Chevalier').save
|
83
|
+
Distinction.new(title: 'Baron').save
|
84
|
+
Distinction.new(title: 'Baron').save
|
85
|
+
result = Distinction.admin_distinct :title
|
86
|
+
assert_instance_of Array, result
|
87
|
+
assert_equal 3, result.size
|
88
|
+
assert_includes result, 'Lord'
|
89
|
+
assert_includes result, 'Chevalier'
|
90
|
+
assert_includes result, 'Baron'
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'Can list all distinct values for a specific selector' do
|
94
|
+
Distinction.new(title: 'Chevalier', age: 33).save
|
95
|
+
Distinction.new(title: 'Chevalier', age: 34).save
|
96
|
+
Distinction.new(title: 'Baron', age: 35).save
|
97
|
+
Distinction.new(title: 'Baron', age: 36).save
|
98
|
+
result = Distinction.admin_distinct :age, query: {title: 'Baron'}
|
99
|
+
assert_instance_of Array, result
|
100
|
+
assert_equal 2, result.size
|
101
|
+
assert_includes result, 35
|
102
|
+
assert_includes result, 36
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
67
107
|
end
|
@@ -103,5 +103,49 @@ describe PopulateMe::Document, 'Callbacks' do
|
|
103
103
|
assert_equal 'good before_argument after_argument', subject.taste
|
104
104
|
end
|
105
105
|
|
106
|
+
describe '#ensure_position' do
|
107
|
+
|
108
|
+
class RaceCar < PopulateMe::Document
|
109
|
+
field :name
|
110
|
+
field :team
|
111
|
+
field :position_by_team, type: :position, scope: :team
|
112
|
+
position_field
|
113
|
+
end
|
114
|
+
|
115
|
+
before do
|
116
|
+
RaceCar.documents = []
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'Does not change position if already set' do
|
120
|
+
RaceCar.new(name: 'racer3', position: 3, team: 'A', position_by_team: 14).save
|
121
|
+
car = RaceCar.new(name: 'racer1', position: 12, team: 'A', position_by_team: 41)
|
122
|
+
car.ensure_position
|
123
|
+
assert_equal 12, car.position
|
124
|
+
assert_equal 41, car.position_by_team
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'Sets position fields to last position' do
|
128
|
+
RaceCar.new(name: 'racer1', position: 1, team: 'A', position_by_team: 41).save
|
129
|
+
RaceCar.new(name: 'racer2', position: 2, team: 'B', position_by_team: 12).save
|
130
|
+
RaceCar.new(name: 'racer3', position: 3, team: 'B', position_by_team: 14).save
|
131
|
+
car = RaceCar.new(team: 'B').ensure_position
|
132
|
+
assert_equal 4, car.position
|
133
|
+
assert_equal 15, car.position_by_team
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'Returns 0 when first document in scope' do
|
137
|
+
assert_equal 0, RaceCar.new.ensure_position.position
|
138
|
+
RaceCar.new(name: 'racer1', position: 1, team: 'A', position_by_team: 41).save
|
139
|
+
assert_equal 0, RaceCar.new(team: 'B').ensure_position.position_by_team
|
140
|
+
end
|
141
|
+
|
142
|
+
it "Ensures position before creating new document" do
|
143
|
+
car = RaceCar.new
|
144
|
+
car.save
|
145
|
+
assert_equal 0, car.position
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
106
150
|
end
|
107
151
|
|
data/test/test_mongo.rb
CHANGED
@@ -211,6 +211,45 @@ describe 'PopulateMe::Mongo' do
|
|
211
211
|
|
212
212
|
end
|
213
213
|
|
214
|
+
describe '::admin_distinct' do
|
215
|
+
|
216
|
+
class MongoDistinction < PopulateMe::Mongo
|
217
|
+
attr_accessor :title, :age
|
218
|
+
end
|
219
|
+
|
220
|
+
before do
|
221
|
+
MongoDistinction.collection.drop
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'Can list all distinct values' do
|
225
|
+
MongoDistinction.new(title: 'Lord').save
|
226
|
+
MongoDistinction.new(title: 'Lord').save
|
227
|
+
MongoDistinction.new.save
|
228
|
+
MongoDistinction.new(title: 'Chevalier').save
|
229
|
+
MongoDistinction.new(title: 'Baron').save
|
230
|
+
MongoDistinction.new(title: 'Baron').save
|
231
|
+
result = MongoDistinction.admin_distinct :title
|
232
|
+
assert_instance_of Array, result
|
233
|
+
assert_equal 3, result.size
|
234
|
+
assert_includes result, 'Lord'
|
235
|
+
assert_includes result, 'Chevalier'
|
236
|
+
assert_includes result, 'Baron'
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'Can list all distinct values for a specific selector' do
|
240
|
+
MongoDistinction.new(title: 'Chevalier', age: 33).save
|
241
|
+
MongoDistinction.new(title: 'Chevalier', age: 34).save
|
242
|
+
MongoDistinction.new(title: 'Baron', age: 35).save
|
243
|
+
MongoDistinction.new(title: 'Baron', age: 36).save
|
244
|
+
result = MongoDistinction.admin_distinct :age, query: {title: 'Baron'}
|
245
|
+
assert_instance_of Array, result
|
246
|
+
assert_equal 2, result.size
|
247
|
+
assert_includes result, 35
|
248
|
+
assert_includes result, 36
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
214
253
|
end
|
215
254
|
|
216
255
|
DB.drop
|
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.1.
|
4
|
+
version: 0.1.6
|
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-07-
|
11
|
+
date: 2017-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: web-utils
|
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
236
236
|
version: '0'
|
237
237
|
requirements: []
|
238
238
|
rubyforge_project:
|
239
|
-
rubygems_version: 2.
|
239
|
+
rubygems_version: 2.4.5.1
|
240
240
|
signing_key:
|
241
241
|
specification_version: 4
|
242
242
|
summary: PopulateMe is an admin system for web applications.
|