simple_nested_set 0.0.3 → 0.0.4

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.
@@ -6,9 +6,7 @@ module SimpleNestedSet
6
6
  include SimpleNestedSet::InstanceMethods
7
7
  extend SimpleNestedSet::ClassMethods
8
8
 
9
- # TODO get callbacks working
10
- # define_callbacks :move, :terminator => "result == false"
11
- # before_move :init_as_node
9
+ define_callbacks :move, :terminator => "result == false"
12
10
 
13
11
  before_validation lambda { |r| r.nested_set.init_as_node }
14
12
  before_destroy lambda { |r| r.nested_set.prune_branch }
@@ -2,6 +2,14 @@ require 'active_support/core_ext/hash/slice'
2
2
 
3
3
  module SimpleNestedSet
4
4
  module ClassMethods
5
+ def before_move(*args, &block)
6
+ set_callback(:move, :before, *args, &block)
7
+ end
8
+
9
+ def after_move(*args, &block)
10
+ set_callback(:move, :after, *args, &block)
11
+ end
12
+
5
13
  def create(attributes)
6
14
  nested_set_class.with_move_by_attributes(attributes) { super }
7
15
  end
@@ -14,9 +14,14 @@ module SimpleNestedSet
14
14
  end
15
15
 
16
16
  def perform
17
- unless bound == node.rgt || bound == node.lft # there would be no change
18
- reload
19
- nested_set.transaction { nested_set.update_all(query) }
17
+ node.run_callbacks(:move) do
18
+ # reload
19
+ unless bound == node.rgt || bound == node.lft # there would be no change
20
+ nested_set.transaction do
21
+ # node.save!
22
+ nested_set.update_all(query)
23
+ end
24
+ end
20
25
  reload
21
26
  end
22
27
  end
@@ -25,9 +25,12 @@ module SimpleNestedSet
25
25
  def with_move_by_attributes(attributes, node = nil)
26
26
  node_class.transaction do
27
27
  nested_set_attributes = extract_nested_set_attributes!(attributes)
28
- result = yield
29
- (node || result).nested_set.move_by_attributes(nested_set_attributes) unless nested_set_attributes.empty?
30
- result
28
+ yield.tap do |result|
29
+ unless nested_set_attributes.empty?
30
+ node ||= result
31
+ node.nested_set.move_by_attributes(nested_set_attributes)
32
+ end
33
+ end
31
34
  end
32
35
  end
33
36
 
@@ -75,6 +78,7 @@ module SimpleNestedSet
75
78
  max_right = maximum(:rgt) || 0
76
79
  node.lft, node.rgt = max_right + 1, max_right + 2
77
80
  end
81
+ true
78
82
  end
79
83
 
80
84
  # Prunes a branch off of the tree, shifting all of the elements on the right
@@ -1,3 +1,3 @@
1
1
  module SimpleNestedSet
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require 'active_record'
2
+
1
3
  module SimpleNestedSet
2
4
  autoload :ActMacro, 'simple_nested_set/act_macro'
3
5
  autoload :ClassMethods, 'simple_nested_set/class_methods'
@@ -8,6 +10,8 @@ module SimpleNestedSet
8
10
  autoload :ByAttributes, 'simple_nested_set/move/by_attributes'
9
11
  autoload :ToTarget, 'simple_nested_set/move/to_target'
10
12
  autoload :Protection, 'simple_nested_set/move/protection'
13
+ autoload :Impossible, 'simple_nested_set/move/protection'
14
+ autoload :Inconsistent, 'simple_nested_set/move/protection'
11
15
  end
12
16
  end
13
17
 
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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-12 00:00:00 +02:00
18
+ date: 2010-08-18 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency