Method: Sequel::Plugins::Tree.apply

Defined in:
lib/sequel/plugins/tree.rb

.apply(model, opts = OPTS) ⇒ Object

Create parent and children associations. Any options specified are passed to both associations. You can also specify options to use for just the parent association using a :parent option, and options to use for just the children association using a :children option.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sequel/plugins/tree.rb', line 32

def self.apply(model, opts=OPTS)
  opts = opts.dup
  opts[:class] = model
  opts[:key] ||= :parent_id

  par = opts.merge(opts.fetch(:parent, OPTS))
  parent = par.fetch(:name, :parent)
  
  chi = opts.merge(opts.fetch(:children, OPTS))
  children = chi.fetch(:name, :children)

  par[:reciprocal] = children
  chi[:reciprocal] = parent

  model.instance_exec do
    @parent_column = opts[:key]
    @qualified_parent_column = Sequel.deep_qualify(table_name, opts[:key])
    @tree_order = opts[:order]
    @parent_association_name = parent
    @children_association_name = children

    many_to_one parent, par
    one_to_many children, chi
    plugin SingleRoot if opts[:single_root]
  end
end