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

Defined in:
lib/awesome_nested_set/depth.rb

Instance Method Summary collapse

Instance Method Details

#depth?Boolean

Check is model has depth column

Returns:

  • (Boolean)


18
19
20
# File 'lib/awesome_nested_set/depth.rb', line 18

def depth?
  self.respond_to?(:depth)
end

#scope_condition(table_name = nil) ⇒ Object

Model scope conditions



7
8
9
10
11
12
13
14
15
# File 'lib/awesome_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



28
29
30
31
32
33
34
35
36
37
# File 'lib/awesome_nested_set/depth.rb', line 28

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



23
24
25
# File 'lib/awesome_nested_set/depth.rb', line 23

def update_depth
  self.update_attribute(:depth, level)
end