Sory td ke send ngak sengaja. :D

Daya punya tabel user, department, role dan mengunakan acts_as_tree

tabel user :
belongs_to :department
has_and_belongs_to_many :roles
acts_as_list :scope => :department

def allowed_departments(sort = false)
    departments = []
    dept_orders = Hash.new(10)

    dept_parents = self.department.ancestors
    roles.each do |role|
      case role.title.downcase
        when 'to_child'
          self.department.children.each { |x| departments << x; dept_orders[
x.id] }
        when 'to_sibling'
          self.department.siblings.each { |x| departments << x; dept_orders[
x.id] }
        when 'to_parent'
          self.department.parents.each { |x| departments << x; dept_orders[
x.id] }
      end
    end
    if sort then
      departments.sort! { |a,b|
        dept_orders[ a.id ] <=> dept_orders[ b.id ]
      }
    end
    departments
  end

permasalahannya dalam acts_as_tree secara default hanya ada tiga method
(root, ancestor, siblings, child) untuk melihat data dalam tiap level sbb:

tree.rb (vendor/rails/activerecord/acts/tree.rb)
## cut ##

module InstanceMethods
        # Returns list of ancestors, starting from parent until root.
        #
        #   subchild1.ancestors # => [child1, root]
        def ancestors
          node, nodes = self, []
          nodes << node = node.parent while node.parent
          nodes
        end

        # Returns the root node of the tree.
        def root
          node = self
          node = node.parent while node.parent
          node
        end

        # Returns all siblings of the current node.
        #
        #   subchild1.siblings # => [subchild2]
        def siblings
          self_and_siblings - [self]
        end

        # Returns all siblings and a reference to the current node.
        #
        #   subchild1.self_and_siblings # => [subchild1, subchild2]
        def self_and_siblings
          parent ? parent.children : self.class.roots
        end
      end

## cut ##

Pertanyaanya :
1. dalam table tersebut banyak user dengan level department yang lumayan
banyak, bagai mana perintah untuk bisa melihat list user di semua department
dengan satu perintah karena dengan menggabungkan perintah InstanceMethods
pada acts_ass_tree tidak bisa melihat semua user ?



Regards

Wawan


[Non-text portions of this message have been removed]

Kirim email ke