nested_set 1.6.3 → 1.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -36,6 +36,8 @@ To make use of nested_set, your model needs to have 3 fields: lft, rgt, and pare
36
36
  end
37
37
  end
38
38
 
39
+ ###NB: There is no reason to use depth column. It's only add additional queries to DB without benefit. If you need level you should use `each_with_level` instead.
40
+
39
41
  Enable the nested set functionality by declaring acts_as_nested_set on your model
40
42
 
41
43
  class Category < ActiveRecord::Base
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.3
1
+ 1.6.4
@@ -99,7 +99,7 @@ module CollectiveIdea #:nodoc:
99
99
  }
100
100
  scope :leaves, lambda {
101
101
  where("#{quoted_right_column_name} - #{quoted_left_column_name} = 1").
102
- order(quoted_left_column_name)
102
+ order(quoted_left_column_name)
103
103
  }
104
104
  scope :with_depth, proc {|level| where(:depth => level).order(quoted_left_column_name) }
105
105
 
@@ -208,34 +208,24 @@ module CollectiveIdea #:nodoc:
208
208
  # Don't rebuild a valid tree.
209
209
  return true if valid?
210
210
 
211
- scope = lambda{|node|}
212
- if acts_as_nested_set_options[:scope]
213
- scope = lambda{|node|
214
- scope_column_names.inject(""){|str, column_name|
215
- str << "AND #{connection.quote_column_name(column_name)} = #{connection.quote(node.send(column_name.to_sym))} "
216
- }
217
- }
218
- end
219
211
  indices = {}
220
212
 
221
213
  set_left_and_rights = lambda do |node|
214
+ node_scope = scope_for_rebuild(node)
222
215
  # set left
223
- node[left_column_name] = indices[scope.call(node)] += 1
216
+ node[left_column_name] = indices[node_scope] += 1
224
217
  # find
225
- where("#{quoted_parent_column_name} = ? #{scope.call(node)}", node).
226
- order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").
227
- all.each{|n| set_left_and_rights.call(n) }
218
+ nodes_for_rebuild(node, node_scope).each{ |n| set_left_and_rights.call(n) }
228
219
  # set right
229
- node[right_column_name] = indices[scope.call(node)] += 1
220
+ node[right_column_name] = indices[node_scope] += 1
230
221
  node.save!
231
222
  end
232
223
 
233
224
  # Find root node(s)
234
- root_nodes = where(parent_column_name => nil).
235
- order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").
236
- all.each do |root_node|
225
+ root_nodes_for_rebuild.each do |root_node|
226
+ node_scope = scope_for_rebuild(root_node)
237
227
  # setup index for this scope
238
- indices[scope.call(root_node)] ||= 0
228
+ indices[node_scope] ||= 0
239
229
  set_left_and_rights.call(root_node)
240
230
  end
241
231
  end
@@ -269,6 +259,33 @@ module CollectiveIdea #:nodoc:
269
259
  def after_move(*args, &block)
270
260
  set_callback :move, :after, *args, &block
271
261
  end
262
+
263
+ private
264
+
265
+ def scope_for_rebuild(node)
266
+ scope_column_names.inject({}) do |hash, column_name|
267
+ hash[column_name] = node.send(column_name.to_sym)
268
+ hash
269
+ end
270
+ end
271
+
272
+ def nodes_for_rebuild(node, node_scope)
273
+ where(parent_column_name => node).
274
+ where(node_scope).
275
+ order(order_for_rebuild).
276
+ all
277
+ end
278
+
279
+ def root_nodes_for_rebuild
280
+ where(parent_column_name => nil).
281
+ order(order_for_rebuild).
282
+ all
283
+ end
284
+
285
+ def order_for_rebuild
286
+ "#{quoted_left_column_name}, #{quoted_right_column_name}, id"
287
+ end
288
+
272
289
  end
273
290
 
274
291
  # Mixed into both classes and instances to provide easy access to the column names
@@ -21,10 +21,11 @@ module CollectiveIdea #:nodoc:
21
21
 
22
22
  # Update cached_level attribute
23
23
  def update_depth
24
- depth_delta = level - depth
25
- if depth_delta != 0
26
- self.depth += depth_delta
27
- self.self_and_descendants.update_all(["#{self.class.quoted_depth_column_name} = #{self.class.quoted_depth_column_name} + ?", depth_delta])
24
+ self.depth = level
25
+ if depth_changed?
26
+ self.self_and_descendants.
27
+ update_all(["#{self.class.quoted_depth_column_name} = #{self.class.quoted_depth_column_name} + ?",
28
+ depth_change[1] - depth_change[0].to_i])
28
29
  end
29
30
  end
30
31
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nested_set}
8
- s.version = "1.6.3"
8
+ s.version = "1.6.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brandon Keepers", "Daniel Morrison"]
12
- s.date = %q{2011-01-02}
12
+ s.date = %q{2011-02-10}
13
13
  s.description = %q{An awesome nested set implementation for Active Record}
14
14
  s.email = %q{info@collectiveidea.com}
15
15
  s.extra_rdoc_files = [
@@ -6,7 +6,7 @@ ActiveRecord::Schema.define(:version => 0) do
6
6
  t.column :lft, :integer
7
7
  t.column :rgt, :integer
8
8
  t.column :organization_id, :integer
9
- t.column :depth, :integer, :default => 0
9
+ t.column :depth, :integer
10
10
  end
11
11
 
12
12
  create_table :departments, :force => true do |t|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 6
8
- - 3
9
- version: 1.6.3
8
+ - 4
9
+ version: 1.6.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brandon Keepers
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-02 00:00:00 +03:00
18
+ date: 2011-02-10 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
153
  requirements:
154
154
  - - ">="
155
155
  - !ruby/object:Gem::Version
156
- hash: 768447921
156
+ hash: 462466689
157
157
  segments:
158
158
  - 0
159
159
  version: "0"