Sha256: 2d5e5c490f2a87bb32e4db78c2da6ab3cab59b335d4a045abe67c0c7a4eef5e0
Contents?: true
Size: 1.78 KB
Versions: 1
Compression:
Stored size: 1.78 KB
Contents
require 'ordered_tree/class_methods' require 'ordered_tree/instance_methods' module OrderedTree #:nodoc: # Configuration: # # class Person < ActiveRecord::Base # ordered_tree :foreign_key => :parent_id, # :order => :position # end # # class CreatePeople < ActiveRecord::Migration # def self.up # create_table :people do |t| # t.column :parent_id ,:integer ,:null => false ,:default => 0 # t.column :position ,:integer # end # add_index(:people, :parent_id) # end # end def ordered_tree(options = {}) cattr_accessor :ordered_tree_config self.ordered_tree_config ||= {} self.ordered_tree_config[:foreign_key] ||= :parent_id self.ordered_tree_config[:order] ||= :position self.ordered_tree_config[:primary_key] ||= :id self.ordered_tree_config.update(options) if options.is_a?(Hash) belongs_to :parent_node, :class_name => self.name, :foreign_key => ordered_tree_config[:foreign_key], :primary_key => ordered_tree_config[:primary_key], :conditions => proc {scope_condition} has_many :child_nodes, :class_name => self.name, :foreign_key => ordered_tree_config[:foreign_key], :primary_key => ordered_tree_config[:primary_key], :conditions => proc {scope_condition}, :order => ordered_tree_config[:order] scope :roots, lambda { |*args| scope_condition = args[0] where(scope_condition).where(self.ordered_tree_config[:foreign_key].to_sym => 0).order(self.ordered_tree_config[:order]) } include OrderedTree::ClassMethods include OrderedTree::InstanceMethods end #ordered_tree end #module OrderedTree ActiveRecord::Base.extend OrderedTree
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ordered_tree-0.1.9 | lib/ordered_tree.rb |