simple_nested_set 0.0.8 → 0.0.9

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.
@@ -17,6 +17,10 @@ module SimpleNestedSet
17
17
  autoload :Impossible, 'simple_nested_set/move/protection'
18
18
  autoload :Inconsistent, 'simple_nested_set/move/protection'
19
19
  end
20
+
21
+ module Rebuild
22
+ autoload :FromPaths, 'simple_nested_set/rebuild/from_paths'
23
+ end
20
24
  end
21
25
 
22
26
  ActiveRecord::Base.send :extend, SimpleNestedSet::ActMacro
@@ -97,6 +97,10 @@ module SimpleNestedSet
97
97
  Move::ToTarget.new(node, target, position).perform
98
98
  end
99
99
 
100
+ def rebuild_from_paths!
101
+ Rebuild::FromPaths.new.run(self)
102
+ end
103
+
100
104
  def denormalize_level_query
101
105
  query = arel_table.as(:l)
102
106
  query = query.project('count(id)').
@@ -0,0 +1,37 @@
1
+ module SimpleNestedSet
2
+ module Rebuild
3
+ class FromPaths
4
+ attr_writer :num
5
+
6
+ def num
7
+ @num ||= 0
8
+ end
9
+
10
+ def run(nested_set)
11
+ nodes = nested_set.order(:path).to_a
12
+ renumber(nodes.dup)
13
+ nodes.each(&:save)
14
+ end
15
+
16
+ def renumber(nodes)
17
+ until nodes.empty?
18
+ node = nodes.shift
19
+ node.lft = self.num += 1
20
+ num = renumber(extract_children(node, nodes))
21
+ node.rgt = self.num += 1
22
+ end
23
+ num
24
+ end
25
+
26
+ def extract_children(node, nodes)
27
+ children = nodes.select { |child| child?(node, child) }
28
+ nodes.replace(nodes - children)
29
+ children
30
+ end
31
+
32
+ def child?(node, child)
33
+ child && child.path =~ %r(^#{node.path}/)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleNestedSet
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_nested_set
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-20 00:00:00 +02:00
18
+ date: 2010-08-21 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -109,6 +109,7 @@ files:
109
109
  - lib/simple_nested_set/move/protection.rb
110
110
  - lib/simple_nested_set/move/to_target.rb
111
111
  - lib/simple_nested_set/nested_set.rb
112
+ - lib/simple_nested_set/rebuild/from_paths.rb
112
113
  - lib/simple_nested_set/sql_abstraction.rb
113
114
  - lib/simple_nested_set/version.rb
114
115
  has_rdoc: true