Module: CollectiveIdea::Acts::NestedSet::Depth

Defined in:
lib/nested_set/depth.rb

Instance Method Summary collapse

Instance Method Details

#depthObject



17
18
19
# File 'lib/nested_set/depth.rb', line 17

def depth
  super.to_i if depth?
end

#scope_condition(table_name = nil) ⇒ Object

Model scope conditions



7
8
9
10
11
12
13
14
15
# File 'lib/nested_set/depth.rb', line 7

def scope_condition(table_name=nil)
  table_name ||= self.class.quoted_table_name

  scope_string = Array(acts_as_nested_set_options[:scope]).map do |c|
    "#{table_name}.#{connection.quote_column_name(c)} = #{self.send(c)}"
  end.join(" AND ")

  scope_string.blank? ? "1 = 1" : scope_string
end

#update_all_depthObject

Update cached_level attribute for all record tree



32
33
34
35
36
37
38
39
40
41
# File 'lib/nested_set/depth.rb', line 32

def update_all_depth
  if depth?
    self.class.connection.execute("UPDATE #{self.class.quoted_table_name} a SET a.depth = \
        (SELECT count(*) - 1 FROM (SELECT * FROM #{self.class.quoted_table_name} WHERE #{scope_condition}) AS b \
      	WHERE #{scope_condition('a')} AND \
      	(a.#{quoted_left_column_name} BETWEEN b.#{quoted_left_column_name} AND b.#{quoted_right_column_name}))
      	WHERE #{scope_condition('a')}
      ")
  end
end

#update_depthObject

Update cached_level attribute



22
23
24
25
26
27
28
29
# File 'lib/nested_set/depth.rb', line 22

def update_depth
  self.depth = level
  if depth_changed?
    self.self_and_descendants.
      update_all(["#{self.class.quoted_depth_column_name} = COALESCE(#{self.class.quoted_depth_column_name}, 0) + ?",
        depth_change[1] - depth_change[0].to_i])
  end
end