Module: TinySupport::ActiveRecord::NestedSet::ClassMethods
- Defined in:
- lib/tiny_support/active_record/nested_set.rb
Overview
Configuration options are:
-
:parent_column- specifies the column name to use for keeping the position integer (default: parent_id) -
:left_column- column name for left boundry data, default “lft” -
:right_column- column name for right boundry data, default “rgt” -
:depth_column- column name for the depth data, default “depth” -
:scope- restricts what is to be considered a list. Given a symbol, it’ll attach “_id” (if it hasn’t been already) and use that as the foreign key restriction. You can also pass an array to scope by multiple attributes. Example:acts_as_nested_set :scope => [:notable_id, :notable_type] -
:dependent- behavior for cascading destroy. If set to :destroy, all the child objects are destroyed alongside this object by calling their destroy method. If set to :delete_all (default), all the child objects are deleted without calling their destroy method. -
:counter_cacheadds a counter cache for the number of children. defaults to false. Example:acts_as_nested_set :counter_cache => :children_count -
:order_columnon which column to do sorting, by default it is the left_column_name Example:acts_as_nested_set :order_column => :position
See TinySupport::ActiveRecord::NestedSet::Model::ClassMethods for a list of class methods and TinySupport::ActiveRecord::NestedSet::Model for a list of instance methods added to acts_as_nested_set models
Instance Method Summary collapse
Instance Method Details
#acts_as_nested_set(options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tiny_support/active_record/nested_set.rb', line 47 def acts_as_nested_set( = {}) = { :parent_column => 'parent_id', :left_column => 'lft', :right_column => 'rgt', :depth_column => 'depth', :dependent => :delete_all, # or :destroy :polymorphic => false, :counter_cache => false }.merge() if [:scope].is_a?(Symbol) && [:scope].to_s !~ /_id$/ [:scope] = "#{options[:scope]}_id".intern end class_attribute :acts_as_nested_set_options self. = include TinySupport::ActiveRecord::NestedSet::Model include Columns extend Columns belongs_to :parent, :class_name => self.base_class.to_s, :foreign_key => parent_column_name, :counter_cache => [:counter_cache], :inverse_of => (:children unless [:polymorphic]), :polymorphic => [:polymorphic] = { :class_name => self.base_class.to_s, :foreign_key => parent_column_name, :inverse_of => (:parent unless [:polymorphic]), } # Add callbacks, if they were supplied.. otherwise, we don't want them. [:before_add, :after_add, :before_remove, :after_remove].each do |ar_callback| .update(ar_callback => [ar_callback]) if [ar_callback] end has_many :children, , -> { order(order_column) } attr_accessor :skip_before_destroy before_create :set_default_left_and_right before_save :store_new_parent after_save :move_to_new_parent, :set_depth! before_destroy :destroy_descendants # no assignment to structure fields [left_column_name, right_column_name, depth_column_name].each do |column| module_eval " def \#{column}=(x)\n raise ActiveRecord::ActiveRecordError, \"Unauthorized assignment to \#{column}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead.\"\n end\n end_eval\n end\n\n define_model_callbacks :move\nend\n", __FILE__, __LINE__ |