git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10930 e93f8b46-1217-0410-a6f0-8f06a7374b81tags/2.3.0
# Return the next higher item in the list. | # Return the next higher item in the list. | ||||
def higher_item | def higher_item | ||||
return nil unless in_list? | return nil unless in_list? | ||||
acts_as_list_class.find(:first, :conditions => | |||||
acts_as_list_class.where( | |||||
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" | "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" | ||||
) | |||||
).first | |||||
end | end | ||||
# Return the next lower item in the list. | # Return the next lower item in the list. | ||||
def lower_item | def lower_item | ||||
return nil unless in_list? | return nil unless in_list? | ||||
acts_as_list_class.find(:first, :conditions => | |||||
acts_as_list_class.where( | |||||
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" | "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" | ||||
) | |||||
).first | |||||
end | end | ||||
# Test if this record is in a list | # Test if this record is in a list |
has_many :versions, version_association_options do | has_many :versions, version_association_options do | ||||
# finds earliest version of this record | # finds earliest version of this record | ||||
def earliest | def earliest | ||||
@earliest ||= find(:first, :order => 'version') | |||||
@earliest ||= order('version').first | |||||
end | end | ||||
# find latest version of this record | # find latest version of this record | ||||
def latest | def latest | ||||
@latest ||= find(:first, :order => 'version desc') | |||||
@latest ||= order('version desc').first | |||||
end | end | ||||
end | end | ||||
before_save :set_new_version | before_save :set_new_version |
# on creation, set automatically lft and rgt to the end of the tree | # on creation, set automatically lft and rgt to the end of the tree | ||||
def set_default_left_and_right | def set_default_left_and_right | ||||
highest_right_row = nested_set_scope(:order => "#{quoted_right_column_name} desc").find(:first, :limit => 1,:lock => true ) | |||||
highest_right_row = nested_set_scope(:order => "#{quoted_right_column_name} desc").limit(1).lock(true).first | |||||
maxright = highest_right_row ? (highest_right_row[right_column_name] || 0) : 0 | maxright = highest_right_row ? (highest_right_row[right_column_name] || 0) : 0 | ||||
# adds the new node to the right of all existing nodes | # adds the new node to the right of all existing nodes | ||||
self[left_column_name] = maxright + 1 | self[left_column_name] = maxright + 1 | ||||
in_tenacious_transaction do | in_tenacious_transaction do | ||||
reload_nested_set | reload_nested_set | ||||
# select the rows in the model that extend past the deletion point and apply a lock | # select the rows in the model that extend past the deletion point and apply a lock | ||||
self.class.base_class.find(:all, | |||||
:select => "id", | |||||
:conditions => ["#{quoted_left_column_name} >= ?", left], | |||||
:lock => true | |||||
) | |||||
self.class.base_class. | |||||
select("id"). | |||||
where("#{quoted_left_column_name} >= ?", left). | |||||
lock(true). | |||||
all | |||||
if acts_as_nested_set_options[:dependent] == :destroy | if acts_as_nested_set_options[:dependent] == :destroy | ||||
descendants.each do |model| | descendants.each do |model| |