lib/activity_notification/models/concerns/notifiable.rb in activity_notification-1.7.1 vs lib/activity_notification/models/concerns/notifiable.rb in activity_notification-2.0.0

- old
+ new

@@ -10,26 +10,27 @@ 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. + # Dependency for these notifications can be overridden from acts_as_notifiable. # @scope instance # @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, - :_notification_group_expiry_delay, - :_notifier, - :_notification_parameters, - :_notification_email_allowed, - :_notifiable_path, - :_printable_notifiable_name, - :_optional_targets + class_attribute :_notification_targets, + :_notification_group, + :_notification_group_expiry_delay, + :_notifier, + :_notification_parameters, + :_notification_email_allowed, + :_notification_action_cable_allowed, + :_notifiable_path, + :_printable_notifiable_name, + :_optional_targets set_notifiable_class_defaults end # Returns default_url_options for polymorphic_path. # @return [Hash] Rails.application.routes.default_url_options @@ -45,25 +46,26 @@ end # Sets default values to notifiable class fields. # @return [NilClass] nil def set_notifiable_class_defaults - self._notification_targets = {} - self._notification_group = {} - self._notification_group_expiry_delay = {} - self._notifier = {} - self._notification_parameters = {} - self._notification_email_allowed = {} - self._notifiable_path = {} - self._printable_notifiable_name = {} - self._optional_targets = {} + self._notification_targets = {} + self._notification_group = {} + self._notification_group_expiry_delay = {} + self._notifier = {} + self._notification_parameters = {} + self._notification_email_allowed = {} + self._notification_action_cable_allowed = {} + self._notifiable_path = {} + self._printable_notifiable_name = {} + self._optional_targets = {} nil end end - # Returns notification targets from configured field or overriden method. - # This method is able to be overriden. + # Returns notification targets from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [Hash] options Options for notifications # @option options [String] :key (notifiable.default_notification_key) Key of the notification # @option options [Hash] :parameters ({}) Additional parameters of the notifications @@ -80,12 +82,12 @@ "or set :targets in acts_as_notifiable" end resolved_parameter end - # Returns group unit of the notifications from configured field or overriden method. - # This method is able to be overriden. + # Returns group unit of the notifications from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Object] Group unit of the notifications def notification_group(target_type, key = nil) @@ -94,12 +96,12 @@ _notification_group[cast_to_resources_sym(target_type)], nil, key) end - # Returns group expiry period of the notifications from configured field or overriden method. - # This method is able to be overriden. + # Returns group expiry period of the notifications from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Object] Group expiry period of the notifications def notification_group_expiry_delay(target_type, key = nil) @@ -108,12 +110,12 @@ _notification_group_expiry_delay[cast_to_resources_sym(target_type)], nil, key) end - # Returns additional notification parameters from configured field or overriden method. - # This method is able to be overriden. + # Returns additional notification parameters from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Hash] Additional notification parameters def notification_parameters(target_type, key = nil) @@ -122,12 +124,12 @@ _notification_parameters[cast_to_resources_sym(target_type)], {}, key) end - # Returns notifier of the notification from configured field or overriden method. - # This method is able to be overriden. + # Returns notifier of the notification from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Object] Notifier of the notification def notifier(target_type, key = nil) @@ -136,12 +138,12 @@ _notifier[cast_to_resources_sym(target_type)], nil, key) end - # Returns if sending notification email is allowed for the notifiable from configured field or overriden method. - # This method is able to be overriden. + # Returns if sending notification email is allowed for the notifiable from configured field or overridden method. + # This method is able to be overridden. # # @param [Object] target Target instance to notify # @param [String] key Key of the notification # @return [Boolean] If sending notification email is allowed for the notifiable def notification_email_allowed?(target, key = nil) @@ -150,13 +152,27 @@ _notification_email_allowed[cast_to_resources_sym(target.class)], ActivityNotification.config.email_enabled, target, key) end - # Returns notifiable_path to move after opening notification from configured field or overriden method. - # This method is able to be overriden. + # Returns if publishing WebSocket using ActionCable is allowed for the notifiable from configured field or overridden method. + # This method is able to be overridden. # + # @param [Object] target Target instance to notify + # @param [String] key Key of the notification + # @return [Boolean] If publishing WebSocket using ActionCable is allowed for the notifiable + def notification_action_cable_allowed?(target, key = nil) + resolve_parameter( + "notification_action_cable_allowed_for_#{cast_to_resources_name(target.class)}?", + _notification_action_cable_allowed[cast_to_resources_sym(target.class)], + ActivityNotification.config.action_cable_enabled, + target, key) + end + + # Returns notifiable_path to move after opening notification from configured field or overridden method. + # This method is able to be overridden. + # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [String] Notifiable path URL to move after opening notification def notifiable_path(target_type, key = nil) resolved_parameter = resolve_parameter( @@ -184,12 +200,12 @@ _printable_notifiable_name[cast_to_resources_sym(target.class)], printable_name, target, key) end - # Returns optional_targets of the notification from configured field or overriden method. - # This method is able to be overriden. + # Returns optional_targets of the notification from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Array<ActivityNotification::OptionalTarget::Base>] Array of optional target instances def optional_targets(target_type, key = nil) @@ -198,12 +214,12 @@ _optional_targets[cast_to_resources_sym(target_type)], [], key) end - # Returns optional_target names of the notification from configured field or overriden method. - # This method is able to be overriden. + # Returns optional_target names of the notification from configured field or overridden method. + # This method is able to be overridden. # # @param [String] target_type Target type to notify # @param [String] key Key of the notification # @return [Array<Symbol>] Array of optional target names def optional_target_names(target_type, key = nil) @@ -351,29 +367,29 @@ def notify_later_to(target, options = {}) Notification.notify_later_to(target, self, options) end # Returns default key of the notification. - # This method is able to be overriden. + # This method is able to be overridden. # "#{to_resource_name}.default" is defined as default key. # # @return [String] Default key of the notification def default_notification_key "#{to_resource_name}.default" end # Returns key of the notification for tracked notifiable creation. - # This method is able to be overriden. + # This method is able to be overridden. # "#{to_resource_name}.create" is defined as default creation key. # # @return [String] Key of the notification for tracked notifiable creation def notification_key_for_tracked_creation "#{to_resource_name}.create" end # Returns key of the notification for tracked notifiable update. - # This method is able to be overriden. + # This method is able to be overridden. # "#{to_resource_name}.update" is defined as default update key. # # @return [String] Key of the notification for tracked notifiable update def notification_key_for_tracked_update "#{to_resource_name}.update" @@ -382,14 +398,14 @@ private # Used to transform parameter value from configured field or defined method. # @api private # - # @param [String] target_typed_method_name Method name overriden for the target type + # @param [String] target_typed_method_name Method name overridden for the target type # @param [Object] parameter_field Parameter Configured field in this model # @param [Object] default_value Default parameter value - # @param [Array] args Arguments to pass to the method overriden or defined as parameter field + # @param [Array] args Arguments to pass to the method overridden or defined as parameter field # @return [Object] Resolved parameter value def resolve_parameter(target_typed_method_name, parameter_field, default_value, *args) if respond_to?(target_typed_method_name) send(target_typed_method_name, *args) elsif parameter_field @@ -417,23 +433,15 @@ 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 - ActivityNotification::Notification.raise_delete_restriction_error("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}") unless generated_notifications.empty? + ActivityNotification::Notification.raise_delete_restriction_error("generated_notifications_as_notifiable_for_#{target_type.to_s.pluralize.underscore}") unless generated_notifications.to_a.empty? when :restrict_with_error - unless generated_notifications.empty? + unless generated_notifications.to_a.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) - # :only-rails5+: - if Rails::VERSION::MAJOR >= 5 - throw(:abort) - # :only-rails5+: - # :except-rails5+: - else - false - end - # :except-rails5+: + if Rails::VERSION::MAJOR >= 5 then throw(:abort) else false end end when :destroy generated_notifications.each { |n| n.destroy } when :delete_all generated_notifications.delete_all \ No newline at end of file