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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 526f9c845b23d850f5edf0d19c497710a74e4c06
4
- data.tar.gz: 163e32750f2d8b3aa02b2d274a3f25a0bcdebab9
3
+ metadata.gz: 4e6c7bf4a8f1d514b164c6089193db8fce6bd1f2
4
+ data.tar.gz: 4881c632f52cd21d084884b6904f4172aee316b0
5
5
  SHA512:
6
- metadata.gz: be52a79241cb22b829f23d9157ba99e436f56253d4094f9c8a50ac4c8506c3eb4e9f4cf305195f980c465209f10be92049c80dc6dc7162b2d5840fb5f5276380
7
- data.tar.gz: da84fd60662a5fb30a5e834b1f29f3c384d6a9217af1554a033c652f13bda65fc3b080c1ec3ab70fac045cddfd802a1104367baacd22e736c01e9ac4476e9b08
6
+ metadata.gz: 964bc86b0186495bb6cfe0d61461a912dd4b5403ce87cd31855573198bcec269dba26705d6c00df2fedd1e650a6e5149729587b6699ebb061c7401d5ffb104f7
7
+ data.tar.gz: dc7696a1e6fda80ab3965ac0d67d6205abe9939159a2c79bc0152b9f7152527d049b62d19428ff803ff7361f1e18a0c6299e8b9d363eed5822c6e8cfde730cfc
@@ -90,6 +90,10 @@ module PopulateMe
90
90
  docs
91
91
  end
92
92
 
93
+ def admin_distinct field, o={}
94
+ self.admin_find(o).map{|d| d.__send__ field}.compact.uniq
95
+ end
96
+
93
97
  def sort_field_for o={}
94
98
  filter = o[:params][:filter]
95
99
  return nil if !filter.nil?&&filter.size>1
@@ -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
@@ -69,6 +69,11 @@ module PopulateMe
69
69
  self.cast{ collection.find(query, o) }
70
70
  end
71
71
 
72
+ def admin_distinct field, o={}
73
+ query = o.delete(:query) || {}
74
+ self.collection.distinct field, query, o
75
+ end
76
+
72
77
  end
73
78
 
74
79
  attr_accessor :_id
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
4
4
 
@@ -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.5
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-04 00:00:00.000000000 Z
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.6.8
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.