activity_notification 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +15 -5
- data/CHANGELOG.md +13 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +78 -71
- data/README.md +64 -43
- data/activity_notification.gemspec +5 -4
- data/app/controllers/activity_notification/notifications_controller.rb +1 -1
- data/app/controllers/activity_notification/subscriptions_controller.rb +1 -1
- data/gemfiles/Gemfile.rails-4.2 +1 -1
- data/gemfiles/Gemfile.rails-4.2.lock +71 -62
- data/gemfiles/Gemfile.rails-5.0 +1 -1
- data/gemfiles/Gemfile.rails-5.0.lock +74 -67
- data/lib/activity_notification.rb +9 -2
- data/lib/activity_notification/apis/notification_api.rb +142 -76
- data/lib/activity_notification/apis/subscription_api.rb +72 -0
- data/lib/activity_notification/config.rb +12 -0
- data/lib/activity_notification/models/concerns/notifiable.rb +56 -6
- data/lib/activity_notification/models/concerns/notifier.rb +8 -1
- data/lib/activity_notification/models/concerns/subscriber.rb +13 -10
- data/lib/activity_notification/models/concerns/target.rb +7 -5
- data/lib/activity_notification/models/notification.rb +2 -270
- data/lib/activity_notification/models/subscription.rb +3 -101
- data/lib/activity_notification/optional_targets/base.rb +5 -1
- data/lib/activity_notification/orm/active_record.rb +16 -0
- data/lib/activity_notification/orm/active_record/notification.rb +252 -0
- data/lib/activity_notification/orm/active_record/subscription.rb +52 -0
- data/lib/activity_notification/orm/mongoid.rb +63 -0
- data/lib/activity_notification/orm/mongoid/notification.rb +255 -0
- data/lib/activity_notification/orm/mongoid/subscription.rb +73 -0
- data/lib/activity_notification/rails/routes.rb +7 -3
- data/lib/activity_notification/renderable.rb +5 -1
- data/lib/activity_notification/roles/acts_as_notifiable.rb +4 -18
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/install_generator.rb +3 -5
- data/lib/generators/templates/activity_notification.rb +9 -4
- data/spec/concerns/apis/notification_api_spec.rb +27 -14
- data/spec/concerns/models/notifiable_spec.rb +10 -8
- data/spec/concerns/models/subscriber_spec.rb +12 -12
- data/spec/concerns/models/target_spec.rb +61 -51
- data/spec/controllers/subscriptions_controller_shared_examples.rb +0 -1
- data/spec/helpers/view_helpers_spec.rb +24 -7
- data/spec/models/notification_spec.rb +63 -52
- data/spec/models/subscription_spec.rb +6 -3
- data/spec/optional_targets/amazon_sns_spec.rb +8 -5
- data/spec/optional_targets/base_spec.rb +3 -1
- data/spec/optional_targets/slack_spec.rb +5 -5
- data/spec/rails_app/app/models/comment.rb +1 -1
- data/spec/rails_app/app/views/activity_notification/notifications/users/overriden/custom/_test.html.erb +1 -0
- data/spec/rails_app/config/application.rb +7 -0
- data/spec/rails_app/config/initializers/activity_notification.rb +5 -0
- data/spec/rails_app/config/mongoid.yml +13 -0
- data/spec/rails_app/db/seeds.rb +1 -1
- data/spec/rails_app/lib/custom_optional_targets/console_output.rb +7 -4
- data/spec/rails_app/lib/custom_optional_targets/wrong_target.rb +3 -0
- data/spec/roles/acts_as_notifiable_spec.rb +77 -16
- data/spec/spec_helper.rb +7 -0
- metadata +38 -14
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'activity_notification/apis/subscription_api'
|
3
|
+
|
4
|
+
module ActivityNotification
|
5
|
+
module ORM
|
6
|
+
module Mongoid
|
7
|
+
# Subscription model implementation generated by ActivityNotification.
|
8
|
+
class Subscription
|
9
|
+
include ::Mongoid::Document
|
10
|
+
include ::Mongoid::Timestamps
|
11
|
+
include ::Mongoid::Attributes::Dynamic
|
12
|
+
include Association
|
13
|
+
include SubscriptionApi
|
14
|
+
store_in collection: ActivityNotification.config.subscription_table_name
|
15
|
+
|
16
|
+
# Belongs to target instance of this subscription as polymorphic association.
|
17
|
+
# @scope instance
|
18
|
+
# @return [Object] Target instance of this subscription
|
19
|
+
belongs_to_polymorphic_xdb_record :target
|
20
|
+
|
21
|
+
field :key, type: String
|
22
|
+
field :subscribing, type: Boolean, default: ActivityNotification.config.subscribe_as_default
|
23
|
+
field :subscribing_to_email, type: Boolean, default: ActivityNotification.config.subscribe_as_default
|
24
|
+
field :subscribed_at, type: DateTime
|
25
|
+
field :unsubscribed_at, type: DateTime
|
26
|
+
field :subscribed_to_email_at, type: DateTime
|
27
|
+
field :unsubscribed_to_email_at, type: DateTime
|
28
|
+
field :optional_targets, type: Hash, default: {}
|
29
|
+
|
30
|
+
validates :target, presence: true
|
31
|
+
validates :key, presence: true
|
32
|
+
validates_inclusion_of :subscribing, in: [true, false]
|
33
|
+
validates_inclusion_of :subscribing_to_email, in: [true, false]
|
34
|
+
validate :subscribing_to_email_cannot_be_true_when_subscribing_is_false
|
35
|
+
validates :subscribed_at, presence: true, if: :subscribing
|
36
|
+
validates :unsubscribed_at, presence: true, unless: :subscribing
|
37
|
+
validates :subscribed_to_email_at, presence: true, if: :subscribing_to_email
|
38
|
+
validates :unsubscribed_to_email_at, presence: true, unless: :subscribing_to_email
|
39
|
+
validate :subscribing_to_optional_target_cannot_be_true_when_subscribing_is_false
|
40
|
+
|
41
|
+
# Selects filtered subscriptions by type of the object.
|
42
|
+
# Filtering with ActivityNotification::Subscription is defined as default scope.
|
43
|
+
# @return [Mongoid::Criteria<Subscription>] Database query of filtered subscriptions
|
44
|
+
default_scope -> { where(_type: "ActivityNotification::Subscription") }
|
45
|
+
|
46
|
+
# Selects filtered subscriptions by target instance.
|
47
|
+
# ActivityNotification::Subscription.filtered_by_target(@user)
|
48
|
+
# is the same as
|
49
|
+
# @user.subscriptions
|
50
|
+
# @scope class
|
51
|
+
# @param [Object] target Target instance for filter
|
52
|
+
# @return [Mongoid::Criteria<Subscription>] Database query of filtered subscriptions
|
53
|
+
scope :filtered_by_target, ->(target) { target.present? ? where(target_id: target.id, target_type: target.class.name) : none }
|
54
|
+
|
55
|
+
# Includes target instance with query for subscriptions.
|
56
|
+
# @return [Mongoid::Criteria<Subscription>] Database query of subscriptions with target
|
57
|
+
scope :with_target, -> { }
|
58
|
+
|
59
|
+
# Dummy reload method for test of subscriptions.
|
60
|
+
scope :reload, -> { }
|
61
|
+
|
62
|
+
# Selects unique keys from query for subscriptions.
|
63
|
+
# @return [Array<String>] Array of subscription unique keys
|
64
|
+
def self.uniq_keys
|
65
|
+
# distinct method cannot keep original sort
|
66
|
+
# distinct(:key)
|
67
|
+
pluck(:key).uniq
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -204,16 +204,20 @@ module ActionDispatch::Routing
|
|
204
204
|
|
205
205
|
# Create options fo routing
|
206
206
|
# @api private
|
207
|
+
# @todo Check resources if it includes target module
|
208
|
+
# @todo Check devise configuration in model
|
209
|
+
# @todo Support other options like :as, :path_prefix, :path_names ...
|
210
|
+
#
|
207
211
|
# @param [Symbol] resource Name of the resource model
|
208
212
|
# @return [Boolean] Whether action path is ignored
|
209
213
|
def create_options(resource, options = {}, except_actions = [])
|
210
|
-
#
|
214
|
+
# Check resources if it includes target module
|
211
215
|
resources_name = resource.to_s.pluralize.underscore
|
212
216
|
options[:model] ||= resources_name.to_sym
|
213
217
|
if options[:with_devise].present?
|
214
218
|
options[:controller] ||= "activity_notification/#{resources_name}_with_devise"
|
215
219
|
options[:as] ||= resources_name
|
216
|
-
#
|
220
|
+
# Check devise configuration in model
|
217
221
|
options[:devise_defaults] = { devise_type: options[:with_devise].to_s }
|
218
222
|
else
|
219
223
|
options[:controller] ||= "activity_notification/#{resources_name}"
|
@@ -224,7 +228,7 @@ module ActionDispatch::Routing
|
|
224
228
|
options[:subscription_option] = (options[:with_subscription].is_a?(Hash) ? options[:with_subscription] : {})
|
225
229
|
.merge(with_devise: options[:with_devise])
|
226
230
|
end
|
227
|
-
#
|
231
|
+
# Support other options like :as, :path_prefix, :path_names ...
|
228
232
|
options
|
229
233
|
end
|
230
234
|
|
@@ -173,7 +173,11 @@ module ActivityNotification
|
|
173
173
|
root ||= "activity_notification/notifications/#{target}" if target.present?
|
174
174
|
root ||= controller.target_view_path if controller.present? && controller.respond_to?(:target_view_path)
|
175
175
|
root ||= 'activity_notification/notifications/default'
|
176
|
-
|
176
|
+
template_key = notifiable.respond_to?(:overriding_notification_template_key) &&
|
177
|
+
notifiable.overriding_notification_template_key(@target, key).present? ?
|
178
|
+
notifiable.overriding_notification_template_key(@target, key) :
|
179
|
+
key
|
180
|
+
path ||= template_key.tr('.', '/')
|
177
181
|
select_path(path, root)
|
178
182
|
end
|
179
183
|
|
@@ -171,24 +171,21 @@ module ActivityNotification
|
|
171
171
|
if [:delete_all, :destroy, :restrict_with_error, :restrict_with_exception, :update_group_and_delete_all, :update_group_and_destroy].include? options[:dependent_notifications]
|
172
172
|
case options[:dependent_notifications]
|
173
173
|
when :delete_all, :destroy, :restrict_with_error, :restrict_with_exception
|
174
|
-
|
174
|
+
before_destroy -> { destroy_generated_notifications_with_dependency(options[:dependent_notifications], target_type) }
|
175
175
|
when :update_group_and_delete_all
|
176
|
-
before_destroy :
|
177
|
-
has_many_generated_notifications :delete_all
|
176
|
+
before_destroy -> { destroy_generated_notifications_with_dependency(:delete_all, target_type, true) }
|
178
177
|
when :update_group_and_destroy
|
179
|
-
before_destroy :
|
180
|
-
has_many_generated_notifications :destroy
|
178
|
+
before_destroy -> { destroy_generated_notifications_with_dependency(:destroy, target_type, true) }
|
181
179
|
end
|
182
180
|
configured_params = { dependent_notifications: options[:dependent_notifications] }
|
183
181
|
end
|
184
182
|
|
185
183
|
if options[:optional_targets].is_a?(Hash)
|
186
184
|
options[:optional_targets] = options[:optional_targets].map { |target_class, target_options|
|
187
|
-
optional_target = target_class.new
|
185
|
+
optional_target = target_class.new(target_options)
|
188
186
|
unless optional_target.kind_of?(ActivityNotification::OptionalTarget::Base)
|
189
187
|
raise TypeError, "#{optional_target.class.name} for an optional target is not a kind of ActivityNotification::OptionalTarget::Base"
|
190
188
|
end
|
191
|
-
optional_target.initialize_target(target_options)
|
192
189
|
optional_target
|
193
190
|
}
|
194
191
|
end
|
@@ -215,17 +212,6 @@ module ActivityNotification
|
|
215
212
|
].freeze
|
216
213
|
end
|
217
214
|
|
218
|
-
private
|
219
|
-
|
220
|
-
# Define to have many notification instances for this notifiable with dependent option.
|
221
|
-
# @api private
|
222
|
-
def has_many_generated_notifications(dependent_option)
|
223
|
-
has_many :generated_notifications_as_notifiable,
|
224
|
-
class_name: "::ActivityNotification::Notification",
|
225
|
-
as: :notifiable,
|
226
|
-
dependent: dependent_option
|
227
|
-
end
|
228
|
-
|
229
215
|
end
|
230
216
|
end
|
231
217
|
end
|
@@ -13,13 +13,11 @@ module ActivityNotification
|
|
13
13
|
|
14
14
|
# Copies initializer file in application directory
|
15
15
|
def copy_initializer
|
16
|
-
|
17
|
-
unless options[:orm] == :active_record
|
16
|
+
unless [:active_record, :mongoid].include?(options[:orm])
|
18
17
|
raise TypeError, <<-ERROR.strip_heredoc
|
19
|
-
Currently ActivityNotification is only supported with
|
18
|
+
Currently ActivityNotification is only supported with ActiveRecord or Mongoid ORM.
|
20
19
|
|
21
|
-
Be sure to have an
|
22
|
-
app or configure your own at `config/application.rb`.
|
20
|
+
Be sure to have an ActiveRecord or MongoidORM loaded in your app or configure your own at `config/application.rb`.
|
23
21
|
|
24
22
|
config.generators do |g|
|
25
23
|
g.orm :active_record
|
@@ -1,13 +1,18 @@
|
|
1
1
|
ActivityNotification.configure do |config|
|
2
2
|
|
3
|
-
# Configure
|
4
|
-
# Set
|
3
|
+
# Configure ORM name for ActivityNotification.
|
4
|
+
# Set :active_record or :mongoid.
|
5
|
+
ENV['AN_ORM'] = 'active_record' if ENV['AN_ORM'] != 'mongoid'
|
6
|
+
config.orm = ENV['AN_ORM']
|
7
|
+
|
8
|
+
# Configure if all activity notifications are enabled.
|
9
|
+
# Set false when you want to turn off activity notifications.
|
5
10
|
config.enabled = true
|
6
11
|
|
7
|
-
# Configure table name to store notification data
|
12
|
+
# Configure table name to store notification data.
|
8
13
|
config.notification_table_name = "notifications"
|
9
14
|
|
10
|
-
# Configure table name to store subscription data
|
15
|
+
# Configure table name to store subscription data.
|
11
16
|
config.subscription_table_name = "subscriptions"
|
12
17
|
|
13
18
|
# Configure if email notification is enabled as default.
|
@@ -11,7 +11,6 @@ shared_examples_for :notification_api do
|
|
11
11
|
|
12
12
|
describe "as public class methods" do
|
13
13
|
before do
|
14
|
-
described_class.delete_all
|
15
14
|
@author_user = create(:confirmed_user)
|
16
15
|
@user_1 = create(:confirmed_user)
|
17
16
|
@user_2 = create(:confirmed_user)
|
@@ -217,7 +216,7 @@ shared_examples_for :notification_api do
|
|
217
216
|
end
|
218
217
|
|
219
218
|
it "has parameters of parameters in acts_as_notifiable" do
|
220
|
-
expect(created_notification.parameters).to eq({test_default_param
|
219
|
+
expect(created_notification.parameters).to eq({'test_default_param' => '1'})
|
221
220
|
end
|
222
221
|
end
|
223
222
|
|
@@ -517,7 +516,7 @@ shared_examples_for :notification_api do
|
|
517
516
|
describe "#publish_to_optional_targets" do
|
518
517
|
before do
|
519
518
|
require 'custom_optional_targets/console_output'
|
520
|
-
@optional_target = CustomOptionalTarget::ConsoleOutput.new
|
519
|
+
@optional_target = CustomOptionalTarget::ConsoleOutput.new(console_out: false)
|
521
520
|
notifiable_class.acts_as_notifiable test_instance.target.to_resources_name.to_sym, optional_targets: ->{ [@optional_target] }
|
522
521
|
expect(test_instance.notifiable.optional_targets(test_instance.target.to_resources_name, test_instance.key)).to eq([@optional_target])
|
523
522
|
end
|
@@ -556,16 +555,13 @@ shared_examples_for :notification_api do
|
|
556
555
|
end
|
557
556
|
|
558
557
|
describe "#open!" do
|
559
|
-
before do
|
560
|
-
described_class.delete_all
|
561
|
-
end
|
562
|
-
|
563
558
|
it "returns the number of opened notification records" do
|
564
559
|
expect(test_instance.open!).to eq(1)
|
565
560
|
end
|
566
561
|
|
567
562
|
it "returns the number of opened notification records including group members" do
|
568
|
-
create(test_class_name, group_owner: test_instance
|
563
|
+
group_member = create(test_class_name, group_owner: test_instance)
|
564
|
+
expect(group_member.opened_at.blank?).to be_truthy
|
569
565
|
expect(test_instance.open!).to eq(2)
|
570
566
|
end
|
571
567
|
|
@@ -856,23 +852,39 @@ shared_examples_for :notification_api do
|
|
856
852
|
|
857
853
|
context "with limit" do
|
858
854
|
context "when the notification is group owner and has group members" do
|
859
|
-
it "returns member count by limit" do
|
855
|
+
it "returns member count by limit 0" do
|
860
856
|
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
861
857
|
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
862
858
|
test_instance.open!
|
863
859
|
expect(test_instance.group_member_count(0)).to eq(0)
|
864
860
|
expect(test_instance.group_notification_count(0)).to eq(1)
|
865
861
|
end
|
862
|
+
|
863
|
+
it "returns member count by limit 1" do
|
864
|
+
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
865
|
+
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
866
|
+
test_instance.open!
|
867
|
+
expect(test_instance.group_member_count(1)).to eq(1)
|
868
|
+
expect(test_instance.group_notification_count(1)).to eq(2)
|
869
|
+
end
|
866
870
|
end
|
867
871
|
|
868
872
|
context "when the notification belongs to group" do
|
869
|
-
it "returns member count by limit" do
|
873
|
+
it "returns member count by limit 0" do
|
870
874
|
group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
871
875
|
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
872
876
|
test_instance.open!
|
873
877
|
expect(group_member.group_member_count(0)).to eq(0)
|
874
878
|
expect(group_member.group_notification_count(0)).to eq(1)
|
875
879
|
end
|
880
|
+
|
881
|
+
it "returns member count by limit 1" do
|
882
|
+
group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
883
|
+
create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
884
|
+
test_instance.open!
|
885
|
+
expect(group_member.group_member_count(1)).to eq(1)
|
886
|
+
expect(group_member.group_notification_count(1)).to eq(2)
|
887
|
+
end
|
876
888
|
end
|
877
889
|
end
|
878
890
|
end
|
@@ -1135,7 +1147,7 @@ shared_examples_for :notification_api do
|
|
1135
1147
|
it "returns latest group member" do
|
1136
1148
|
member1 = create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
1137
1149
|
member2 = create(test_class_name, target: test_instance.target, group_owner: test_instance)
|
1138
|
-
expect(test_instance.latest_group_member).to eq(member2)
|
1150
|
+
expect(test_instance.latest_group_member.becomes(ActivityNotification::Notification)).to eq(member2)
|
1139
1151
|
end
|
1140
1152
|
end
|
1141
1153
|
|
@@ -1161,12 +1173,13 @@ shared_examples_for :notification_api do
|
|
1161
1173
|
|
1162
1174
|
it "makes a new group owner" do
|
1163
1175
|
test_instance.remove_from_group
|
1164
|
-
expect(@member1.reload.group_owner?).to
|
1165
|
-
expect(@member1.group_members).to
|
1176
|
+
expect(@member1.reload.group_owner?).to be_truthy
|
1177
|
+
expect(@member1.group_members.size).to eq(1)
|
1178
|
+
expect(@member1.group_members.first.becomes(ActivityNotification::Notification)).to eq(@member2)
|
1166
1179
|
end
|
1167
1180
|
|
1168
1181
|
it "returns new group owner instance" do
|
1169
|
-
expect(test_instance.remove_from_group).to eq(@member1)
|
1182
|
+
expect(test_instance.remove_from_group.becomes(ActivityNotification::Notification)).to eq(@member1)
|
1170
1183
|
end
|
1171
1184
|
end
|
1172
1185
|
|
@@ -521,25 +521,26 @@ shared_examples_for :notifiable do
|
|
521
521
|
|
522
522
|
it "returns specified symbol of method" do
|
523
523
|
module AdditionalMethods
|
524
|
+
require 'custom_optional_targets/console_output'
|
524
525
|
def custom_optional_targets
|
525
|
-
[
|
526
|
+
[CustomOptionalTarget::ConsoleOutput.new]
|
526
527
|
end
|
527
528
|
end
|
528
529
|
test_instance.extend(AdditionalMethods)
|
529
530
|
described_class._optional_targets[:users] = :custom_optional_targets
|
530
531
|
expect(test_instance.optional_targets(User, 'dummy_key').size).to eq(1)
|
531
|
-
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(
|
532
|
+
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(CustomOptionalTarget::ConsoleOutput)
|
532
533
|
end
|
533
534
|
|
534
535
|
it "returns specified lambda with no arguments" do
|
535
|
-
described_class._optional_targets[:users] = ->{ [
|
536
|
-
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(
|
536
|
+
described_class._optional_targets[:users] = ->{ [CustomOptionalTarget::ConsoleOutput.new] }
|
537
|
+
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(CustomOptionalTarget::ConsoleOutput)
|
537
538
|
end
|
538
539
|
|
539
540
|
it "returns specified lambda with notifiable and key argument" do
|
540
|
-
described_class._optional_targets[:users] = ->(notifiable, key){ key == 'dummy_key' ? [
|
541
|
+
described_class._optional_targets[:users] = ->(notifiable, key){ key == 'dummy_key' ? [CustomOptionalTarget::ConsoleOutput.new] : [] }
|
541
542
|
expect(test_instance.optional_targets(User)).to eq([])
|
542
|
-
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(
|
543
|
+
expect(test_instance.optional_targets(User, 'dummy_key').first).to be_a(CustomOptionalTarget::ConsoleOutput)
|
543
544
|
end
|
544
545
|
end
|
545
546
|
end
|
@@ -565,13 +566,14 @@ shared_examples_for :notifiable do
|
|
565
566
|
|
566
567
|
it "returns specified symbol of method" do
|
567
568
|
module AdditionalMethods
|
569
|
+
require 'custom_optional_targets/console_output'
|
568
570
|
def custom_optional_targets
|
569
|
-
[
|
571
|
+
[CustomOptionalTarget::ConsoleOutput.new]
|
570
572
|
end
|
571
573
|
end
|
572
574
|
test_instance.extend(AdditionalMethods)
|
573
575
|
described_class._optional_targets[:users] = :custom_optional_targets
|
574
|
-
expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:
|
576
|
+
expect(test_instance.optional_target_names(User, 'dummy_key')).to eq([:console_output])
|
575
577
|
end
|
576
578
|
|
577
579
|
it "returns specified lambda with no arguments" do
|
@@ -12,10 +12,10 @@ shared_examples_for :subscriber do
|
|
12
12
|
it "has many subscriptions" do
|
13
13
|
subscription_1 = create(:subscription, target: test_instance, key: 'subscription_key_1')
|
14
14
|
subscription_2 = create(:subscription, target: test_instance, key: 'subscription_key_2')
|
15
|
-
expect(test_instance.subscriptions.count).to
|
15
|
+
expect(test_instance.subscriptions.count).to eq(2)
|
16
16
|
expect(test_instance.subscriptions.earliest_order.first).to eq(subscription_1)
|
17
17
|
expect(test_instance.subscriptions.latest_order.first).to eq(subscription_2)
|
18
|
-
expect(test_instance.subscriptions).to
|
18
|
+
expect(test_instance.subscriptions).to eq(ActivityNotification::Subscription.filtered_by_target(test_instance))
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -28,10 +28,6 @@ shared_examples_for :subscriber do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "as public instance methods" do
|
31
|
-
before do
|
32
|
-
ActivityNotification::Subscription.delete_all
|
33
|
-
end
|
34
|
-
|
35
31
|
describe "#find_subscription" do
|
36
32
|
before do
|
37
33
|
expect(test_instance.subscriptions).to be_empty
|
@@ -224,9 +220,11 @@ shared_examples_for :subscriber do
|
|
224
220
|
|
225
221
|
context 'with custom_filter options' do
|
226
222
|
it "returns filtered notifications only" do
|
227
|
-
|
228
|
-
|
229
|
-
|
223
|
+
if ActivityNotification.config.orm == :active_record
|
224
|
+
options = { custom_filter: ["key = ?", 'subscription_key_2'] }
|
225
|
+
expect(test_instance.subscription_index(options)[0]).to eq(@subscription2)
|
226
|
+
expect(test_instance.subscription_index(options).size).to eq(1)
|
227
|
+
end
|
230
228
|
|
231
229
|
options = { custom_filter: { key: 'subscription_key_1' } }
|
232
230
|
expect(test_instance.subscription_index(options)[0]).to eq(@subscription1)
|
@@ -324,9 +322,11 @@ shared_examples_for :subscriber do
|
|
324
322
|
|
325
323
|
context 'with custom_filter options' do
|
326
324
|
it "returns filtered notifications only" do
|
327
|
-
|
328
|
-
|
329
|
-
|
325
|
+
if ActivityNotification.config.orm == :active_record
|
326
|
+
options = { custom_filter: ["key = ?", 'notification_key_2'] }
|
327
|
+
expect(test_instance.notification_keys(options)[0]).to eq('notification_key_2')
|
328
|
+
expect(test_instance.notification_keys(options).size).to eq(1)
|
329
|
+
end
|
330
330
|
|
331
331
|
options = { custom_filter: { key: 'notification_key_1' } }
|
332
332
|
expect(test_instance.notification_keys(options)[0]).to eq('notification_key_1')
|
@@ -393,10 +393,10 @@ shared_examples_for :target do
|
|
393
393
|
|
394
394
|
describe "#authenticated_with_devise?" do
|
395
395
|
context "without any configuration" do
|
396
|
-
context "when the current devise resource and called target are
|
396
|
+
context "when the current devise resource and called target are different class instance" do
|
397
397
|
it "raises TypeError" do
|
398
398
|
expect { test_instance.authenticated_with_devise?(test_notifiable) }
|
399
|
-
.to raise_error(TypeError, /
|
399
|
+
.to raise_error(TypeError, /Different type of .+ has been passed to .+ You have to override .+ /)
|
400
400
|
end
|
401
401
|
end
|
402
402
|
|
@@ -414,11 +414,11 @@ shared_examples_for :target do
|
|
414
414
|
end
|
415
415
|
|
416
416
|
context "configured with a field" do
|
417
|
-
context "when the current devise resource and called target are
|
417
|
+
context "when the current devise resource and called target are different class instance" do
|
418
418
|
it "raises TypeError" do
|
419
419
|
described_class._notification_devise_resource = test_notifiable
|
420
420
|
expect { test_instance.authenticated_with_devise?(test_instance) }
|
421
|
-
.to raise_error(TypeError, /
|
421
|
+
.to raise_error(TypeError, /Different type of .+ has been passed to .+ You have to override .+ /)
|
422
422
|
end
|
423
423
|
end
|
424
424
|
|
@@ -614,9 +614,11 @@ shared_examples_for :target do
|
|
614
614
|
|
615
615
|
context 'with custom_filter options' do
|
616
616
|
it "returns filtered notifications only" do
|
617
|
-
|
618
|
-
|
619
|
-
|
617
|
+
if ActivityNotification.config.orm == :active_record
|
618
|
+
options = { custom_filter: ["key = ?", @key] }
|
619
|
+
expect(test_instance.notification_index(options)[0]).to eq(@notification3)
|
620
|
+
expect(test_instance.notification_index(options).size).to eq(1)
|
621
|
+
end
|
620
622
|
|
621
623
|
options = { custom_filter: { key: @key } }
|
622
624
|
expect(test_instance.notification_index(options)[0]).to eq(@notification3)
|
@@ -920,36 +922,40 @@ shared_examples_for :target do
|
|
920
922
|
create(:notification, target: test_instance)
|
921
923
|
create(:notification, target: test_instance)
|
922
924
|
end
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
925
|
+
|
926
|
+
if ActivityNotification.config.orm == :active_record
|
927
|
+
it "calls with_target, with_notifiable and with_notifier" do
|
928
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
929
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
930
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
931
|
+
test_instance.unopened_notification_index_with_attributes
|
932
|
+
end
|
933
|
+
|
934
|
+
it "does not call with_group" do
|
935
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
936
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
937
|
+
expect(ActiveRecord::Base).not_to receive(:includes).with(:group)
|
938
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
939
|
+
test_instance.unopened_notification_index_with_attributes
|
940
|
+
end
|
937
941
|
end
|
938
942
|
end
|
939
|
-
|
943
|
+
|
940
944
|
context "with group members" do
|
941
945
|
before do
|
942
946
|
group_owner = create(:notification, target: test_instance, group_owner: nil)
|
943
947
|
create(:notification, target: test_instance, group_owner: nil)
|
944
948
|
group_member = create(:notification, target: test_instance, group_owner: group_owner)
|
945
949
|
end
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
950
|
+
|
951
|
+
if ActivityNotification.config.orm == :active_record
|
952
|
+
it "calls with_group" do
|
953
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
954
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
955
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:group)
|
956
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
957
|
+
test_instance.unopened_notification_index_with_attributes
|
958
|
+
end
|
953
959
|
end
|
954
960
|
end
|
955
961
|
end
|
@@ -978,36 +984,40 @@ shared_examples_for :target do
|
|
978
984
|
create(:notification, target: test_instance, opened_at: Time.current)
|
979
985
|
create(:notification, target: test_instance, opened_at: Time.current)
|
980
986
|
end
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
987
|
+
|
988
|
+
if ActivityNotification.config.orm == :active_record
|
989
|
+
it "calls with_target, with_notifiable and with_notifier" do
|
990
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
991
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
992
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
993
|
+
test_instance.opened_notification_index_with_attributes
|
994
|
+
end
|
995
|
+
|
996
|
+
it "does not call with_group" do
|
997
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
998
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
999
|
+
expect(ActiveRecord::Base).not_to receive(:includes).with(:group)
|
1000
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
1001
|
+
test_instance.opened_notification_index_with_attributes
|
1002
|
+
end
|
995
1003
|
end
|
996
1004
|
end
|
997
|
-
|
1005
|
+
|
998
1006
|
context "with group members" do
|
999
1007
|
before do
|
1000
1008
|
group_owner = create(:notification, target: test_instance, group_owner: nil, opened_at: Time.current)
|
1001
1009
|
create(:notification, target: test_instance, group_owner: nil, opened_at: Time.current)
|
1002
1010
|
group_member = create(:notification, target: test_instance, group_owner: group_owner, opened_at: Time.current)
|
1003
1011
|
end
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1012
|
+
|
1013
|
+
if ActivityNotification.config.orm == :active_record
|
1014
|
+
it "calls with_group" do
|
1015
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:target)
|
1016
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifiable)
|
1017
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:group)
|
1018
|
+
expect(ActiveRecord::Base).to receive(:includes).with(:notifier)
|
1019
|
+
test_instance.opened_notification_index_with_attributes
|
1020
|
+
end
|
1011
1021
|
end
|
1012
1022
|
end
|
1013
1023
|
end
|