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

Rename subscriptions association to notification_subscriptions #163

Open
wants to merge 7 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 161 additions & 131 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion activity_notification.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.required_ruby_version = '>= 2.1.0'

s.add_dependency 'railties', '>= 5.0.0', '< 7.2'
s.add_dependency 'railties', '>= 5.0.0', '< 8'
s.add_dependency 'i18n', '>= 0.5.0'
s.add_dependency 'jquery-rails', '>= 3.1.1'
s.add_dependency 'swagger-blocks', '>= 3.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def index
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
# @return [Response] JavaScript view for ajax request or redirects to back as default
def create
@subscription = @target.create_subscription(subscription_params)
@subscription = @target.create_notification_subscription(subscription_params)
return_back_or_ajax
end

Expand All @@ -49,7 +49,7 @@ def create
# @option params [required, String] :key (nil) Key of the subscription
# @return [Response] HTML view as default or JSON of subscription index with json format parameter
def find
@subscription = @target.find_subscription(params[:key])
@subscription = @target.find_notification_subscription(params[:key])
@subscription ? redirect_to_subscription_path : render_resource_not_found("Couldn't find subscription with this target and 'key'=#{params[:key]}")
end

Expand Down Expand Up @@ -211,13 +211,13 @@ def set_index_options
def load_index
case @index_options[:filter]
when :configured, 'configured'
@subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
@subscriptions = @target.notification_subscription_index(@index_options.merge(with_target: true))
@notification_keys = nil
when :unconfigured, 'unconfigured'
@subscriptions = nil
@notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
else
@subscriptions = @target.subscription_index(@index_options.merge(with_target: true))
@subscriptions = @target.notification_subscription_index(@index_options.merge(with_target: true))
@notification_keys = @target.notification_keys(@index_options.merge(filter: :unconfigured))
end
end
Expand Down
353 changes: 179 additions & 174 deletions docs/Functions.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/activity_notification/apis/subscription_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ module SubscriptionApi
unless ActivityNotification.config.orm == :dynamoid
# Selects filtered subscriptions by key.
# @example Get filtered subscriptions of the @user with key 'comment.reply'
# @subscriptions = @user.subscriptions.filtered_by_key('comment.reply')
# @subscriptions = @user.notification_subscriptions.filtered_by_key('comment.reply')
# @scope class
# @param [String] key Key of the subscription for filter
# @return [ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>] Database query of filtered subscriptions
scope :filtered_by_key, ->(key) { where(key: key) }

# Selects filtered subscriptions by key with filter options.
# @example Get filtered subscriptions of the @user with key 'comment.reply'
# @subscriptions = @user.subscriptions.filtered_by_key('comment.reply')
# @subscriptions = @user.notification_subscriptions.filtered_by_key('comment.reply')
# @example Get custom filtered subscriptions of the @user
# @subscriptions = @user.subscriptions.filtered_by_options({ custom_filter: ["created_at >= ?", time.hour.ago] })
# @subscriptions = @user.notification_subscriptions.filtered_by_options({ custom_filter: ["created_at >= ?", time.hour.ago] })
# @scope class
# @param [Hash] options Options for filter
# @option options [String] :filtered_by_key (nil) Key of the subscription for filter
Expand Down
8 changes: 2 additions & 6 deletions lib/activity_notification/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ module Models
if defined?(ActiveRecord::Base)
ActiveRecord::Base.class_eval { include ActivityNotification::Models }

