module ErpTechSvcs::Utils::DefaultNestedSetMethods

Public Class Methods

included(base) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 4
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

children_to_tree_hash(options={}) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 201
def children_to_tree_hash(options={})
  self.children.collect { |child| child.to_tree_hash(options) }
end
is_descendant_of?(type) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 228
def is_descendant_of?(type)
  type = self.class.iid(type) if (type.is_a? String)
  parent = self.parent

  if type.id == self.id
    result = true
  elsif parent.nil?
    result = false
  elsif parent.id == type.id
    result = true
  else
    result = parent.is_descendant_of? type
  end
  result
end
leaf() click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 180
def leaf
  children.size == 0
end
to_json_with_leaf(options = {}) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 184
def to_json_with_leaf(options = {})
  self.to_json_without_leaf(options.merge(:methods => :leaf))
end
to_label() click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 176
def to_label
  description
end
to_record_representation(root = self.class.root) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 218
def to_record_representation(root = self.class.root)
  # returns a string of category descriptions like
  # 'main_category > sub_category n > ... > this category instance'
  if root?
    description
  else
    crawl_up_from(self, root).split('///').compact.reverse.join(' > ')
  end
end
to_representation(level) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 205
def to_representation(level)
  # returns a string that consists of 1) a number of dashes equal to
  # the category's level and 2) the category's description attr
  rep = ''

  if level > 0
    level.times { rep << '-' }
    rep += ' '
  end

  rep << description
end
to_tree_hash(options={}) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 190
def to_tree_hash(options={})
  options = {
    only: [:parent_id, :internal_identifier],
    leaf: self.leaf?,
    text: self.to_label,
    children: self.children.collect { |child| child.to_tree_hash(options) }
  }.merge(options)

  self.to_hash(options)
end

Private Instance Methods

crawl_up_from(node, to_node = self.class.root) click to toggle source
# File lib/erp_tech_svcs/utils/default_nested_set_methods.rb, line 246
def crawl_up_from(node, to_node = self.class.root)
  unless node.nil?
    # returns a string that is a '///'-separated list of nodes
    # from child node to root
    "#{node.description}///#{crawl_up_from(node.parent, to_node) if node != to_node}"
  end
end