Module: Nestable

Defined in:
lib/nestable.rb,
lib/nestable/version.rb,
lib/nestable/strategy.rb,
lib/nestable/interface.rb,
lib/nestable/strategy/path.rb,
lib/nestable/strategy/tree.rb

Overview

Adds nesting functionality to ActiveRecord objects with various strategies like: tree, set, path, etc.

Defined Under Namespace

Modules: Interface, Strategy, Version

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:


9
10
11
12
13
14
# File 'lib/nestable.rb', line 9

def self.extended(base) # :nodoc:
  base.class_eval do
    cattr_accessor :nestable_options
    self.nestable_options = { :strategy => :tree }
  end
end

Instance Method Details

#nestable(options = {}) ⇒ Object

Adds nesting functionality to the current class.

Accepts an optional argument :strategy along with any other options related with that strategy. Take a look at each strategy's documentation for a list of its related options.

Example:

class Category < ActiveRecord::Base
  nestable :strategy => :path, :scope => :store_id
end

The default :strategy is :tree


28
29
30
31
32
33
34
35
36
# File 'lib/nestable.rb', line 28

def nestable(options = {})
  nestable_options.merge!(options)
  nestable_options[:strategy] = case nestable_options[:strategy]
    when String then nestable_options[:strategy].constantize
    when Symbol then Strategy.const_get(nestable_options[:strategy].to_s.classify)
    else nestable_options[:strategy]
  end
  implement Interface and include nestable_options[:strategy]
end