Module: DataMapper::Is::Tree

Defined in:
lib/dm-is-tree/is/tree.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/dm-is-tree/is/tree.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#is_a_tree(options = {}) ⇒ Object



89
90
91
92
# File 'lib/dm-is-tree/is/tree.rb', line 89

def is_a_tree(options = {})
  warn('#is_a_tree is depreciated. use #is :tree instead.')
  is :tree, options
end

#is_tree(options = {}) ⇒ Object Also known as: can_has_tree

Configuration options are:

  • child_key - specifies the column name to use for tracking of the tree (default: parent_id)

  • constraint - allows a constraint option to be set on the ‘has n` association. Only valid when dm-constraints is used.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dm-is-tree/is/tree.rb', line 64

def is_tree(options = {})

  if options[:class_name]
    warn '+options[:class_name]+ is deprecated, use :model instead'
    options[:model] = options.delete(:class_name)
  end

  options = { :model => name, :child_key => :parent_id }.merge(options) if Hash === options
  @tree_options = options

  include DataMapper::Is::Tree::InstanceMethods
  extend  DataMapper::Is::Tree::ClassMethods

  assc_options = { :model => options[:model], :child_key => Array(options[:child_key]) }
  has_n_options = options[:order] ? { :order => Array(options[:order]) }.merge(assc_options) : assc_options
  has_n_options.merge!(:constraint => options[:constraint]) if options[:constraint]

  belongs_to :parent, assc_options.merge(:required => false)
  has n, :children, has_n_options

  class << self
    alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
  end
end