Sha256: 49aeb992b18a9fa715dacea66cf915d04676229029d0c11e0a573fdf95e3c0a9

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 Bytes

Contents

module OrderedTree
  module ClassMethods
    extend ActiveSupport::Concern

    included do
      belongs_to :parent_node, :class_name => name, :foreign_key => ordered_tree_config[:foreign_key]
      has_many :child_nodes, :class_name => name, :foreign_key => ordered_tree_config[:foreign_key], :order => ordered_tree_config[:order]
      scope :roots, lambda { { :conditions => {ordered_tree_config[:foreign_key] => 0}, :order => ordered_tree_config[:order].to_s } }

      def foreign_key_column
        :"#{ordered_tree_config[:foreign_key]}"
      end

      def order_column
        :"#{ordered_tree_config[:order]}"
      end

      before_create :add_to_list
      before_update :check_list_changes
      after_update :reorder_old_list
      before_destroy :destroy_descendants
      after_destroy :reorder_old_list
      validate :check_parentage, :on => :update
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ordered_tree-0.1.1 lib/ordered_tree/class_methods.rb