Module: ActsAsOrderedTree
- Defined in:
- lib/acts_as_ordered_tree.rb,
lib/acts_as_ordered_tree/version.rb,
lib/acts_as_ordered_tree/validators.rb,
lib/acts_as_ordered_tree/arrangeable.rb,
lib/acts_as_ordered_tree/class_methods.rb,
lib/acts_as_ordered_tree/relation/base.rb,
lib/acts_as_ordered_tree/instance_methods.rb,
lib/acts_as_ordered_tree/relation/preloaded.rb,
lib/acts_as_ordered_tree/tenacious_transaction.rb,
lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb
Defined Under Namespace
Modules: Adapters, Arrangeable, ClassMethods, Columns, InstanceMethods, Relation, TenaciousTransaction, Validators
Constant Summary collapse
- PROTECTED_ATTRIBUTES_SUPPORTED =
ActiveRecord::VERSION::STRING < '4.0.0' || defined?(ProtectedAttributes)
- PLAIN_ORDER_OPTION_SUPPORTED =
can we use has_many :children, :order => :position
ActiveRecord::VERSION::STRING < '4.0.0'
- VERSION =
'1.3.1'
Instance Method Summary collapse
-
#acts_as_ordered_tree(options = {}) ⇒ Object
Usage class Category < ActiveRecord::Base acts_as_ordered_tree :parent_column => :parent_id, :position_column => :position, :depth_column => :depth, :counter_cache => :children_count end.
Instance Method Details
#acts_as_ordered_tree(options = {}) ⇒ Object
Usage
class Category < ActiveRecord::Base
acts_as_ordered_tree :parent_column => :parent_id,
:position_column => :position,
:depth_column => :depth,
:counter_cache => :children_count
end
21 22 23 24 25 26 27 28 29 30 31 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 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 |
# File 'lib/acts_as_ordered_tree.rb', line 21 def acts_as_ordered_tree( = {}) = { :parent_column => :parent_id, :position_column => :position, :depth_column => :depth }.merge() class_attribute :acts_as_ordered_tree_options, :instance_writer => false self. = [:depth_column] = nil unless columns_hash.include?([:depth_column].to_s) extend Columns include Columns = { :class_name => "::#{base_class.name}", :foreign_key => [:parent_column], :inverse_of => (:parent unless [:polymorphic]), :dependent => :destroy } [:before_add, :after_add, :before_remove, :after_remove].each do |callback| [callback] = [callback] if .key?(callback) end if PLAIN_ORDER_OPTION_SUPPORTED [:order] = [:position_column] if scope_column_names.any? [:conditions] = proc do [scope_column_names.map { |c| "#{c} = ?" }.join(' AND '), scope_column_names.map { |c| self[c] }] end end has_many :children, else scope = ->(parent) { relation = order([:position_column]) if scope_column_names.any? relation = relation.where( Hash[scope_column_names.map { |c| [c, parent[c]]}] ) end relation } has_many :children, scope, end # create parent association belongs_to :parent, :class_name => "::#{base_class.name}", :foreign_key => [:parent_column], :counter_cache => [:counter_cache], :inverse_of => (:children unless [:polymorphic]) include ClassMethods include InstanceMethods setup_ordered_tree_adapter setup_ordered_tree_callbacks setup_ordered_tree_validations end |