simple_nested_set 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,8 +7,8 @@ module SimpleNestedSet
7
7
  end
8
8
 
9
9
  def attributes=(attributes)
10
- @_nested_set_attributes = attributes.extract_nested_set_attributes!
11
- super
10
+ @_nested_set_attributes = nested_set_class.extract_attributes!(attributes)
11
+ super(attributes)
12
12
  end
13
13
 
14
14
  # recursively populates the parent and children associations of self and
@@ -160,5 +160,10 @@ module SimpleNestedSet
160
160
  def move_to_right_of(node)
161
161
  nested_set.move_to(node, :right)
162
162
  end
163
+
164
+ # Makes this node to the given path
165
+ def move_to_path(path)
166
+ nested_set.move_to_path(path)
167
+ end
163
168
  end
164
169
  end
@@ -1,18 +1,9 @@
1
1
  module SimpleNestedSet
2
2
  module Move
3
3
  class ByAttributes
4
- class << self
5
- def attribute_reader(*names)
6
- names.each do |name|
7
- define_method(name) { attributes[name].blank? ? nil : attributes[name].to_i }
8
- end
9
- end
10
- end
11
-
12
4
  include Protection
13
5
 
14
6
  attr_reader :node, :attributes
15
- attribute_reader :parent_id, :left_id, :right_id
16
7
 
17
8
  delegate :nested_set, :to => :node
18
9
 
@@ -23,7 +14,9 @@ module SimpleNestedSet
23
14
  end
24
15
 
25
16
  def perform
26
- if left_id && left_id != node.id
17
+ if path && node.path_changed?
18
+ node.move_to_path(path)
19
+ elsif left_id && left_id != node.id
27
20
  node.move_to_right_of(left_id)
28
21
  elsif right_id && right_id != node.id
29
22
  node.move_to_left_of(right_id)
@@ -34,6 +27,22 @@ module SimpleNestedSet
34
27
 
35
28
  protected
36
29
 
30
+ def parent_id
31
+ attributes[:parent_id].blank? ? nil : attributes[:parent_id].to_i
32
+ end
33
+
34
+ def left_id
35
+ attributes[:left_id].blank? ? nil : attributes[:left_id].to_i
36
+ end
37
+
38
+ def right_id
39
+ attributes[:right_id].blank? ? nil : attributes[:right_id].to_i
40
+ end
41
+
42
+ def path
43
+ attributes[:path].blank? ? nil : attributes[:path]
44
+ end
45
+
37
46
  def normalize_attributes!
38
47
  attributes.symbolize_keys!
39
48
  attributes.each { |key, value| attributes[key] = nil if value == 'null' }
@@ -15,11 +15,9 @@ module SimpleNestedSet
15
15
 
16
16
  def perform
17
17
  node.run_callbacks(:move) do
18
- # unless bound == node.rgt || bound == node.lft # there would be no change
19
- nested_set.transaction do
20
- update_structure!
21
- end
22
- # end
18
+ unless bound == node.rgt || bound == node.lft # there would be no change
19
+ nested_set.transaction { update_structure! }
20
+ end
23
21
  reload
24
22
  end
25
23
  end
@@ -21,6 +21,12 @@ module SimpleNestedSet
21
21
  c.merge(name => scope.respond_to?(name) ? scope.send(name) : scope[name])
22
22
  end
23
23
  end
24
+
25
+ def extract_attributes!(attributes)
26
+ attributes.slice(*SimpleNestedSet::ATTRIBUTES).tap do
27
+ attributes.except!(*(SimpleNestedSet::ATTRIBUTES - [:path]))
28
+ end
29
+ end
24
30
  end
25
31
 
26
32
  attr_reader :node
@@ -52,9 +58,12 @@ module SimpleNestedSet
52
58
  scope_names.all? { |scope| node.send(scope) == other.send(scope) }
53
59
  end
54
60
 
55
- # reload left, right, and parent
61
+ # reload nested set attributes
56
62
  def reload
57
- node.reload(:select => attribute_names.join(', ')) unless node.new_record? # FIXME reloading doesn't seem to work?
63
+ reloaded = unscoped { find(node.id, :select => [:parent_id, :lft, :rgt, :level, :path]) }
64
+ node.instance_eval { @attributes.merge!(reloaded.instance_variable_get(:@attributes)) }
65
+ node.parent = nil if node.parent_id.nil?
66
+ node.children.reset
58
67
  end
59
68
 
60
69
  def attribute_names
@@ -93,6 +102,12 @@ module SimpleNestedSet
93
102
  Move::ByAttributes.new(node, attributes).perform
94
103
  end
95
104
 
105
+ def move_to_path(path)
106
+ node.path, parent_path = path, path.split('/')[0..-2].join('/')
107
+ parent = parent_path.empty? ? nil : node.nested_set.where(:path => parent_path).first
108
+ node.move_to_child_of(parent)
109
+ end
110
+
96
111
  def move_to(target, position)
97
112
  Move::ToTarget.new(node, target, position).perform
98
113
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleNestedSet
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'active_record'
2
- require 'core_ext/hash/extract_nested_set_attributes'
3
2
 
4
3
  module SimpleNestedSet
5
4
  ATTRIBUTES = [:parent, :parent_id, :left_id, :right_id, :lft, :rgt, :level, :path]
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: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 9
10
- version: 0.0.9
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -100,7 +100,6 @@ extensions: []
100
100
  extra_rdoc_files: []
101
101
 
102
102
  files:
103
- - lib/core_ext/hash/extract_nested_set_attributes.rb
104
103
  - lib/simple_nested_set.rb
105
104
  - lib/simple_nested_set/act_macro.rb
106
105
  - lib/simple_nested_set/class_methods.rb
@@ -1,8 +0,0 @@
1
- class Hash
2
- def extract_nested_set_attributes!
3
- slice(*SimpleNestedSet::ATTRIBUTES).tap do
4
- except!(*SimpleNestedSet::ATTRIBUTES)
5
- end
6
- end
7
- end
8
-