mongoid_orderable 1.1.0 → 1.2.0

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.
data/.travis.yml CHANGED
@@ -8,4 +8,11 @@ rvm:
8
8
 
9
9
  env:
10
10
  - MONGOID_VERSION=2
11
- - MONGOID_VERSION='~> 3.0.0.rc'
11
+ - MONGOID_VERSION='~> 3.0.0'
12
+
13
+ matrix:
14
+ exclude:
15
+ - rvm: 1.9.2
16
+ env: MONGOID_VERSION='~> 3.0.0'
17
+
18
+ services: mongodb
data/README.md CHANGED
@@ -30,6 +30,7 @@ class Item
30
30
  # orderable :scope => :group, :column => :pos
31
31
  # orderable :scope => lambda { |document| where(:group_id => document.group_id) }
32
32
  # orderable :index => false # this one if you want specify indexes manually
33
+ # orderable :base => 0 # count position from zero as the top-most value (1 is the default value)
33
34
  end
34
35
  ```
35
36
 
@@ -6,7 +6,8 @@ module Mongoid::Orderable
6
6
  configuration = {
7
7
  :column => :position,
8
8
  :index => true,
9
- :scope => nil
9
+ :scope => nil,
10
+ :base => 1
10
11
  }
11
12
 
12
13
  configuration.merge! options if options.is_a?(Hash)
@@ -35,6 +36,10 @@ module Mongoid::Orderable
35
36
  configuration[:column]
36
37
  end
37
38
 
39
+ define_method :orderable_base do
40
+ configuration[:base]
41
+ end
42
+
38
43
  before_save :add_to_list
39
44
  after_destroy :remove_from_list
40
45
  end
@@ -77,7 +82,7 @@ module Mongoid::Orderable
77
82
  end
78
83
 
79
84
  def first?
80
- in_list? && orderable_position == 1
85
+ in_list? && orderable_position == orderable_base
81
86
  end
82
87
 
83
88
  def last?
@@ -142,20 +147,22 @@ private
142
147
  target_position = :bottom unless target_position
143
148
 
144
149
  target_position = case target_position.to_sym
145
- when :top then 1
150
+ when :top then orderable_base
146
151
  when :bottom then bottom_orderable_position
147
152
  when :higher then orderable_position.pred
148
153
  when :lower then orderable_position.next
149
154
  end unless target_position.is_a? Numeric
150
155
 
151
- target_position = 1 if target_position < 1
156
+ target_position = orderable_base if target_position < orderable_base
152
157
  target_position = bottom_orderable_position if target_position > bottom_orderable_position
153
158
  target_position
154
159
  end
155
160
 
156
161
  def bottom_orderable_position
157
162
  @bottom_orderable_position = begin
158
- max = orderable_scoped.distinct(orderable_column).map(&:to_i).max.to_i
163
+ positions_list = orderable_scoped.distinct(orderable_column)
164
+ return orderable_base if positions_list.empty?
165
+ max = positions_list.map(&:to_i).max.to_i
159
166
  in_list? ? max : max.next
160
167
  end
161
168
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidOrderable
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -52,6 +52,13 @@ describe Mongoid::Orderable do
52
52
  orderable :index => false
53
53
  end
54
54
 
55
+ class ZeroBasedOrderable
56
+ include Mongoid::Document
57
+ include Mongoid::Orderable
58
+
59
+ orderable :base => 0
60
+ end
61
+
55
62
  describe SimpleOrderable do
56
63
  before :each do
57
64
  SimpleOrderable.delete_all
@@ -77,6 +84,10 @@ describe Mongoid::Orderable do
77
84
  end
78
85
  end
79
86
 
87
+ it 'should have a orderable base of 1' do
88
+ SimpleOrderable.create!.orderable_base.should == 1
89
+ end
90
+
80
91
  it 'should set proper position while creation' do
81
92
  positions.should == [1, 2, 3, 4, 5]
82
93
  end
@@ -105,16 +116,19 @@ describe Mongoid::Orderable do
105
116
  it 'top' do
106
117
  newbie = SimpleOrderable.create! :move_to => :top
107
118
  positions.should == [1, 2, 3, 4, 5, 6]
119
+ newbie.position.should == 1
108
120
  end
109
121
 
110
122
  it 'bottom' do
111
123
  newbie = SimpleOrderable.create! :move_to => :bottom
112
124
  positions.should == [1, 2, 3, 4, 5, 6]
125
+ newbie.position.should == 6
113
126
  end
114
127
 
115
128
  it 'middle' do
116
129
  newbie = SimpleOrderable.create! :move_to => 4
117
130
  positions.should == [1, 2, 3, 4, 5, 6]
131
+ newbie.position.should == 4
118
132
  end
119
133
 
120
134
  end
@@ -217,16 +231,19 @@ describe Mongoid::Orderable do
217
231
  it 'top' do
218
232
  newbie = ScopedOrderable.create! :move_to => :top, :group_id => 1
219
233
  positions.should == [1, 2, 3, 1, 2, 3]
234
+ newbie.position.should == 1
220
235
  end
221
236
 
222
237
  it 'bottom' do
223
238
  newbie = ScopedOrderable.create! :move_to => :bottom, :group_id => 2
224
239
  positions.should == [1, 2, 1, 2, 3, 4]
240
+ newbie.position.should == 4
225
241
  end
226
242
 
227
243
  it 'middle' do
228
244
  newbie = ScopedOrderable.create! :move_to => 2, :group_id => 2
229
245
  positions.should == [1, 2, 1, 2, 3, 4]
246
+ newbie.position.should == 2
230
247
  end
231
248
 
232
249
  end
@@ -290,4 +307,121 @@ describe Mongoid::Orderable do
290
307
  end
291
308
  end
292
309
 
310
+
311
+ describe ZeroBasedOrderable do
312
+ before :each do
313
+ ZeroBasedOrderable.delete_all
314
+ 5.times do
315
+ ZeroBasedOrderable.create!
316
+ end
317
+ end
318
+
319
+ def positions
320
+ ZeroBasedOrderable.all.map(&:position).sort
321
+ end
322
+
323
+ it 'should have a orderable base of 0' do
324
+ ZeroBasedOrderable.create!.orderable_base.should == 0
325
+ end
326
+
327
+ it 'should set proper position while creation' do
328
+ positions.should == [0, 1, 2, 3, 4]
329
+ end
330
+
331
+ describe 'removement' do
332
+
333
+ it 'top' do
334
+ ZeroBasedOrderable.where(:position => 0).destroy
335
+ positions.should == [0, 1, 2, 3]
336
+ end
337
+
338
+ it 'bottom' do
339
+ ZeroBasedOrderable.where(:position => 4).destroy
340
+ positions.should == [0, 1, 2, 3]
341
+ end
342
+
343
+ it 'middle' do
344
+ ZeroBasedOrderable.where(:position => 2).destroy
345
+ positions.should == [0, 1, 2, 3]
346
+ end
347
+
348
+ end
349
+
350
+ describe 'inserting' do
351
+
352
+ it 'top' do
353
+ newbie = ZeroBasedOrderable.create! :move_to => :top
354
+ positions.should == [0, 1, 2, 3, 4, 5]
355
+ newbie.position.should == 0
356
+ end
357
+
358
+ it 'bottom' do
359
+ newbie = ZeroBasedOrderable.create! :move_to => :bottom
360
+ positions.should == [0, 1, 2, 3, 4, 5]
361
+ newbie.position.should == 5
362
+ end
363
+
364
+ it 'middle' do
365
+ newbie = ZeroBasedOrderable.create! :move_to => 3
366
+ positions.should == [0, 1, 2, 3, 4, 5]
367
+ newbie.position.should == 3
368
+ end
369
+
370
+ end
371
+
372
+ describe 'movement' do
373
+
374
+ it 'higher from top' do
375
+ record = ZeroBasedOrderable.where(:position => 0).first
376
+ record.update_attributes :move_to => :higher
377
+ positions.should == [0, 1, 2, 3, 4]
378
+ record.reload.position.should == 0
379
+ end
380
+
381
+ it 'higher from bottom' do
382
+ record = ZeroBasedOrderable.where(:position => 4).first
383
+ record.update_attributes :move_to => :higher
384
+ positions.should == [0, 1, 2, 3, 4]
385
+ record.reload.position.should == 3
386
+ end
387
+
388
+ it 'higher from middle' do
389
+ record = ZeroBasedOrderable.where(:position => 3).first
390
+ record.update_attributes :move_to => :higher
391
+ positions.should == [0, 1, 2, 3, 4]
392
+ record.reload.position.should == 2
393
+ end
394
+
395
+ it 'lower from top' do
396
+ record = ZeroBasedOrderable.where(:position => 0).first
397
+ record.update_attributes :move_to => :lower
398
+ positions.should == [0, 1, 2, 3, 4]
399
+ record.reload.position.should == 1
400
+ end
401
+
402
+ it 'lower from bottom' do
403
+ record = ZeroBasedOrderable.where(:position => 4).first
404
+ record.update_attributes :move_to => :lower
405
+ positions.should == [0, 1, 2, 3, 4]
406
+ record.reload.position.should == 4
407
+ end
408
+
409
+ it 'lower from middle' do
410
+ record = ZeroBasedOrderable.where(:position => 2).first
411
+ record.update_attributes :move_to => :lower
412
+ positions.should == [0, 1, 2, 3, 4]
413
+ record.reload.position.should == 3
414
+ end
415
+
416
+ it "does nothing if position not change" do
417
+ record = ZeroBasedOrderable.where(:position => 3).first
418
+ record.save
419
+ positions.should == [0, 1, 2, 3, 4]
420
+ record.reload.position.should == 3
421
+ end
422
+
423
+ end
424
+
425
+ end
426
+
293
427
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_orderable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-23 00:00:00.000000000 Z
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -95,16 +95,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
95
  - - ! '>='
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
+ segments:
99
+ - 0
100
+ hash: 796603552408515910
98
101
  required_rubygems_version: !ruby/object:Gem::Requirement
99
102
  none: false
100
103
  requirements:
101
104
  - - ! '>='
102
105
  - !ruby/object:Gem::Version
103
106
  version: '0'
107
+ segments:
108
+ - 0
109
+ hash: 796603552408515910
104
110
  requirements: []
105
111
  rubyforge_project: mongoid_orderable
106
112
  rubygems_version: 1.8.24
107
113
  signing_key:
108
114
  specification_version: 3
109
115
  summary: Acts as list mongoid implementation
110
- test_files: []
116
+ test_files:
117
+ - spec/mongoid/orderable_spec.rb
118
+ - spec/spec_helper.rb