Module: Edge::Forest::ActsAsForest
- Defined in:
- lib/edge/forest.rb
Overview
acts_as_forest models a tree/multi-tree structure.
Instance Method Summary collapse
-
#acts_as_forest(options = {}) ⇒ Object
options:.
Instance Method Details
#acts_as_forest(options = {}) ⇒ Object
options:
-
dependent - passed to children has_many (default: none)
-
foreign_key - column name to use for parent foreign_key (default: parent_id)
-
order - how to order children (default: none)
-
optional - passed to belongs_to (default: none)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/edge/forest.rb', line 11 def acts_as_forest(={}) .assert_valid_keys :foreign_key, :order, :dependent, :optional class_attribute :forest_foreign_key self.forest_foreign_key = [:foreign_key] || "parent_id" class_attribute :forest_order self.forest_order = [:order] || nil = { :class_name => self.name, :foreign_key => forest_foreign_key } = [:dependent] ? { dependent: [:dependent] } : {} = (ActiveRecord::VERSION::MAJOR >= 5 && [:optional]) ? { optional: [:optional] } : {} belongs_to :parent, .merge(inverse_of: :children).merge() if forest_order has_many :children, -> { order(forest_order) }, .merge(inverse_of: :parent).merge() else has_many :children, .merge(inverse_of: :parent).merge() end scope :root, -> { where(forest_foreign_key => nil) } include Edge::Forest::InstanceMethods extend Edge::Forest::ClassMethods end |