# https://github.com/simukappu/activity_notification/issues/166
# https://discuss.rubyonrails.org/t/cve-2022-32224-possible-rce-escalation-bug-with-serialized-columns-in-active-record/81017
if (Gem::Version.new("5.2.8.1") <= Rails.gem_version && Rails.gem_version < Gem::Version.new("6.0")) ||
(Gem::Version.new("6.0.5.1") <= Rails.gem_version && Rails.gem_version < Gem::Version.new("6.1")) ||
(Gem::Version.new("6.1.6.1") <= Rails.gem_version && Rails.gem_version < Gem::Version.new("7.0"))
if ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes)
ActiveRecord::Base.yaml_column_permitted_classes ||= []
ActiveRecord::Base.yaml_column_permitted_classes << ActiveSupport::HashWithIndifferentAccess
ActiveRecord::Base.yaml_column_permitted_classes << ActiveSupport::TimeWithZone
ActiveRecord::Base.yaml_column_permitted_classes << ActiveSupport::TimeZone
ActiveRecord::Base.yaml_column_permitted_classes << Symbol
ActiveRecord::Base.yaml_column_permitted_classes << Time
elsif Gem::Version.new("7.0.3.1") <= Rails.gem_version
elsif ActiveRecord.respond_to?(:yaml_column_permitted_classes)
ActiveRecord.yaml_column_permitted_classes ||= []
ActiveRecord.yaml_column_permitted_classes << ActiveSupport::HashWithIndifferentAccess
ActiveRecord.yaml_column_permitted_classes << ActiveSupport::TimeWithZone
Expand Down
78 changes: 39 additions & 39 deletions lib/activity_notification/models/concerns/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Subscriber
# Has many subscription instances of this target.
# @scope instance
# @return [Array<Subscription>, Mongoid::Criteria<Subscription>] Array or database query of subscriptions of this target
has_many_records :subscriptions,
has_many_records :notification_subscriptions,
class_name: "::ActivityNotification::Subscription",
as: :target,
dependent: :delete_all
Expand All @@ -19,7 +19,7 @@ module Subscriber
# Checks if the model includes subscriber and subscriber methods are available.
# Also checks if the model includes target and target methods are available, then return true.
# @return [Boolean] If the model includes target and subscriber are available
def available_as_subscriber?
def available_as_notification_subscriber?
available_as_target?
end
end
Expand All @@ -29,54 +29,54 @@ def available_as_subscriber?
#
# @param [Hash] key Key of the notification for subscription
# @return [Subscription] Configured subscription instance
def find_subscription(key)
subscriptions.where(key: key).first
def find_notification_subscription(key)
notification_subscriptions.where(key: key).first
end

# Gets subscription of the target and notification key.
#
# @param [Hash] key Key of the notification for subscription
# @param [Hash] subscription_params Parameters to create subscription record
# @return [Subscription] Found or created subscription instance
def find_or_create_subscription(key, subscription_params = {})
subscription = find_subscription(key)
subscription || create_subscription(subscription_params.merge(key: key))
# @return [NotificationSubscription] Found or created subscription instance
def find_or_create_notification_subscription(key, notification_subscription_params = {})
notification_subscription = find_notification_subscription(key)
notification_subscription || create_notification_subscription(notification_subscription_params.merge(key: key))
end

# Creates new subscription of the target.
#
# @param [Hash] subscription_params Parameters to create subscription record
# @raise [ActivityNotification::RecordInvalidError] Failed to save subscription due to model validation
# @return [Subscription] Created subscription instance
def create_subscription(subscription_params = {})
subscription = build_subscription(subscription_params)
raise RecordInvalidError, subscription.errors.full_messages.first unless subscription.save
subscription
# @return [NotificationSubscription] Created subscription instance
def create_notification_subscription(notification_subscription_params = {})
notification_subscription = build_notification_subscription(notification_subscription_params)
raise RecordInvalidError, notification_subscription.errors.full_messages.first unless notification_subscription.save
notification_subscription
end

