lib/activity_notification/models/concerns/notifiable.rb in activity_notification-1.2.1 vs lib/activity_notification/models/concerns/notifiable.rb in activity_notification-1.3.0

- old
+ new

@@ -5,18 +5,19 @@ # include PolymorphicHelpers to resolve string extentions include ActivityNotification::PolymorphicHelpers included do include Common + include Association include ActionDispatch::Routing::PolymorphicRoutes include Rails.application.routes.url_helpers # Has many notification instances for this notifiable. # Dependency for these notifications can be overriden from acts_as_notifiable. # @scope instance - # @return [Array<Notificaion>] Array or database query of notifications for this notifiable - has_many :generated_notifications_as_notifiable, + # @return [Array<Notificaion>, Mongoid::Criteria<Notificaion>] Array or database query of notifications for this notifiable + has_many_records :generated_notifications_as_notifiable, class_name: "::ActivityNotification::Notification", as: :notifiable class_attribute :_notification_targets, :_notification_group, @@ -205,12 +206,18 @@ # @return [Array<Symbol>] Array of optional target names def optional_target_names(target_type, key = nil) optional_targets(target_type, key).map { |optional_target| optional_target.to_optional_target_name } end + # overriding_notification_template_key is the method to override key definition for Renderable + # When respond_to?(:overriding_notification_template_key) returns true, + # Renderable uses overriding_notification_template_key instead of original key. + # + # overriding_notification_template_key(target, key) + # overriding_notification_email_key is the method to override key definition for Mailer - # When respond_to?(overriding_notification_email_key) returns true, + # When respond_to?(:overriding_notification_email_key) returns true, # Mailer uses overriding_notification_email_key instead of original key. # # overriding_notification_email_key(target, key) @@ -301,14 +308,57 @@ else default_value end end - # Remove generated notifications from notification group to new group owner. + # Gets generated notifications for specified target type. + # @api private + # @param [String] target_type Target type of generated notifications + def generated_notifications_as_notifiable_for(target_type = nil) + target_type.nil? ? + generated_notifications_as_notifiable.all : + generated_notifications_as_notifiable.filtered_by_target_type(target_type.to_s.to_model_name) + end + + # Destroies generated notifications for specified target type with dependency. # This method is intended to be called before destroy this notifiable as dependent configuration. # @api private - def remove_generated_notifications_from_group - generated_notifications_as_notifiable.group_owners_only.each { |n| n.remove_from_group } + # @param [Symbol] dependent Has_many dependency, [:delete_all, :destroy, :restrict_with_error, :restrict_with_exception] are available + # @param [String] target_type Target type of generated notifications + # @param [Boolean] remove_from_group Whether it removes generated notifications from notification group before destroy + def destroy_generated_notifications_with_dependency(dependent = :delete_all, target_type = nil, remove_from_group = false) + remove_generated_notifications_from_group(target_type) if remove_from_group + generated_notifications = generated_notifications_as_notifiable_for(target_type) + case dependent + when :restrict_with_exception + raise ActiveRecord::DeleteRestrictionError.new("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}") unless generated_notifications.empty? + when :restrict_with_error + unless generated_notifications.empty? + record = self.class.human_attribute_name("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}").downcase + self.errors.add(:base, :'restrict_dependent_destroy.has_many', record: record) + # :skip-rails4: + if Rails::VERSION::MAJOR >= 5 + throw(:abort) + # :skip-rails4: + # :skip-rails5: + else + false + end + # :skip-rails5: + end + when :destroy + generated_notifications.each { |n| n.destroy } + when :delete_all + generated_notifications.delete_all + end + 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 + # @param [String] target_type Target type of generated notifications + def remove_generated_notifications_from_group(target_type = nil) + generated_notifications_as_notifiable_for(target_type).group_owners_only.each { |n| n.remove_from_group } end # Casts to resources name. # @api private def cast_to_resources_name(target_type) \ No newline at end of file