mongoid-tree-rational 0.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -4
- data/Gemfile +6 -3
- data/README.md +39 -4
- data/Rakefile +0 -2
- data/VERSION +1 -1
- data/lib/mongoid/locale/en.yml +1 -1
- data/lib/mongoid/tree.rb +8 -9
- data/lib/mongoid/tree/ordering.rb +12 -12
- data/lib/mongoid/tree/rational_numbering.rb +100 -74
- data/mongoid-tree-rational.gemspec +15 -14
- data/spec/mongoid/tree/rational_numbering_spec.rb +188 -0
- data/spec/mongoid/tree/traversal_spec.rb +1 -1
- data/spec/mongoid/tree_spec.rb +16 -2
- data/spec/support/models/node.rb +26 -3
- metadata +59 -94
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05a801b4f6d70f729fa1f32d6836dc5404beb672
|
4
|
+
data.tar.gz: 500f13152270fe33af60bb9536f796f790082310
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 868ea89f0264f823d4deb7046e0078a2b2b74059326dc8b477299856539ea7d265f058912eecd8160190eca5e8b434922b85b5e57d79b79f6c34e4d6d737ceaa
|
7
|
+
data.tar.gz: 72d879b3c8ac06062bde4ebfb2ddcb68e832ea517744fe8130cd1846728ffdb2a879842f686e6e007a9a85216f7897873765fcea5f61130cf59cba29aac97d52
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem 'mongoid', ['<=
|
3
|
+
gem 'mongoid', ['<= 5.0', '>= 4.0']
|
4
4
|
gem 'rational_number'
|
5
5
|
|
6
6
|
group :development do
|
@@ -12,12 +12,15 @@ group :development do
|
|
12
12
|
gem 'rb-inotify', :require => false
|
13
13
|
gem 'rb-fsevent', :require => false
|
14
14
|
gem 'wdm', :platforms => [:mswin, :mingw], :require => false
|
15
|
-
gem 'hirb'
|
16
|
-
gem 'wirble'
|
17
15
|
gem 'awesome_print'
|
16
|
+
gem 'timecop'
|
18
17
|
end
|
19
18
|
|
20
19
|
group :development, :test do
|
21
20
|
gem 'coveralls', :require => false
|
22
21
|
gem 'simplecov', :require => false
|
23
22
|
end
|
23
|
+
|
24
|
+
platforms :rbx do
|
25
|
+
gem 'rubysl-rake', '~> 2.0'
|
26
|
+
end
|
data/README.md
CHANGED
@@ -4,9 +4,10 @@ A tree structure for Mongoid documents using rational numbers and materialized p
|
|
4
4
|
|
5
5
|
## Requirements
|
6
6
|
|
7
|
-
* mongoid (~>
|
7
|
+
* mongoid (~> 4.0)
|
8
8
|
|
9
|
-
|
9
|
+
Version 2.x.x supports mongoid 4.x
|
10
|
+
Version 0.1.x supports Mongoid 3.x
|
10
11
|
|
11
12
|
## Install
|
12
13
|
|
@@ -18,7 +19,7 @@ In order to get the latest development version of mongoid-tree:
|
|
18
19
|
|
19
20
|
gem 'mongoid-tree-rational', :git => 'git://github.com/boxcms/mongoid-tree-rational', :require => 'mongoid/tree'
|
20
21
|
|
21
|
-
You might want to
|
22
|
+
You might want to add `:require => nil` option and explicitly `require 'mongoid/tree'` where needed and finally run
|
22
23
|
|
23
24
|
bundle install
|
24
25
|
|
@@ -258,9 +259,43 @@ class Node
|
|
258
259
|
end
|
259
260
|
```
|
260
261
|
|
262
|
+
### Timestamps
|
263
|
+
|
264
|
+
Per default timestamps are only updated on the a node that is changed, and not siblings that are moved/shifted due to changes on a given node. Usually the tree position of a document does not give information about changes to the content of the document. This behaviour can be changed through the option ```auto_tree_timestamping``` .
|
265
|
+
|
266
|
+
Disable auto timestamps:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
class Node
|
270
|
+
include Mongoid::Document
|
271
|
+
include Mongoid::Tree
|
272
|
+
include Mongoid::Tree::RationalNumbering
|
273
|
+
|
274
|
+
self.auto_tree_timestamping = false
|
275
|
+
|
276
|
+
validates_associated :parent, :children
|
277
|
+
end
|
278
|
+
```
|
279
|
+
|
280
|
+
Enable auto timestamps: (default behaviour)
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
class Node
|
284
|
+
include Mongoid::Document
|
285
|
+
include Mongoid::Tree
|
286
|
+
include Mongoid::Tree::RationalNumbering
|
287
|
+
|
288
|
+
self.auto_tree_timestamping = false
|
289
|
+
|
290
|
+
validates_associated :parent, :children
|
291
|
+
end
|
292
|
+
```
|
293
|
+
|
294
|
+
|
295
|
+
|
261
296
|
## Build Status
|
262
297
|
|
263
|
-
mongoid-tree is on [Travis CI](http://travis-ci.org/boxcms/mongoid-tree-rational) running the specs
|
298
|
+
mongoid-tree is on tested on [Travis CI](http://travis-ci.org/boxcms/mongoid-tree-rational) running the specs against ruby 2.0.0, ruby 2.1.2 and rbx-2
|
264
299
|
|
265
300
|
## Known issues
|
266
301
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
2.0.0
|
data/lib/mongoid/locale/en.yml
CHANGED
@@ -3,7 +3,7 @@ en:
|
|
3
3
|
errors:
|
4
4
|
messages:
|
5
5
|
tree:
|
6
|
-
cyclic: "Can't be
|
6
|
+
cyclic: "Can't be child of a descendant"
|
7
7
|
search_class_mismatch: "Mismatch between search classes. Parent: %{parent_search_class} Node: %{node_search_class}"
|
8
8
|
rational:
|
9
9
|
incorrect_parent: "Positional values doesn't match parent (check nv/dv values)"
|
data/lib/mongoid/tree.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Mongoid
|
2
4
|
##
|
3
5
|
# = Mongoid::Tree
|
@@ -88,8 +90,10 @@ module Mongoid
|
|
88
90
|
|
89
91
|
belongs_to :parent, :class_name => self.name, :inverse_of => :children, :index => true, :validate => false
|
90
92
|
|
91
|
-
field :parent_ids, :
|
93
|
+
field :parent_ids, type: Array, default: []
|
92
94
|
index :parent_ids => 1
|
95
|
+
field :depth, type: Integer
|
96
|
+
index depth: 1
|
93
97
|
|
94
98
|
set_callback :save, :after, :rearrange_children, :if => :rearrange_children?
|
95
99
|
set_callback :validation, :before do
|
@@ -248,7 +252,7 @@ module Mongoid
|
|
248
252
|
#
|
249
253
|
# @return [Fixnum] Depth of this document
|
250
254
|
def depth
|
251
|
-
parent_ids.count
|
255
|
+
super || parent_ids.count
|
252
256
|
end
|
253
257
|
|
254
258
|
##
|
@@ -273,13 +277,7 @@ module Mongoid
|
|
273
277
|
#
|
274
278
|
# @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents ancestors
|
275
279
|
def ancestors
|
276
|
-
|
277
|
-
base_class.and({
|
278
|
-
'$or' => parent_ids.map { |id| { :_id => id } }
|
279
|
-
})
|
280
|
-
else
|
281
|
-
base_class.where(:_id.in => [])
|
282
|
-
end
|
280
|
+
base_class.where(:_id.in => parent_ids).order(:depth => :asc)
|
283
281
|
end
|
284
282
|
|
285
283
|
##
|
@@ -428,6 +426,7 @@ module Mongoid
|
|
428
426
|
else
|
429
427
|
self.parent_ids = []
|
430
428
|
end
|
429
|
+
self.depth = parent_ids.size
|
431
430
|
rearrange_children! if self.parent_ids_changed?
|
432
431
|
end
|
433
432
|
|
@@ -33,9 +33,9 @@ module Mongoid
|
|
33
33
|
extend ActiveSupport::Concern
|
34
34
|
|
35
35
|
included do
|
36
|
-
field :position, :
|
36
|
+
field :position, type: Integer
|
37
37
|
|
38
|
-
default_scope asc(:position)
|
38
|
+
default_scope ->{ asc(:position) }
|
39
39
|
|
40
40
|
before_save :assign_default_position, :if => :assign_default_position?
|
41
41
|
before_save :reposition_former_siblings, :if => :sibling_reposition_required?
|
@@ -157,11 +157,11 @@ module Mongoid
|
|
157
157
|
|
158
158
|
if position > other.position
|
159
159
|
new_position = other.position
|
160
|
-
self.siblings_between(other).inc(:
|
161
|
-
other.inc(:
|
160
|
+
self.siblings_between(other).inc(position: 1)
|
161
|
+
other.inc(position: 1)
|
162
162
|
else
|
163
163
|
new_position = other.position - 1
|
164
|
-
self.siblings_between(other).inc(:
|
164
|
+
self.siblings_between(other).inc(position: -1)
|
165
165
|
end
|
166
166
|
|
167
167
|
self.position = new_position
|
@@ -181,11 +181,11 @@ module Mongoid
|
|
181
181
|
|
182
182
|
if position > other.position
|
183
183
|
new_position = other.position + 1
|
184
|
-
self.siblings_between(other).inc(:
|
184
|
+
self.siblings_between(other).inc(position: 1)
|
185
185
|
else
|
186
186
|
new_position = other.position
|
187
|
-
self.siblings_between(other).inc(:
|
188
|
-
other.inc(:
|
187
|
+
self.siblings_between(other).inc(position: -1)
|
188
|
+
other.inc(position: -1)
|
189
189
|
end
|
190
190
|
|
191
191
|
self.position = new_position
|
@@ -195,8 +195,8 @@ module Mongoid
|
|
195
195
|
private
|
196
196
|
|
197
197
|
def switch_with_sibling_at_offset(offset)
|
198
|
-
siblings.where(:
|
199
|
-
inc(:
|
198
|
+
siblings.where(position: self.position + offset).first.inc(position: -offset)
|
199
|
+
inc(position: offset)
|
200
200
|
end
|
201
201
|
|
202
202
|
def ensure_to_be_sibling_of(other)
|
@@ -206,14 +206,14 @@ module Mongoid
|
|
206
206
|
end
|
207
207
|
|
208
208
|
def move_lower_siblings_up
|
209
|
-
lower_siblings.inc(:
|
209
|
+
lower_siblings.inc(position: -1)
|
210
210
|
end
|
211
211
|
|
212
212
|
def reposition_former_siblings
|
213
213
|
former_siblings = base_class.where(:parent_id => attribute_was('parent_id')).
|
214
214
|
and(:position.gt => (attribute_was('position') || 0)).
|
215
215
|
excludes(:id => self.id)
|
216
|
-
former_siblings.inc(:
|
216
|
+
former_siblings.inc(position: -1)
|
217
217
|
end
|
218
218
|
|
219
219
|
def sibling_reposition_required?
|
@@ -22,11 +22,11 @@ module Mongoid
|
|
22
22
|
@@_disable_timestamp_count = 0
|
23
23
|
|
24
24
|
included do
|
25
|
-
field :rational_number_nv, :
|
26
|
-
field :rational_number_dv, :
|
27
|
-
field :rational_number_snv, :
|
28
|
-
field :rational_number_sdv, :
|
29
|
-
field :rational_number_value, :
|
25
|
+
field :rational_number_nv, type: Integer, default: 0
|
26
|
+
field :rational_number_dv, type: Integer, default: 1
|
27
|
+
field :rational_number_snv, type: Integer, default: 1
|
28
|
+
field :rational_number_sdv, type: Integer, default: 0
|
29
|
+
field :rational_number_value, type: BigDecimal
|
30
30
|
|
31
31
|
validate :validate_rational_hierarchy
|
32
32
|
|
@@ -47,11 +47,35 @@ module Mongoid
|
|
47
47
|
|
48
48
|
after_destroy :move_lower_siblings
|
49
49
|
|
50
|
-
default_scope asc(:rational_number_value)
|
50
|
+
default_scope ->{ asc(:rational_number_value) }
|
51
51
|
|
52
52
|
end # included do
|
53
53
|
|
54
54
|
module ClassMethods
|
55
|
+
attr_writer :auto_tree_timestamping
|
56
|
+
|
57
|
+
def auto_tree_timestamping
|
58
|
+
@auto_tree_timestamping.nil? ? true : @auto_tree_timestamping
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# Set options for rational numbers
|
63
|
+
#
|
64
|
+
# @param [Hash] options a hash
|
65
|
+
#
|
66
|
+
# :auto_tree_timestamping (true/false)
|
67
|
+
# Per default timestamps are only updated on the a node that is changed, and not siblings that are moved/shifted
|
68
|
+
# due to changes on a given node. Usually the tree position of a document does not give information about
|
69
|
+
# changes to the content of the document. This behaviour can be changed through the option
|
70
|
+
# ':auto_tree_timestamping'.
|
71
|
+
|
72
|
+
def rational_number_options(opts)
|
73
|
+
if !opts[:auto_tree_timestamping].nil?
|
74
|
+
@auto_tree_timestamping = !!opts[:auto_tree_timestamping]
|
75
|
+
else
|
76
|
+
@auto_tree_timestamping = true
|
77
|
+
end
|
78
|
+
end
|
55
79
|
|
56
80
|
# helper metods for nv/dv
|
57
81
|
|
@@ -103,7 +127,6 @@ module Mongoid
|
|
103
127
|
#
|
104
128
|
def validate_rational_hierarchy
|
105
129
|
if self.rational_number_nv_changed? && self.rational_number_dv_changed?
|
106
|
-
# puts "#{self.name} #{self.changes.inspect}"
|
107
130
|
unless correct_rational_parent?(self.rational_number_nv, self.rational_number_dv)
|
108
131
|
errors.add(:base, I18n.t(:cyclic, :scope => [:mongoid, :errors, :messages, :tree]))
|
109
132
|
end
|
@@ -147,6 +170,10 @@ module Mongoid
|
|
147
170
|
|
148
171
|
# shouldn't be any conflicting sibling now...
|
149
172
|
self.from_rational_number(RationalNumber.new(nv,dv))
|
173
|
+
# if parent_id is unknown, find parent and set correct parent_id
|
174
|
+
if self.parent_id.nil? and self.rational_number.root?
|
175
|
+
# puts "!!!!!!!!! #{self.name} move_to_rational_number missing parent and NOT root rational number!"
|
176
|
+
end
|
150
177
|
end
|
151
178
|
|
152
179
|
##
|
@@ -173,7 +200,7 @@ module Mongoid
|
|
173
200
|
other = base_class.where(:rational_number_nv => nv).where(:rational_number_dv => dv).excludes(:id => self.id).first
|
174
201
|
already_sibling_of = other.nil? ? false : self.sibling_of?(other)
|
175
202
|
|
176
|
-
# puts "
|
203
|
+
# puts " conflicting node: #{other.nil? ? '-' : other.name } already_sibling_of :#{already_sibling_of}"
|
177
204
|
return false if ensure_to_have_correct_parent(nv,dv) == false
|
178
205
|
|
179
206
|
move_to_rational = RationalNumber.new(nv,dv)
|
@@ -245,12 +272,15 @@ module Mongoid
|
|
245
272
|
|
246
273
|
conflicting_sibling = base_class.where(:rational_number_nv => nv).where(:rational_number_dv => dv).excludes(:id => self.id).first
|
247
274
|
if (conflicting_sibling != nil)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
275
|
+
# puts "moving conflicting nodes"
|
276
|
+
without_timestamping do
|
277
|
+
# ensure_to_be_sibling_of(conflicting_sibling)
|
278
|
+
return if conflicting_sibling.position == self.position + 1
|
279
|
+
# If there are nodes between this and conflicting_sibling before move, make sure their position shifted before moving
|
280
|
+
_direction = (self.position > conflicting_sibling.position ? 1 : -1)
|
281
|
+
_position = (_direction < 0 ? conflicting_sibling.position + _direction : conflicting_sibling.position)
|
282
|
+
shift_nodes_position(conflicting_sibling, _direction, (_direction > 0 ? false : true))
|
283
|
+
end
|
254
284
|
end
|
255
285
|
end
|
256
286
|
|
@@ -261,6 +291,7 @@ module Mongoid
|
|
261
291
|
# @return [RationalNumber] returns the rational number for the ancestor or nil for "not found"
|
262
292
|
#
|
263
293
|
def query_ancestor_rational_number
|
294
|
+
# puts " #{self.name} query_ancestor_rational_number parent_id: #{self.parent_id}"
|
264
295
|
check_parent = base_class.where(:_id => self.parent_id).first
|
265
296
|
return nil if (check_parent.nil? || check_parent == [])
|
266
297
|
check_parent.rational_number
|
@@ -274,6 +305,7 @@ module Mongoid
|
|
274
305
|
#
|
275
306
|
def correct_rational_parent?(nv, dv)
|
276
307
|
q_rational_number = query_ancestor_rational_number
|
308
|
+
# puts " #{self.name} correct_rational_parent? nv: #{nv} dv: #{dv} query_ancestor_rational_number: #{q_rational_number.inspect}"
|
277
309
|
if q_rational_number.nil?
|
278
310
|
if RationalNumber.new(nv,dv).parent.root?
|
279
311
|
return true
|
@@ -330,37 +362,6 @@ module Mongoid
|
|
330
362
|
end
|
331
363
|
end
|
332
364
|
|
333
|
-
##
|
334
|
-
#
|
335
|
-
# Not needed, as each child gets the rational number updated after updating path?
|
336
|
-
# @return [undefined]
|
337
|
-
#
|
338
|
-
# def children_update_rational_number
|
339
|
-
# if rearrange_children?
|
340
|
-
# _position = 0
|
341
|
-
# # self.disable_timestamp_callback()
|
342
|
-
# self.children.each do |child|
|
343
|
-
# child.update_rational_number!(:position => _position)
|
344
|
-
# _position += 1
|
345
|
-
# end
|
346
|
-
# # self.enable_timestamp_callback()
|
347
|
-
# end
|
348
|
-
# end
|
349
|
-
|
350
|
-
##
|
351
|
-
# Enable timestamping callback if existing
|
352
|
-
#
|
353
|
-
def enable_timestamp_callback
|
354
|
-
|
355
|
-
end
|
356
|
-
|
357
|
-
##
|
358
|
-
# Disable timestamping callback if existing
|
359
|
-
#
|
360
|
-
def disable_timestamp_callback
|
361
|
-
|
362
|
-
end
|
363
|
-
|
364
365
|
##
|
365
366
|
# Convert to rational number
|
366
367
|
#
|
@@ -517,29 +518,33 @@ module Mongoid
|
|
517
518
|
#
|
518
519
|
#
|
519
520
|
def shift_nodes_position(other, direction, exclude_other = false)
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
521
|
+
without_timestamping do
|
522
|
+
# puts "#{self.name} shift_nodes_position other: #{other.name} direction #{direction} exclude_other: #{exclude_other}"
|
523
|
+
if exclude_other
|
524
|
+
nodes_to_shift = siblings_between(other)
|
525
|
+
else
|
526
|
+
nodes_to_shift = siblings_between_including_other(other)
|
527
|
+
end
|
528
|
+
shift_nodes(nodes_to_shift, direction)
|
525
529
|
end
|
526
|
-
shift_nodes(nodes_to_shift, direction)
|
527
530
|
end
|
528
531
|
|
529
532
|
def shift_lower_nodes_from_other(other, direction)
|
530
|
-
# puts "#{self.name} shift_lower_nodes_from_other other: #{other.name} direction: #{direction}"
|
531
|
-
range = [other.rational_number_value, other.
|
533
|
+
# puts "#{self.name} shift_lower_nodes_from_other other: #{other.name} direction: #{direction} other.siblings_and_self.count: #{other.siblings_and_self.count}"
|
534
|
+
range = [other.rational_number_value, other.siblings_and_self.last.rational_number_value].sort
|
532
535
|
nodes_to_shift = other.siblings_and_self.where(:rational_number_value.gte => range.first, :rational_number_value.lte => range.last)
|
533
536
|
shift_nodes(nodes_to_shift, direction)
|
534
537
|
end
|
535
538
|
|
536
539
|
def shift_nodes(nodes_to_shift, direction)
|
537
540
|
# puts "#{self.name} shift_nodes direction: #{direction}"
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
541
|
+
without_timestamping do
|
542
|
+
nodes_to_shift.each do |node_to_shift|
|
543
|
+
pos = node_to_shift.position + direction
|
544
|
+
# puts " shifting #{node_to_shift.name} from position #{node_to_shift.position} to #{pos}"
|
545
|
+
node_to_shift.move_to_position(pos, {:force => true})
|
546
|
+
node_to_shift.save_with_force_rational_numbers!
|
547
|
+
end
|
543
548
|
end
|
544
549
|
end
|
545
550
|
|
@@ -589,14 +594,29 @@ module Mongoid
|
|
589
594
|
@_rational_moving_nodes = false
|
590
595
|
end
|
591
596
|
|
597
|
+
|
598
|
+
## These are only used when a document is shifted/moved because of repositioning another document.
|
599
|
+
|
600
|
+
##
|
601
|
+
# Call block without triggeringtimestamps
|
602
|
+
# @param [&block] code block to call
|
603
|
+
|
604
|
+
def without_timestamping(&block)
|
605
|
+
# # puts "without_timestamping: Automagic timpestamping enabled? #{self.class.auto_tree_timestamping}"
|
606
|
+
disable_timestamp_callback() if self.class.auto_tree_timestamping
|
607
|
+
yield
|
608
|
+
enable_timestamp_callback() if self.class.auto_tree_timestamping
|
609
|
+
end
|
610
|
+
|
592
611
|
##
|
593
612
|
# Disable the timestamps for the document type, and increase the disable count
|
594
613
|
# Will only disable once, even if called multiple times
|
595
614
|
#
|
596
615
|
# @return [undefined]
|
597
616
|
def disable_timestamp_callback
|
617
|
+
# # puts "Disabling timestamp callback count: #{@@_disable_timestamp_count}"
|
598
618
|
if self.respond_to?("updated_at")
|
599
|
-
self.class.skip_callback(:
|
619
|
+
self.class.skip_callback(:update, :before, :set_updated_at ) if @@_disable_timestamp_count == 0
|
600
620
|
@@_disable_timestamp_count += 1
|
601
621
|
end
|
602
622
|
end
|
@@ -607,9 +627,10 @@ module Mongoid
|
|
607
627
|
#
|
608
628
|
# @return [undefined]
|
609
629
|
def enable_timestamp_callback
|
630
|
+
# # puts "Enabling timestamp callback count: #{@@_disable_timestamp_count}"
|
610
631
|
if self.respond_to?("updated_at")
|
611
632
|
@@_disable_timestamp_count -= 1
|
612
|
-
self.class.set_callback(:
|
633
|
+
self.class.set_callback(:update, :before, :set_updated_at ) if @@_disable_timestamp_count == 0
|
613
634
|
end
|
614
635
|
end
|
615
636
|
|
@@ -692,6 +713,7 @@ module Mongoid
|
|
692
713
|
# save when forcing rational numbers
|
693
714
|
#
|
694
715
|
def save_with_force_rational_numbers!
|
716
|
+
# puts "-- Saving #{self.name} #{self.updated_at.utc}" if self.respond_to?("updated_at")
|
695
717
|
@_forced_rational_number = true
|
696
718
|
self.save!
|
697
719
|
@_forced_rational_number = false
|
@@ -730,9 +752,11 @@ module Mongoid
|
|
730
752
|
def switch_with_sibling(sibling)
|
731
753
|
self_pos = self.position
|
732
754
|
sibling_pos = sibling.position
|
733
|
-
|
734
|
-
|
735
|
-
|
755
|
+
without_timestamping do
|
756
|
+
sibling.move_to_position(self_pos, {:force => true})
|
757
|
+
self.move_to_position(sibling_pos, {:force => true})
|
758
|
+
sibling.save_with_force_rational_numbers!
|
759
|
+
end
|
736
760
|
self.save_with_force_rational_numbers!
|
737
761
|
end
|
738
762
|
|
@@ -755,13 +779,18 @@ module Mongoid
|
|
755
779
|
new_rational_number = RationalNumber.new(nv,dv)
|
756
780
|
new_parent = nil
|
757
781
|
# puts " root: #{new_rational_number.root?} #{("parent: " + self.parent.name + " nv/dv : "+ self.parent.rational_number.nv.to_s+ "/"+ self.parent.rational_number.dv.to_s) unless self.parent.nil?}#{"parent: nil" if self.parent.nil?}"
|
758
|
-
if self.parent
|
782
|
+
if self.parent.nil?
|
783
|
+
# puts " new_rational_number.parent == RationalNumber.new #{new_rational_number.parent == RationalNumber.new}"
|
759
784
|
return true if new_rational_number.parent == RationalNumber.new
|
785
|
+
new_parent = base_class.where(:rational_number_nv => new_rational_number.parent.nv, :rational_number_dv => new_rational_number.parent.dv).first
|
760
786
|
elsif new_rational_number.parent.root?
|
787
|
+
# puts " new_rational_number.parent.root? #{new_rational_number.parent.root?}"
|
761
788
|
new_parent = nil
|
762
789
|
else
|
790
|
+
# puts " self.parent.rational_number == new_rational_number.parent #{self.parent.rational_number == new_rational_number.parent}"
|
763
791
|
return true if self.parent.rational_number == new_rational_number.parent
|
764
|
-
|
792
|
+
# puts " searching for parent: #{new_rational_number.parent.nv}, #{new_rational_number.parent.dv}"
|
793
|
+
new_parent = base_class.where(:rational_number_nv => new_rational_number.parent.nv, :rational_number_dv => new_rational_number.parent.dv).first
|
765
794
|
return false if new_parent.nil? # INVALID PARENT
|
766
795
|
end
|
767
796
|
# If entered here, the parent needs to change
|
@@ -769,14 +798,13 @@ module Mongoid
|
|
769
798
|
self.parent = new_parent
|
770
799
|
end
|
771
800
|
|
772
|
-
#
|
773
|
-
|
801
|
+
# Shifting/rekeying of lower siblings on destroy
|
774
802
|
def move_lower_siblings
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
803
|
+
without_timestamping do
|
804
|
+
lower_siblings.each do |sibling|
|
805
|
+
sibling.move_to_position(sibling.position - 1)
|
806
|
+
sibling.save_with_force_rational_numbers!
|
807
|
+
end
|
780
808
|
end
|
781
809
|
end
|
782
810
|
|
@@ -791,8 +819,6 @@ module Mongoid
|
|
791
819
|
# parent_id_changed? && persisted?
|
792
820
|
# end
|
793
821
|
|
794
|
-
|
795
|
-
|
796
822
|
end # RationalNumbering
|
797
823
|
end # Tree
|
798
824
|
end # Mongoid
|