# Builds new subscription of the target.
#
# @param [Hash] subscription_params Parameters to build subscription record
# @return [Subscription] Built subscription instance
def build_subscription(subscription_params = {})
# @return [NotificationSubscription] Built subscription instance
def build_notification_subscription(notification_subscription_params = {})
created_at = Time.current
if subscription_params[:subscribing] == false && subscription_params[:subscribing_to_email].nil?
subscription_params[:subscribing_to_email] = subscription_params[:subscribing]
elsif subscription_params[:subscribing_to_email].nil?
subscription_params[:subscribing_to_email] = ActivityNotification.config.subscribe_to_email_as_default
if notification_subscription_params[:subscribing] == false && notification_subscription_params[:subscribing_to_email].nil?
notification_subscription_params[:subscribing_to_email] = notification_subscription_params[:subscribing]
elsif notification_subscription_params[:subscribing_to_email].nil?
notification_subscription_params[:subscribing_to_email] = ActivityNotification.config.subscribe_to_email_as_default
end
subscription = Subscription.new(subscription_params)
subscription.assign_attributes(target: self)
subscription.subscribing? ?
subscription.assign_attributes(subscribing: true, subscribed_at: created_at) :
subscription.assign_attributes(subscribing: false, unsubscribed_at: created_at)
subscription.subscribing_to_email? ?
subscription.assign_attributes(subscribing_to_email: true, subscribed_to_email_at: created_at) :
subscription.assign_attributes(subscribing_to_email: false, unsubscribed_to_email_at: created_at)
subscription.optional_targets = (subscription.optional_targets || {}).with_indifferent_access
notification_subscription = Subscription.new(notification_subscription_params)
notification_subscription.assign_attributes(target: self)
notification_subscription.subscribing? ?
notification_subscription.assign_attributes(subscribing: true, subscribed_at: created_at) :
notification_subscription.assign_attributes(subscribing: false, unsubscribed_at: created_at)
notification_subscription.subscribing_to_email? ?
notification_subscription.assign_attributes(subscribing_to_email: true, subscribed_to_email_at: created_at) :
notification_subscription.assign_attributes(subscribing_to_email: false, unsubscribed_to_email_at: created_at)
notification_subscription.optional_targets = (notification_subscription.optional_targets || {}).with_indifferent_access
optional_targets = {}.with_indifferent_access
subscription.optional_target_names.each do |optional_target_name|
optional_targets = subscription.subscribing_to_optional_target?(optional_target_name) ?
notification_subscription.optional_target_names.each do |optional_target_name|
optional_targets = notification_subscription.subscribing_to_optional_target?(optional_target_name) ?
optional_targets.merge(
Subscription.to_optional_target_key(optional_target_name) => true,
Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(created_at)
Expand All @@ -86,14 +86,14 @@ def build_subscription(subscription_params = {})
Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(created_at)
)
end
subscription.assign_attributes(optional_targets: optional_targets)
subscription
notification_subscription.assign_attributes(optional_targets: optional_targets)
notification_subscription
end

# Gets configured subscription index of the target.
#
# @example Get configured subscription index of the @user
# @subscriptions = @user.subscription_index
# @subscriptions = @user.notification_subscription_index
#
# @param [Hash] options Options for subscription index
# @option options [Integer] :limit (nil) Limit to query for subscriptions
Expand All @@ -102,8 +102,8 @@ def build_subscription(subscription_params = {})
# @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
# @option options [Boolean] :with_target (false) If it includes target with subscriptions
# @return [Array<Notificaion>] Configured subscription index of the target
def subscription_index(options = {})
target_index = subscriptions.filtered_by_options(options)
def notification_subscription_index(options = {})
target_index = notification_subscriptions.filtered_by_options(options)
target_index = options[:reverse] ? target_index.earliest_order : target_index.latest_order
target_index = target_index.with_target if options[:with_target]
options[:limit].present? ? target_index.limit(options[:limit]).to_a : target_index.to_a
Expand All @@ -122,7 +122,7 @@ def subscription_index(options = {})
# @option options [Array|Hash] :custom_filter (nil) Custom subscription filter (e.g. ["created_at >= ?", time.hour.ago])
# @return [Array<Notificaion>] Unconfigured notification keys of the target
def notification_keys(options = {})
subscription_keys = subscriptions.uniq_keys
subscription_keys = notification_subscriptions.uniq_keys
target_notifications = notifications.filtered_by_options(options.select { |k, _| [:filtered_by_key, :custom_filter].include?(k) })
target_notifications = options[:reverse] ? target_notifications.earliest_order : target_notifications.latest_order
target_notifications = options[:limit].present? ? target_notifications.limit(options[:limit] + subscription_keys.size) : target_notifications
Expand All @@ -149,7 +149,7 @@ def notification_keys(options = {})
# @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
# @return [Boolean] If the target subscribes to the notification
def _subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
evaluate_subscription(subscriptions.where(key: key).first, :subscribing?, subscribe_as_default)
evaluate_subscription(notification_subscriptions.where(key: key).first, :subscribing?, subscribe_as_default)
end

