awesome_nested_set: import git 2-1-stable branch revision 8eaab19868f326 (#6579)

https://github.com/collectiveidea/awesome_nested_set/commit/8eaab19868f326

git-svn-id: http://svn.redmine.org/redmine/trunk@13145 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA
2014-05-24 10:17:18 +00:00
parent e22c5c3f80
commit 9ea1ae48db
5 changed files with 28 additions and 8 deletions

View File

@@ -51,6 +51,10 @@ module CollectiveIdea #:nodoc:
connection.quote_column_name(order_column)
end
def quoted_order_column_full_name
"#{quoted_table_name}.#{quoted_order_column_name}"
end
def quoted_left_column_full_name
"#{quoted_table_name}.#{quoted_left_column_name}"
end

View File

@@ -72,7 +72,7 @@ module CollectiveIdea #:nodoc:
end
def nested_set_scope(options = {})
options = {:order => quoted_order_column_name}.merge(options)
options = {:order => quoted_order_column_full_name}.merge(options)
order(options.delete(:order)).scoped options
end

View File

@@ -19,6 +19,9 @@ module CollectiveIdea #:nodoc:
# update lefts and rights for remaining nodes
update_siblings_for_remaining_nodes
# Reload is needed because children may have updated their parent (self) during deletion.
reload
# Don't allow multiple calls to destroy to corrupt the set
self.skip_before_destroy = true
end

View File

@@ -17,7 +17,7 @@ module CollectiveIdea
def no_duplicates_for_columns?
[quoted_left_column_full_name, quoted_right_column_full_name].all? do |column|
# No duplicates
select("#{scope_string}#{column}, COUNT(#{column})").
select("#{scope_string}#{column}, COUNT(#{column}) as _count").
group("#{scope_string}#{column}").
having("COUNT(#{column}) > 1").
first.nil?

View File

@@ -24,8 +24,9 @@ module CollectiveIdea #:nodoc:
lock_nodes_between! a, d
nested_set_scope.where(where_statement(a, d)).
update_all(conditions(a, b, c, d))
nested_set_scope.where(where_statement(a, d)).update_all(
conditions(a, b, c, d)
)
end
private
@@ -44,11 +45,23 @@ module CollectiveIdea #:nodoc:
end
def conditions(a, b, c, d)
[
case_condition_for_direction(:quoted_left_column_name) +
_conditions = case_condition_for_direction(:quoted_left_column_name) +
case_condition_for_direction(:quoted_right_column_name) +
case_condition_for_parent,
{:a => a, :b => b, :c => c, :d => d, :id => instance.id, :new_parent => new_parent}
case_condition_for_parent
# We want the record to be 'touched' if it timestamps.
if @instance.respond_to?(:updated_at)
_conditions << ", updated_at = :timestamp"
end
[
_conditions,
{
:a => a, :b => b, :c => c, :d => d,
:id => instance.id,
:new_parent => new_parent,
:timestamp => Time.now.utc
}
]
end