Module: Forestify

Defined in:
lib/forestify.rb

Overview

Forestify

Provides a tree structure to Active Record models.

New leaves are added to the right.

For example, a Tag model could implement it like this :

class Tag < ActiveRecord::Base
  forestify
end

We’ll use the following example throughout this documentation :

@vehicle = Tag.create!(name: "Vehicle")
@animal = Tag.create!(name: "Animal")
@car = Tag.create!(name: "Car", parent_id: @vehicle.id)
@plane = Tag.create!(name: "plane", parent_id: @vehicle.id)
@boat = Tag.create!(name: "Boat", parent_id: @vehicle.id)
@audi = Tag.create!(name: "Audi", parent_id: @car.id)

This code produces the following tree :

{ forestify_left_position, name, forestify_right_position, forestify_level }
{ 0, Vehicle, 9, 0 } { 10, Animal, 11, 0}
{ 1, Car, 4, 1 } { 5, Plane, 6, 1 } { 7, Boat, 8, 1 }
{ 2, Audi, 3, 2}

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#forestifyObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/forestify.rb', line 31

def forestify
  unless included_modules.include? InstanceMethods
    include InstanceMethods
  end

  before_create :initialize_position
  before_destroy :update_positions_after_delete

  private :initialize_position, :update_positions_after_delete

  attr_accessor :parent_id
end