Chris@1464: module CollectiveIdea #:nodoc: Chris@1464: module Acts #:nodoc: Chris@1464: module NestedSet #:nodoc: Chris@1464: Chris@1464: # This acts provides Nested Set functionality. Nested Set is a smart way to implement Chris@1464: # an _ordered_ tree, with the added feature that you can select the children and all of their Chris@1464: # descendants with a single query. The drawback is that insertion or move need some complex Chris@1464: # sql queries. But everything is done here by this module! Chris@1464: # Chris@1464: # Nested sets are appropriate each time you want either an orderd tree (menus, Chris@1464: # commercial categories) or an efficient way of querying big trees (threaded posts). Chris@1464: # Chris@1464: # == API Chris@1464: # Chris@1464: # Methods names are aligned with acts_as_tree as much as possible to make replacment from one Chris@1464: # by another easier. Chris@1464: # Chris@1464: # item.children.create(:name => "child1") Chris@1464: # Chris@1464: Chris@1464: # Configuration options are: Chris@1464: # Chris@1464: # * +:parent_column+ - specifies the column name to use for keeping the position integer (default: parent_id) Chris@1464: # * +:left_column+ - column name for left boundry data, default "lft" Chris@1464: # * +:right_column+ - column name for right boundry data, default "rgt" Chris@1464: # * +:scope+ - restricts what is to be considered a list. Given a symbol, it'll attach "_id" Chris@1464: # (if it hasn't been already) and use that as the foreign key restriction. You Chris@1464: # can also pass an array to scope by multiple attributes. Chris@1464: # Example: acts_as_nested_set :scope => [:notable_id, :notable_type] Chris@1464: # * +:dependent+ - behavior for cascading destroy. If set to :destroy, all the Chris@1464: # child objects are destroyed alongside this object by calling their destroy Chris@1464: # method. If set to :delete_all (default), all the child objects are deleted Chris@1464: # without calling their destroy method. Chris@1464: # * +:counter_cache+ adds a counter cache for the number of children. Chris@1464: # defaults to false. Chris@1464: # Example: acts_as_nested_set :counter_cache => :children_count Chris@1464: # Chris@1464: # See CollectiveIdea::Acts::NestedSet::Model::ClassMethods for a list of class methods and Chris@1464: # CollectiveIdea::Acts::NestedSet::Model for a list of instance methods added Chris@1464: # to acts_as_nested_set models Chris@1464: def acts_as_nested_set(options = {}) Chris@1464: options = { Chris@1464: :parent_column => 'parent_id', Chris@1464: :left_column => 'lft', Chris@1464: :right_column => 'rgt', Chris@1464: :dependent => :delete_all, # or :destroy Chris@1464: :counter_cache => false, Chris@1464: :order => 'id' Chris@1464: }.merge(options) Chris@1464: Chris@1464: if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/ Chris@1464: options[:scope] = "#{options[:scope]}_id".intern Chris@1464: end Chris@1464: Chris@1464: class_attribute :acts_as_nested_set_options Chris@1464: self.acts_as_nested_set_options = options Chris@1464: Chris@1464: include CollectiveIdea::Acts::NestedSet::Model Chris@1464: include Columns Chris@1464: extend Columns Chris@1464: Chris@1464: belongs_to :parent, :class_name => self.base_class.to_s, Chris@1464: :foreign_key => parent_column_name, Chris@1464: :counter_cache => options[:counter_cache], Chris@1464: :inverse_of => :children Chris@1464: has_many :children, :class_name => self.base_class.to_s, Chris@1464: :foreign_key => parent_column_name, :order => left_column_name, Chris@1464: :inverse_of => :parent, Chris@1464: :before_add => options[:before_add], Chris@1464: :after_add => options[:after_add], Chris@1464: :before_remove => options[:before_remove], Chris@1464: :after_remove => options[:after_remove] Chris@1464: Chris@1464: attr_accessor :skip_before_destroy Chris@1464: Chris@1464: before_create :set_default_left_and_right Chris@1464: before_save :store_new_parent Chris@1464: after_save :move_to_new_parent Chris@1464: before_destroy :destroy_descendants Chris@1464: Chris@1464: # no assignment to structure fields Chris@1464: [left_column_name, right_column_name].each do |column| Chris@1464: module_eval <<-"end_eval", __FILE__, __LINE__ Chris@1464: def #{column}=(x) Chris@1464: raise ActiveRecord::ActiveRecordError, "Unauthorized assignment to #{column}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead." Chris@1464: end Chris@1464: end_eval Chris@1464: end Chris@1464: Chris@1464: define_model_callbacks :move Chris@1464: end Chris@1464: Chris@1464: module Model Chris@1464: extend ActiveSupport::Concern Chris@1464: Chris@1464: module ClassMethods Chris@1464: # Returns the first root Chris@1464: def root Chris@1464: roots.first Chris@1464: end Chris@1464: Chris@1464: def roots Chris@1464: where(parent_column_name => nil).order(quoted_left_column_name) Chris@1464: end Chris@1464: Chris@1464: def leaves Chris@1464: where("#{quoted_right_column_name} - #{quoted_left_column_name} = 1").order(quoted_left_column_name) Chris@1464: end Chris@1464: Chris@1464: def valid? Chris@1464: left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid? Chris@1464: end Chris@1464: Chris@1464: def left_and_rights_valid? Chris@1464: joins("LEFT OUTER JOIN #{quoted_table_name} AS parent ON " + Chris@1464: "#{quoted_table_name}.#{quoted_parent_column_name} = parent.#{primary_key}"). Chris@1464: where( Chris@1464: "#{quoted_table_name}.#{quoted_left_column_name} IS NULL OR " + Chris@1464: "#{quoted_table_name}.#{quoted_right_column_name} IS NULL OR " + Chris@1464: "#{quoted_table_name}.#{quoted_left_column_name} >= " + Chris@1464: "#{quoted_table_name}.#{quoted_right_column_name} OR " + Chris@1464: "(#{quoted_table_name}.#{quoted_parent_column_name} IS NOT NULL AND " + Chris@1464: "(#{quoted_table_name}.#{quoted_left_column_name} <= parent.#{quoted_left_column_name} OR " + Chris@1464: "#{quoted_table_name}.#{quoted_right_column_name} >= parent.#{quoted_right_column_name}))" Chris@1464: ).count == 0 Chris@1464: end Chris@1464: Chris@1464: def no_duplicates_for_columns? Chris@1464: scope_string = Array(acts_as_nested_set_options[:scope]).map do |c| Chris@1464: connection.quote_column_name(c) Chris@1464: end.push(nil).join(", ") Chris@1464: [quoted_left_column_name, quoted_right_column_name].all? do |column| Chris@1464: # No duplicates Chris@1464: select("#{scope_string}#{column}, COUNT(#{column})"). Chris@1464: group("#{scope_string}#{column}"). Chris@1464: having("COUNT(#{column}) > 1"). Chris@1464: first.nil? Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Wrapper for each_root_valid? that can deal with scope. Chris@1464: def all_roots_valid? Chris@1464: if acts_as_nested_set_options[:scope] Chris@1464: roots.group(scope_column_names).group_by{|record| scope_column_names.collect{|col| record.send(col.to_sym)}}.all? do |scope, grouped_roots| Chris@1464: each_root_valid?(grouped_roots) Chris@1464: end Chris@1464: else Chris@1464: each_root_valid?(roots) Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def each_root_valid?(roots_to_validate) Chris@1464: left = right = 0 Chris@1464: roots_to_validate.all? do |root| Chris@1464: (root.left > left && root.right > right).tap do Chris@1464: left = root.left Chris@1464: right = root.right Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Rebuilds the left & rights if unset or invalid. Chris@1464: # Also very useful for converting from acts_as_tree. Chris@1464: def rebuild!(validate_nodes = true) Chris@1464: # Don't rebuild a valid tree. Chris@1464: return true if valid? Chris@1464: Chris@1464: scope = lambda{|node|} Chris@1464: if acts_as_nested_set_options[:scope] Chris@1464: scope = lambda{|node| Chris@1464: scope_column_names.inject(""){|str, column_name| Chris@1464: str << "AND #{connection.quote_column_name(column_name)} = #{connection.quote(node.send(column_name.to_sym))} " Chris@1464: } Chris@1464: } Chris@1464: end Chris@1464: indices = {} Chris@1464: Chris@1464: set_left_and_rights = lambda do |node| Chris@1464: # set left Chris@1464: node[left_column_name] = indices[scope.call(node)] += 1 Chris@1464: # find Chris@1464: where(["#{quoted_parent_column_name} = ? #{scope.call(node)}", node]).order(acts_as_nested_set_options[:order]).each{|n| set_left_and_rights.call(n) } Chris@1464: # set right Chris@1464: node[right_column_name] = indices[scope.call(node)] += 1 Chris@1464: node.save!(:validate => validate_nodes) Chris@1464: end Chris@1464: Chris@1464: # Find root node(s) Chris@1464: root_nodes = where("#{quoted_parent_column_name} IS NULL").order(acts_as_nested_set_options[:order]).each do |root_node| Chris@1464: # setup index for this scope Chris@1464: indices[scope.call(root_node)] ||= 0 Chris@1464: set_left_and_rights.call(root_node) Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Iterates over tree elements and determines the current level in the tree. Chris@1464: # Only accepts default ordering, odering by an other column than lft Chris@1464: # does not work. This method is much more efficent than calling level Chris@1464: # because it doesn't require any additional database queries. Chris@1464: # Chris@1464: # Example: Chris@1464: # Category.each_with_level(Category.root.self_and_descendants) do |o, level| Chris@1464: # Chris@1464: def each_with_level(objects) Chris@1464: path = [nil] Chris@1464: objects.each do |o| Chris@1464: if o.parent_id != path.last Chris@1464: # we are on a new level, did we decent or ascent? Chris@1464: if path.include?(o.parent_id) Chris@1464: # remove wrong wrong tailing paths elements Chris@1464: path.pop while path.last != o.parent_id Chris@1464: else Chris@1464: path << o.parent_id Chris@1464: end Chris@1464: end Chris@1464: yield(o, path.length - 1) Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Any instance method that returns a collection makes use of Rails 2.1's named_scope (which is bundled for Rails 2.0), so it can be treated as a finder. Chris@1464: # Chris@1464: # category.self_and_descendants.count Chris@1464: # category.ancestors.find(:all, :conditions => "name like '%foo%'") Chris@1464: Chris@1464: # Value of the parent column Chris@1464: def parent_id Chris@1464: self[parent_column_name] Chris@1464: end Chris@1464: Chris@1464: # Value of the left column Chris@1464: def left Chris@1464: self[left_column_name] Chris@1464: end Chris@1464: Chris@1464: # Value of the right column Chris@1464: def right Chris@1464: self[right_column_name] Chris@1464: end Chris@1464: Chris@1464: # Returns true if this is a root node. Chris@1464: def root? Chris@1464: parent_id.nil? Chris@1464: end Chris@1464: Chris@1464: def leaf? Chris@1464: new_record? || (right - left == 1) Chris@1464: end Chris@1464: Chris@1464: # Returns true is this is a child node Chris@1464: def child? Chris@1464: !parent_id.nil? Chris@1464: end Chris@1464: Chris@1464: # Returns root Chris@1464: def root Chris@1464: self_and_ancestors.where(parent_column_name => nil).first Chris@1464: end Chris@1464: Chris@1464: # Returns the array of all parents and self Chris@1464: def self_and_ancestors Chris@1464: nested_set_scope.where([ Chris@1464: "#{self.class.quoted_table_name}.#{quoted_left_column_name} <= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} >= ?", left, right Chris@1464: ]) Chris@1464: end Chris@1464: Chris@1464: # Returns an array of all parents Chris@1464: def ancestors Chris@1464: without_self self_and_ancestors Chris@1464: end Chris@1464: Chris@1464: # Returns the array of all children of the parent, including self Chris@1464: def self_and_siblings Chris@1464: nested_set_scope.where(parent_column_name => parent_id) Chris@1464: end Chris@1464: Chris@1464: # Returns the array of all children of the parent, except self Chris@1464: def siblings Chris@1464: without_self self_and_siblings Chris@1464: end Chris@1464: Chris@1464: # Returns a set of all of its nested children which do not have children Chris@1464: def leaves Chris@1464: descendants.where("#{self.class.quoted_table_name}.#{quoted_right_column_name} - #{self.class.quoted_table_name}.#{quoted_left_column_name} = 1") Chris@1464: end Chris@1464: Chris@1464: # Returns the level of this object in the tree Chris@1464: # root level is 0 Chris@1464: def level Chris@1464: parent_id.nil? ? 0 : ancestors.count Chris@1464: end Chris@1464: Chris@1464: # Returns a set of itself and all of its nested children Chris@1464: def self_and_descendants Chris@1464: nested_set_scope.where([ Chris@1464: "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} <= ?", left, right Chris@1464: ]) Chris@1464: end Chris@1464: Chris@1464: # Returns a set of all of its children and nested children Chris@1464: def descendants Chris@1464: without_self self_and_descendants Chris@1464: end Chris@1464: Chris@1464: def is_descendant_of?(other) Chris@1464: other.left < self.left && self.left < other.right && same_scope?(other) Chris@1464: end Chris@1464: Chris@1464: def is_or_is_descendant_of?(other) Chris@1464: other.left <= self.left && self.left < other.right && same_scope?(other) Chris@1464: end Chris@1464: Chris@1464: def is_ancestor_of?(other) Chris@1464: self.left < other.left && other.left < self.right && same_scope?(other) Chris@1464: end Chris@1464: Chris@1464: def is_or_is_ancestor_of?(other) Chris@1464: self.left <= other.left && other.left < self.right && same_scope?(other) Chris@1464: end Chris@1464: Chris@1464: # Check if other model is in the same scope Chris@1464: def same_scope?(other) Chris@1464: Array(acts_as_nested_set_options[:scope]).all? do |attr| Chris@1464: self.send(attr) == other.send(attr) Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Find the first sibling to the left Chris@1464: def left_sibling Chris@1464: siblings.where(["#{self.class.quoted_table_name}.#{quoted_left_column_name} < ?", left]). Chris@1464: order("#{self.class.quoted_table_name}.#{quoted_left_column_name} DESC").last Chris@1464: end Chris@1464: Chris@1464: # Find the first sibling to the right Chris@1464: def right_sibling Chris@1464: siblings.where(["#{self.class.quoted_table_name}.#{quoted_left_column_name} > ?", left]).first Chris@1464: end Chris@1464: Chris@1464: # Shorthand method for finding the left sibling and moving to the left of it. Chris@1464: def move_left Chris@1464: move_to_left_of left_sibling Chris@1464: end Chris@1464: Chris@1464: # Shorthand method for finding the right sibling and moving to the right of it. Chris@1464: def move_right Chris@1464: move_to_right_of right_sibling Chris@1464: end Chris@1464: Chris@1464: # Move the node to the left of another node (you can pass id only) Chris@1464: def move_to_left_of(node) Chris@1464: move_to node, :left Chris@1464: end Chris@1464: Chris@1464: # Move the node to the left of another node (you can pass id only) Chris@1464: def move_to_right_of(node) Chris@1464: move_to node, :right Chris@1464: end Chris@1464: Chris@1464: # Move the node to the child of another node (you can pass id only) Chris@1464: def move_to_child_of(node) Chris@1464: move_to node, :child Chris@1464: end Chris@1464: Chris@1464: # Move the node to root nodes Chris@1464: def move_to_root Chris@1464: move_to nil, :root Chris@1464: end Chris@1464: Chris@1464: def move_possible?(target) Chris@1464: self != target && # Can't target self Chris@1464: same_scope?(target) && # can't be in different scopes Chris@1464: # !(left..right).include?(target.left..target.right) # this needs tested more Chris@1464: # detect impossible move Chris@1464: !((left <= target.left && right >= target.left) or (left <= target.right && right >= target.right)) Chris@1464: end Chris@1464: Chris@1464: def to_text Chris@1464: self_and_descendants.map do |node| Chris@1464: "#{'*'*(node.level+1)} #{node.id} #{node.to_s} (#{node.parent_id}, #{node.left}, #{node.right})" Chris@1464: end.join("\n") Chris@1464: end Chris@1464: Chris@1464: protected Chris@1464: Chris@1464: def without_self(scope) Chris@1464: scope.where(["#{self.class.quoted_table_name}.#{self.class.primary_key} != ?", self]) Chris@1464: end Chris@1464: Chris@1464: # All nested set queries should use this nested_set_scope, which performs finds on Chris@1464: # the base ActiveRecord class, using the :scope declared in the acts_as_nested_set Chris@1464: # declaration. Chris@1464: def nested_set_scope(options = {}) Chris@1464: options = {:order => "#{self.class.quoted_table_name}.#{quoted_left_column_name}"}.merge(options) Chris@1464: scopes = Array(acts_as_nested_set_options[:scope]) Chris@1464: options[:conditions] = scopes.inject({}) do |conditions,attr| Chris@1464: conditions.merge attr => self[attr] Chris@1464: end unless scopes.empty? Chris@1464: self.class.base_class.scoped options Chris@1464: end Chris@1464: Chris@1464: def store_new_parent Chris@1464: @move_to_new_parent_id = send("#{parent_column_name}_changed?") ? parent_id : false Chris@1464: true # force callback to return true Chris@1464: end Chris@1464: Chris@1464: def move_to_new_parent Chris@1464: if @move_to_new_parent_id.nil? Chris@1464: move_to_root Chris@1464: elsif @move_to_new_parent_id Chris@1464: move_to_child_of(@move_to_new_parent_id) Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # on creation, set automatically lft and rgt to the end of the tree Chris@1464: def set_default_left_and_right Chris@1464: highest_right_row = nested_set_scope(:order => "#{quoted_right_column_name} desc").limit(1).lock(true).first Chris@1464: maxright = highest_right_row ? (highest_right_row[right_column_name] || 0) : 0 Chris@1464: # adds the new node to the right of all existing nodes Chris@1464: self[left_column_name] = maxright + 1 Chris@1464: self[right_column_name] = maxright + 2 Chris@1464: end Chris@1464: Chris@1464: def in_tenacious_transaction(&block) Chris@1464: retry_count = 0 Chris@1464: begin Chris@1464: transaction(&block) Chris@1464: rescue ActiveRecord::StatementInvalid => error Chris@1464: raise unless connection.open_transactions.zero? Chris@1464: raise unless error.message =~ /Deadlock found when trying to get lock|Lock wait timeout exceeded/ Chris@1464: raise unless retry_count < 10 Chris@1464: retry_count += 1 Chris@1464: logger.info "Deadlock detected on retry #{retry_count}, restarting transaction" Chris@1464: sleep(rand(retry_count)*0.1) # Aloha protocol Chris@1464: retry Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Prunes a branch off of the tree, shifting all of the elements on the right Chris@1464: # back to the left so the counts still work. Chris@1464: def destroy_descendants Chris@1464: return if right.nil? || left.nil? || skip_before_destroy Chris@1464: Chris@1464: in_tenacious_transaction do Chris@1464: reload_nested_set Chris@1464: # select the rows in the model that extend past the deletion point and apply a lock Chris@1464: self.class.base_class. Chris@1464: select("id"). Chris@1464: where("#{quoted_left_column_name} >= ?", left). Chris@1464: lock(true). Chris@1464: all Chris@1464: Chris@1464: if acts_as_nested_set_options[:dependent] == :destroy Chris@1464: descendants.each do |model| Chris@1464: model.skip_before_destroy = true Chris@1464: model.destroy Chris@1464: end Chris@1464: else Chris@1464: nested_set_scope.delete_all( Chris@1464: ["#{quoted_left_column_name} > ? AND #{quoted_right_column_name} < ?", Chris@1464: left, right] Chris@1464: ) Chris@1464: end Chris@1464: Chris@1464: # update lefts and rights for remaining nodes Chris@1464: diff = right - left + 1 Chris@1464: nested_set_scope.update_all( Chris@1464: ["#{quoted_left_column_name} = (#{quoted_left_column_name} - ?)", diff], Chris@1464: ["#{quoted_left_column_name} > ?", right] Chris@1464: ) Chris@1464: nested_set_scope.update_all( Chris@1464: ["#{quoted_right_column_name} = (#{quoted_right_column_name} - ?)", diff], Chris@1464: ["#{quoted_right_column_name} > ?", right] Chris@1464: ) Chris@1464: Chris@1464: reload Chris@1464: # Don't allow multiple calls to destroy to corrupt the set Chris@1464: self.skip_before_destroy = true Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # reload left, right, and parent Chris@1464: def reload_nested_set Chris@1464: reload( Chris@1464: :select => "#{quoted_left_column_name}, #{quoted_right_column_name}, #{quoted_parent_column_name}", Chris@1464: :lock => true Chris@1464: ) Chris@1464: end Chris@1464: Chris@1464: def move_to(target, position) Chris@1464: raise ActiveRecord::ActiveRecordError, "You cannot move a new node" if self.new_record? Chris@1464: run_callbacks :move do Chris@1464: in_tenacious_transaction do Chris@1464: if target.is_a? self.class.base_class Chris@1464: target.reload_nested_set Chris@1464: elsif position != :root Chris@1464: # load object if node is not an object Chris@1464: target = nested_set_scope.find(target) Chris@1464: end Chris@1464: self.reload_nested_set Chris@1464: Chris@1464: unless position == :root || move_possible?(target) Chris@1464: raise ActiveRecord::ActiveRecordError, "Impossible move, target node cannot be inside moved tree." Chris@1464: end Chris@1464: Chris@1464: bound = case position Chris@1464: when :child; target[right_column_name] Chris@1464: when :left; target[left_column_name] Chris@1464: when :right; target[right_column_name] + 1 Chris@1464: when :root; 1 Chris@1464: else raise ActiveRecord::ActiveRecordError, "Position should be :child, :left, :right or :root ('#{position}' received)." Chris@1464: end Chris@1464: Chris@1464: if bound > self[right_column_name] Chris@1464: bound = bound - 1 Chris@1464: other_bound = self[right_column_name] + 1 Chris@1464: else Chris@1464: other_bound = self[left_column_name] - 1 Chris@1464: end Chris@1464: Chris@1464: # there would be no change Chris@1464: return if bound == self[right_column_name] || bound == self[left_column_name] Chris@1464: Chris@1464: # we have defined the boundaries of two non-overlapping intervals, Chris@1464: # so sorting puts both the intervals and their boundaries in order Chris@1464: a, b, c, d = [self[left_column_name], self[right_column_name], bound, other_bound].sort Chris@1464: Chris@1464: # select the rows in the model between a and d, and apply a lock Chris@1464: self.class.base_class.select('id').lock(true).where( Chris@1464: ["#{quoted_left_column_name} >= :a and #{quoted_right_column_name} <= :d", {:a => a, :d => d}] Chris@1464: ) Chris@1464: Chris@1464: new_parent = case position Chris@1464: when :child; target.id Chris@1464: when :root; nil Chris@1464: else target[parent_column_name] Chris@1464: end Chris@1464: Chris@1464: self.nested_set_scope.update_all([ Chris@1464: "#{quoted_left_column_name} = CASE " + Chris@1464: "WHEN #{quoted_left_column_name} BETWEEN :a AND :b " + Chris@1464: "THEN #{quoted_left_column_name} + :d - :b " + Chris@1464: "WHEN #{quoted_left_column_name} BETWEEN :c AND :d " + Chris@1464: "THEN #{quoted_left_column_name} + :a - :c " + Chris@1464: "ELSE #{quoted_left_column_name} END, " + Chris@1464: "#{quoted_right_column_name} = CASE " + Chris@1464: "WHEN #{quoted_right_column_name} BETWEEN :a AND :b " + Chris@1464: "THEN #{quoted_right_column_name} + :d - :b " + Chris@1464: "WHEN #{quoted_right_column_name} BETWEEN :c AND :d " + Chris@1464: "THEN #{quoted_right_column_name} + :a - :c " + Chris@1464: "ELSE #{quoted_right_column_name} END, " + Chris@1464: "#{quoted_parent_column_name} = CASE " + Chris@1464: "WHEN #{self.class.base_class.primary_key} = :id THEN :new_parent " + Chris@1464: "ELSE #{quoted_parent_column_name} END", Chris@1464: {:a => a, :b => b, :c => c, :d => d, :id => self.id, :new_parent => new_parent} Chris@1464: ]) Chris@1464: end Chris@1464: target.reload_nested_set if target Chris@1464: self.reload_nested_set Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: end Chris@1464: Chris@1464: # Mixed into both classes and instances to provide easy access to the column names Chris@1464: module Columns Chris@1464: def left_column_name Chris@1464: acts_as_nested_set_options[:left_column] Chris@1464: end Chris@1464: Chris@1464: def right_column_name Chris@1464: acts_as_nested_set_options[:right_column] Chris@1464: end Chris@1464: Chris@1464: def parent_column_name Chris@1464: acts_as_nested_set_options[:parent_column] Chris@1464: end Chris@1464: Chris@1464: def scope_column_names Chris@1464: Array(acts_as_nested_set_options[:scope]) Chris@1464: end Chris@1464: Chris@1464: def quoted_left_column_name Chris@1464: connection.quote_column_name(left_column_name) Chris@1464: end Chris@1464: Chris@1464: def quoted_right_column_name Chris@1464: connection.quote_column_name(right_column_name) Chris@1464: end Chris@1464: Chris@1464: def quoted_parent_column_name Chris@1464: connection.quote_column_name(parent_column_name) Chris@1464: end Chris@1464: Chris@1464: def quoted_scope_column_names Chris@1464: scope_column_names.collect {|column_name| connection.quote_column_name(column_name) } Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: end Chris@1464: end Chris@1464: end