Post#expunge!: simplify has_children flag logic.
This commit is contained in:
@@ -1283,17 +1283,8 @@ class Post < ApplicationRecord
|
|||||||
# - Move favorites to the first child.
|
# - Move favorites to the first child.
|
||||||
# - Reparent all children to the first child.
|
# - Reparent all children to the first child.
|
||||||
|
|
||||||
module ClassMethods
|
def update_has_children_flag
|
||||||
def update_has_children_flag_for(post_id)
|
update({has_children: children.exists?, has_active_children: children.undeleted.exists?}, without_protection: true)
|
||||||
return if post_id.nil?
|
|
||||||
has_children = Post.where("parent_id = ?", post_id).exists?
|
|
||||||
has_active_children = Post.where("parent_id = ? and is_deleted = ?", post_id, false).exists?
|
|
||||||
execute_sql("UPDATE posts SET has_children = ?, has_active_children = ? WHERE id = ?", has_children, has_active_children, post_id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.included(m)
|
|
||||||
m.extend(ClassMethods)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def blank_out_nonexistent_parents
|
def blank_out_nonexistent_parents
|
||||||
@@ -1310,7 +1301,7 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_parent_on_destroy
|
def update_parent_on_destroy
|
||||||
Post.update_has_children_flag_for(parent_id) if parent_id
|
parent.update_has_children_flag if parent
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_children_on_destroy
|
def update_children_on_destroy
|
||||||
@@ -1325,14 +1316,10 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_parent_on_save
|
def update_parent_on_save
|
||||||
if parent_id == parent_id_was
|
return unless parent_id_changed? || is_deleted_changed?
|
||||||
Post.update_has_children_flag_for(parent_id)
|
|
||||||
elsif !parent_id_was.nil?
|
parent.update_has_children_flag if parent.present?
|
||||||
Post.update_has_children_flag_for(parent_id)
|
Post.find(parent_id_was).update_has_children_flag if parent_id_was.present?
|
||||||
Post.update_has_children_flag_for(parent_id_was)
|
|
||||||
else
|
|
||||||
Post.update_has_children_flag_for(parent_id)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def give_favorites_to_parent
|
def give_favorites_to_parent
|
||||||
|
|||||||
Reference in New Issue
Block a user