-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
268 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
source 'https://rubygems.org' | ||
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | ||
|
||
# Specify your gem's dependencies in delete_recursively.gemspec | ||
gemspec | ||
|
||
group :development, :test do | ||
# until fix for https://github.com/thoughtbot/appraisal/issues/199 is released | ||
gem 'appraisal', github: "thoughtbot/appraisal" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# override ::valid_dependent_options to make the new | ||
# dependent option available to Association::Builder classes | ||
module DeleteRecursively::OptionPermission | ||
def valid_dependent_options | ||
super + [DeleteRecursively::NEW_DEPENDENT_OPTION] | ||
end | ||
end | ||
|
||
# override Association#handle_dependency to apply the new option if it is set | ||
module DeleteRecursively::DependencyHandling | ||
def handle_dependency | ||
if DeleteRecursively.enabled_for?(self) | ||
delete_dependencies_recursively | ||
else | ||
super | ||
end | ||
end | ||
|
||
def delete_dependencies_recursively(force: false) | ||
if reflection.belongs_to? | ||
# Special case. The owner is already destroyed at this point, | ||
# so we cannot use the efficient ::dependent_ids lookup. Note that this | ||
# only happens for a single entry-record on #destroy, though. | ||
return unless target = load_target | ||
|
||
DeleteRecursively.delete_records_recursively(target.class, target.id, force: force) | ||
else | ||
DeleteRecursively.delete_recursively(reflection, nil, owner.id, force: force) | ||
end | ||
end | ||
end | ||
|
||
require 'active_record' | ||
|
||
module ActiveRecord | ||
module Associations | ||
%w[BelongsTo HasMany HasOne].each do |assoc_name| | ||
assoc_builder = Builder.const_get(assoc_name) | ||
assoc_builder.singleton_class.prepend(DeleteRecursively::OptionPermission) | ||
|
||
assoc_class = const_get("#{assoc_name}Association") | ||
assoc_class.prepend(DeleteRecursively::DependencyHandling) | ||
end | ||
end | ||
|
||
class Base | ||
def delete_recursively(force: false) | ||
DeleteRecursively.delete_records_recursively(self.class, id, force: force) | ||
end | ||
end | ||
|
||
class Relation | ||
def delete_all_recursively(force: false) | ||
DeleteRecursively.delete_records_recursively(klass, ids, force: force) | ||
end | ||
end | ||
end |
Oops, something went wrong.