# Returns if the target subscribes to the notification email.
Expand All @@ -160,7 +160,7 @@ def _subscribes_to_notification?(key, subscribe_as_default = ActivityNotificatio
# @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
# @return [Boolean] If the target subscribes to the notification
def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_to_email_as_default)
evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_email?, subscribe_as_default)
evaluate_subscription(notification_subscriptions.where(key: key).first, :subscribing_to_email?, subscribe_as_default)
end
alias_method :_subscribes_to_email?, :_subscribes_to_notification_email?

Expand All @@ -174,7 +174,7 @@ def _subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotif
# @return [Boolean] If the target subscribes to the specified optional target
def _subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default)
_subscribes_to_notification?(key, subscribe_as_default) &&
evaluate_subscription(subscriptions.where(key: key).first, :subscribing_to_optional_target?, subscribe_as_default, optional_target_name, subscribe_as_default)
evaluate_subscription(notification_subscriptions.where(key: key).first, :subscribing_to_optional_target?, subscribe_as_default, optional_target_name, subscribe_as_default)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Subscription < ::ActiveRecord::Base
# Selects filtered subscriptions by target instance.
# ActivityNotification::Subscription.filtered_by_target(@user)
# is the same as
# @user.subscriptions
# @user.notification_subscriptions
# @scope class
# @param [Object] target Target instance for filter
# @return [ActiveRecord_AssociationRelation<Subscription>] Database query of filtered subscriptions
Expand Down
2 changes: 1 addition & 1 deletion lib/activity_notification/orm/mongoid/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Subscription
# Selects filtered subscriptions by target instance.
# ActivityNotification::Subscription.filtered_by_target(@user)
# is the same as
# @user.subscriptions
# @user.notification_subscriptions
# @scope class
# @param [Object] target Target instance for filter
# @return [Mongoid::Criteria<Subscription>] Database query of filtered subscriptions
Expand Down
6 changes: 3 additions & 3 deletions lib/activity_notification/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ def subscribed_by(*resources)
options[:defaults] = { target_type: target.to_s }.merge(options[:devise_defaults])
resources_options = options.select { |key, _| [:api_mode, :with_devise, :devise_default_routes, :model, :devise_defaults].exclude? key }
if options[:with_devise].present? && options[:devise_default_routes].present?
create_subscription_routes options, resources_options
create_notification_subscription_routes options, resources_options
else
self.resources target, only: :none do
create_subscription_routes options, resources_options
create_notification_subscription_routes options, resources_options
end
end
end
Expand Down Expand Up @@ -428,7 +428,7 @@ def create_notification_routes(options = {}, resources_options = [])
# @param [Symbol] resource Name of the resource model
# @param [Hash] options Passed options from subscribed_by
# @param [Hash] resources_options Options to send resources method
def create_subscription_routes(options = {}, resources_options = [])
def create_notification_subscription_routes(options = {}, resources_options = [])
self.resources options[:model], resources_options do
collection do
get :find unless ignore_path?(:find, options)
Expand Down
Loading
Loading