simple_nested_set 0.0.6 → 0.0.7

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.
@@ -8,9 +8,9 @@ module SimpleNestedSet
8
8
 
9
9
  define_callbacks :move, :terminator => "result == false"
10
10
 
11
- before_validation lambda { |r| r.nested_set.init_as_node }, :if => :new_record?
12
- before_destroy lambda { |r| r.nested_set.prune_branch }
13
- after_save lambda { |r| r.nested_set.save! }
11
+ before_create lambda { |r| r.nested_set.init_as_node }
12
+ before_destroy lambda { |r| r.nested_set.prune_branch }
13
+ after_save lambda { |r| r.nested_set.save! }
14
14
 
15
15
  belongs_to :parent, :class_name => self.name
16
16
  has_many :children, :foreign_key => :parent_id, :class_name => self.name
@@ -15,13 +15,11 @@ 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
18
+ # unless bound == node.rgt || bound == node.lft # there would be no change
19
19
  nested_set.transaction do
20
- # puts ActiveRecord::Base.send(:sanitize_sql_array, query)
21
20
  update_structure!
22
- update_denormalizations!
23
21
  end
24
- end
22
+ # end
25
23
  reload
26
24
  end
27
25
  end
@@ -45,34 +43,9 @@ module SimpleNestedSet
45
43
 
46
44
  a, b, c, d = boundaries
47
45
  sql = [sql, { :a => a, :b => b, :c => c, :d => d, :id => id, :parent_id => parent_id }]
48
- nested_set.update_all(sql)
49
- end
50
-
51
- def update_denormalizations!
52
- sql = []
53
- sql << denormalize_level_query if node.has_attribute?(:level)
54
- sql << denormalize_path_query if node.has_attribute?(:path)
55
- nested_set.update_all(sql.join(',')) unless sql.blank?
56
- end
57
46
 
58
- def denormalize_level_query
59
- <<-sql
60
- level = (
61
- SELECT count(id)
62
- FROM #{table_name} as l
63
- WHERE l.lft < #{table_name}.lft AND l.rgt > #{table_name}.rgt
64
- )
65
- sql
66
- end
67
-
68
- def denormalize_path_query
69
- <<-sql
70
- path = (
71
- SELECT GROUP_CONCAT(slug, '/')
72
- FROM #{table_name} as l
73
- WHERE l.lft <= #{table_name}.lft AND l.rgt >= #{table_name}.rgt
74
- )
75
- sql
47
+ # puts ActiveRecord::Base.send(:sanitize_sql_array, sql)
48
+ nested_set.update_all(sql)
76
49
  end
77
50
 
78
51
  def reload
@@ -33,6 +33,16 @@ module SimpleNestedSet
33
33
  attributes = node.instance_variable_get(:@_nested_set_attributes)
34
34
  node.instance_variable_set(:@_nested_set_attributes, nil)
35
35
  move_by_attributes(attributes) unless attributes.blank?
36
+ denormalize!
37
+ end
38
+
39
+ # FIXME we don't always want to call this on after_save, do we? it's only relevant when
40
+ # either the structure or the slug has changed
41
+ def denormalize!
42
+ sql = []
43
+ sql << denormalize_level_query if node.has_attribute?(:level)
44
+ sql << denormalize_path_query if node.has_attribute?(:path)
45
+ update_all(sql.join(',')) unless sql.blank?
36
46
  end
37
47
 
38
48
  # Returns true if the node has the same scope as the given node
@@ -84,5 +94,23 @@ module SimpleNestedSet
84
94
  def move_to(target, position)
85
95
  Move::ToTarget.new(node, target, position).perform
86
96
  end
97
+
98
+ def denormalize_level_query
99
+ query = arel_table.as(:l)
100
+ query = query.project('count(id)').
101
+ where(query[:lft].lt(arel_table[:lft])).
102
+ where(query[:rgt].gt(arel_table[:rgt])).
103
+ where(where_clauses.map { |clause| clause.gsub(table_name, 'l') })
104
+ "level = (#{query.to_sql})"
105
+ end
106
+
107
+ def denormalize_path_query
108
+ query = arel_table.as(:l)
109
+ query = query.project("GROUP_CONCAT(slug, '/')").
110
+ where(query[:lft].lteq(arel_table[:lft])).
111
+ where(query[:rgt].gteq(arel_table[:rgt])).
112
+ where(where_clauses.map { |clause| clause.gsub(table_name, 'l') })
113
+ "path = (#{query.to_sql})"
114
+ end
87
115
  end
88
116
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleNestedSet
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
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-18 00:00:00 +02:00
18
+ date: 2010-08-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency