Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding nullify_notifiable as option to dependent_notifications #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/activity_notification/models/concerns/notifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ def destroy_generated_notifications_with_dependency(dependent = :delete_all, tar
end
end

# Update the notifiable_type and notifiable_id from the target to nil
# This method is intended to be called before destroy this notifiable as dependent configuration.
# @api private
# @param [String] target_type Target type of generated notifications
def nullify_notifiable_on_notifications_with_dependency(target_type)
generated_notifications_as_notifiable_for(target_type).update_all(notifiable_id: nil, notifiable_type: nil)
end

# Removes generated notifications from notification group to new group owner.
# This method is intended to be called before destroy this notifiable as dependent configuration.
# @api private
Expand Down
10 changes: 7 additions & 3 deletions lib/activity_notification/roles/acts_as_notifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ module ActsAsNotifiable
# * :dependent_notifications
# * Dependency for notifications to delete generated notifications with this notifiable.
# This option is used to configure generated_notifications_as_notifiable association.
# You can use :delete_all, :destroy, :restrict_with_error, :restrict_with_exception, :update_group_and_delete_all or :update_group_and_destroy for this option.
# You can use :delete_all, :destroy, :restrict_with_error, :restrict_with_exception, :update_group_and_delete_all, :update_group_and_destroy or :nullify_notifiable for this option.
# When you use :update_group_and_delete_all or :update_group_and_destroy to this parameter, the oldest group member notification becomes a new group owner as `before_destroy` of this Notifiable.
# When you use :nullify_notifiable to this parameter, the notifications won't be destroyed but the notifiable_type and notifiable_id of those dependent notifications will be set to nil.
# This parameter is effective for all target and is a optional since no dependent option is used as default.
# @example Define :delete_all dependency to generated notifications
# # app/models/comment.rb
Expand Down Expand Up @@ -224,7 +225,7 @@ module ActsAsNotifiable
# @option options [Symbol, Proc, String] :notifiable_path (polymorphic_path(self)) Path to redirect from open or move action of notification controller
# @option options [Boolean, Hash] :tracked (nil) Flag or parameters for automatic tracked notifications
# @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notifiable name
# @option options [Symbol, Proc] :dependent_notifications (nil) Dependency for notifications to delete generated notifications with this notifiable, [:delete_all, :destroy, :restrict_with_error, :restrict_with_exception, :update_group_and_delete_all, :update_group_and_destroy] are available
# @option options [Symbol, Proc] :dependent_notifications (nil) Dependency for notifications to delete generated notifications with this notifiable, [:delete_all, :destroy, :restrict_with_error, :restrict_with_exception, :update_group_and_delete_all, :update_group_and_destroy, :nullify_notifiable] are available
# @option options [Hash<Class, Hash>] :optional_targets (nil) Optional target configurations with hash of `OptionalTarget` implementation class as key and initializing option parameter as value
# @return [Hash] Configured parameters as notifiable model
def acts_as_notifiable(target_type, options = {})
Expand Down Expand Up @@ -295,7 +296,8 @@ def available_dependent_notifications_options
:restrict_with_error,
:restrict_with_exception,
:update_group_and_delete_all,
:update_group_and_destroy
:update_group_and_destroy,
:nullify_notifiable
].freeze
end

Expand Down Expand Up @@ -358,6 +360,8 @@ def add_destroy_dependency(target_type, dependent_notifications_option)
before_destroy -> { destroy_generated_notifications_with_dependency(:delete_all, target_type, true) }
when :update_group_and_destroy
before_destroy -> { destroy_generated_notifications_with_dependency(:destroy, target_type, true) }
when :nullify_notifiable
before_destroy -> { nullify_notifiable_on_notifications_with_dependency(target_type) }
end
{ dependent_notifications: dependent_notifications_option }
end
Expand Down