activity_notification 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +3 -3
  3. data/README.md +49 -9
  4. data/activity_notification.gemspec +1 -1
  5. data/app/controllers/activity_notification/notifications_controller.rb +75 -31
  6. data/app/mailers/activity_notification/mailer.rb +19 -4
  7. data/app/views/activity_notification/mailer/default/batch_default.html.erb +79 -0
  8. data/app/views/activity_notification/mailer/default/batch_default.text.erb +13 -0
  9. data/app/views/activity_notification/mailer/default/default.html.erb +75 -10
  10. data/app/views/activity_notification/mailer/default/default.text.erb +2 -2
  11. data/app/views/activity_notification/notifications/default/_default.html.erb +15 -14
  12. data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +165 -0
  13. data/app/views/activity_notification/notifications/default/_index.html.erb +8 -4
  14. data/app/views/activity_notification/notifications/default/destroy.js.erb +2 -2
  15. data/app/views/activity_notification/notifications/default/index.html.erb +9 -5
  16. data/app/views/activity_notification/notifications/default/open.js.erb +6 -2
  17. data/app/views/activity_notification/notifications/default/open_all.js.erb +6 -2
  18. data/lib/activity_notification/apis/notification_api.rb +42 -9
  19. data/lib/activity_notification/helpers/view_helpers.rb +48 -19
  20. data/lib/activity_notification/mailers/helpers.rb +74 -37
  21. data/lib/activity_notification/models/concerns/target.rb +290 -26
  22. data/lib/activity_notification/models/notification.rb +85 -29
  23. data/lib/activity_notification/roles/acts_as_target.rb +4 -2
  24. data/lib/activity_notification/version.rb +1 -1
  25. data/spec/concerns/apis/notification_api_spec.rb +46 -0
  26. data/spec/concerns/models/target_spec.rb +281 -22
  27. data/spec/controllers/notifications_controller_shared_examples.rb +77 -0
  28. data/spec/helpers/view_helpers_spec.rb +39 -3
  29. data/spec/mailers/mailer_spec.rb +54 -1
  30. data/spec/models/notification_spec.rb +11 -0
  31. data/spec/rails_app/app/models/user.rb +1 -1
  32. data/spec/rails_app/app/views/layouts/_header.html.erb +2 -0
  33. data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +6 -0
  34. data/spec/roles/acts_as_target_spec.rb +1 -1
  35. metadata +7 -4
@@ -149,6 +149,83 @@ shared_examples_for :notification_controller do
149
149
  end
150
150
  end
151
151
  end
152
+
153
+ context "with reverse parameter" do
154
+ before do
155
+ @notifiable = create(:article)
156
+ @group = create(:article)
157
+ @key = 'test.key.1'
158
+ create(:notification, target: test_target, notifiable: @notifiable)
159
+ create(:notification, target: test_target, notifiable: create(:comment), group: @group)
160
+ create(:notification, target: test_target, notifiable: create(:article), key: @key).open!
161
+ @notification1 = test_target.notification_index[0]
162
+ @notification2 = test_target.notification_index[1]
163
+ @notification3 = test_target.notification_index[2]
164
+ end
165
+
166
+ context "as default" do
167
+ before do
168
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target }), valid_session
169
+ end
170
+
171
+ it "returns the latest order" do
172
+ expect(assigns(:notifications)[0]).to eq(@notification1)
173
+ expect(assigns(:notifications)[1]).to eq(@notification2)
174
+ expect(assigns(:notifications)[2]).to eq(@notification3)
175
+ expect(assigns(:notifications).size).to eq(3)
176
+ end
177
+ end
178
+
179
+ context "with true as reverse" do
180
+ before do
181
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, reverse: true }), valid_session
182
+ end
183
+
184
+ it "returns the earliest order" do
185
+ expect(assigns(:notifications)[0]).to eq(@notification2)
186
+ expect(assigns(:notifications)[1]).to eq(@notification1)
187
+ expect(assigns(:notifications)[2]).to eq(@notification3)
188
+ expect(assigns(:notifications).size).to eq(3)
189
+ end
190
+ end
191
+ end
192
+
193
+ context "with options filter parameters" do
194
+ before do
195
+ @notifiable = create(:article)
196
+ @group = create(:article)
197
+ @key = 'test.key.1'
198
+ @notification2 = create(:notification, target: test_target, notifiable: @notifiable)
199
+ @notification1 = create(:notification, target: test_target, notifiable: create(:comment), group: @group)
200
+ @notification3 = create(:notification, target: test_target, notifiable: create(:article), key: @key)
201
+ @notification3.open!
202
+ end
203
+
204
+ context 'with filtered_by_type parameter' do
205
+ it "returns filtered notifications only" do
206
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filtered_by_type: 'Article' }), valid_session
207
+ expect(assigns(:notifications)[0]).to eq(@notification2)
208
+ expect(assigns(:notifications)[1]).to eq(@notification3)
209
+ expect(assigns(:notifications).size).to eq(2)
210
+ end
211
+ end
212
+
213
+ context 'with filtered_by_group_type and :filtered_by_group_id parameters' do
214
+ it "returns filtered notifications only" do
215
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filtered_by_group_type: 'Article', filtered_by_group_id: @group.id.to_s }), valid_session
216
+ expect(assigns(:notifications)[0]).to eq(@notification1)
217
+ expect(assigns(:notifications).size).to eq(1)
218
+ end
219
+ end
220
+
221
+ context 'with filtered_by_key parameter' do
222
+ it "returns filtered notifications only" do
223
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filtered_by_key: @key }), valid_session
224
+ expect(assigns(:notifications)[0]).to eq(@notification3)
225
+ expect(assigns(:notifications).size).to eq(1)
226
+ end
227
+ end
228
+ end
152
229
  end
153
230
 
154
231
  describe "POST #open_all" do
@@ -122,7 +122,7 @@ describe ActivityNotification::ViewHelpers, type: :helper do
122
122
  expect(render_notification_of target_user, fallback: :default)
123
123
  .to eq(
124
124
  render partial: 'activity_notification/notifications/default/index',
125
- locals: { target: target_user }
125
+ locals: { target: target_user, parameters: { fallback: :default } }
126
126
  )
127
127
  end
128
128
  end
@@ -149,7 +149,7 @@ describe ActivityNotification::ViewHelpers, type: :helper do
149
149
  expect(self).to receive(:render).with({
150
150
  partial: 'activity_notification/notifications/users/index',
151
151
  layout: 'layouts/test',
152
- locals: { target: target_user }
152
+ locals: { target: target_user, parameters: {} }
153
153
  })
154
154
  render_notification_of target_user, layout: 'test'
155
155
  end
@@ -174,13 +174,41 @@ describe ActivityNotification::ViewHelpers, type: :helper do
174
174
  end
175
175
  end
176
176
 
177
- context "with :with_attributes or any other key" do
177
+ context "with :unopened_simple" do
178
+ it "uses target.unopened_notification_index" do
179
+ expect(target_user).to receive(:unopened_notification_index).at_least(:once)
180
+ render_notification_of target_user, index_content: :unopened_simple
181
+ end
182
+ end
183
+
184
+ context "with :opened_simple" do
185
+ it "uses target.opened_notification_index" do
186
+ expect(target_user).to receive(:opened_notification_index).at_least(:once)
187
+ render_notification_of target_user, index_content: :opened_simple
188
+ end
189
+ end
190
+
191
+ context "with :with_attributes" do
178
192
  it "uses target.notification_index_with_attributes" do
179
193
  expect(target_user).to receive(:notification_index_with_attributes)
180
194
  render_notification_of target_user, index_content: :with_attributes
181
195
  end
182
196
  end
183
197
 
198
+ context "with :unopened_with_attributes" do
199
+ it "uses target.unopened_notification_index_with_attributes" do
200
+ expect(target_user).to receive(:unopened_notification_index_with_attributes).at_least(:once)
201
+ render_notification_of target_user, index_content: :unopened_with_attributes
202
+ end
203
+ end
204
+
205
+ context "with :opened_with_attributes" do
206
+ it "uses target.opened_notification_index_with_attributes" do
207
+ expect(target_user).to receive(:opened_notification_index_with_attributes).at_least(:once)
208
+ render_notification_of target_user, index_content: :opened_with_attributes
209
+ end
210
+ end
211
+
184
212
  context "with :none" do
185
213
  it "uses neither target.notification_index nor notification_index_with_attributes" do
186
214
  expect(target_user).not_to receive(:notification_index)
@@ -188,6 +216,14 @@ describe ActivityNotification::ViewHelpers, type: :helper do
188
216
  render_notification_of target_user, index_content: :none
189
217
  end
190
218
  end
219
+
220
+ context "with any other key" do
221
+ it "uses target.notification_index_with_attributes" do
222
+ expect(target_user).to receive(:notification_index_with_attributes)
223
+ render_notification_of target_user, index_content: :hoge
224
+ end
225
+ end
226
+
191
227
  end
192
228
  end
193
229
 
@@ -1,6 +1,8 @@
1
1
  describe ActivityNotification::Mailer do
2
2
  include ActiveJob::TestHelper
3
3
  let(:notification) { create(:notification) }
4
+ let(:test_target) { notification.target }
5
+ let(:notifications) { [create(:notification, target: test_target), create(:notification, target: test_target)] }
4
6
 
5
7
  before do
6
8
  ActivityNotification::Mailer.deliveries.clear
@@ -56,11 +58,19 @@ describe ActivityNotification::Mailer do
56
58
  context "with email proc as ActivityNotification.config.mailer_sender" do
57
59
  it "sends from configured email as ActivityNotification.config.mailer_sender" do
58
60
  ActivityNotification.config.mailer_sender =
59
- ->(notification){ notification.target_type == 'User' ? "test03@example.com" : "test04@example.com" }
61
+ ->(key){ key == notification.key ? "test03@example.com" : "test04@example.com" }
60
62
  ActivityNotification::Mailer.send_notification_email(notification).deliver_now
61
63
  expect(ActivityNotification::Mailer.deliveries.last.from[0])
62
64
  .to eq("test03@example.com")
63
65
  end
66
+
67
+ it "sends from configured email as ActivityNotification.config.mailer_sender" do
68
+ ActivityNotification.config.mailer_sender =
69
+ ->(key){ key == 'hogehoge' ? "test03@example.com" : "test04@example.com" }
70
+ ActivityNotification::Mailer.send_notification_email(notification).deliver_now
71
+ expect(ActivityNotification::Mailer.deliveries.last.from[0])
72
+ .to eq("test04@example.com")
73
+ end
64
74
  end
65
75
 
66
76
  context "with defined overriding_notification_email_key in notifiable model" do
@@ -102,4 +112,47 @@ describe ActivityNotification::Mailer do
102
112
  end
103
113
  end
104
114
  end
115
+
116
+ describe ".send_batch_notification_email" do
117
+ context "with deliver_now" do
118
+ context "as default" do
119
+ before do
120
+ ActivityNotification::Mailer.send_batch_notification_email(test_target, notifications).deliver_now
121
+ end
122
+
123
+ it "sends batch notification email now" do
124
+ expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
125
+ end
126
+
127
+ it "sends to target email" do
128
+ expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(test_target.email)
129
+ end
130
+
131
+ end
132
+
133
+ context "when fallback option is :none and the template is missing" do
134
+ it "raise ActionView::MissingTemplate" do
135
+ expect { ActivityNotification::Mailer.send_batch_notification_email(test_target, notifications, fallback: :none).deliver_now }
136
+ .to raise_error(ActionView::MissingTemplate)
137
+ end
138
+ end
139
+ end
140
+
141
+ context "with deliver_later" do
142
+ it "sends notification email later" do
143
+ expect {
144
+ perform_enqueued_jobs do
145
+ ActivityNotification::Mailer.send_batch_notification_email(test_target, notifications).deliver_later
146
+ end
147
+ }.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
148
+ expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
149
+ end
150
+
151
+ it "sends notification email with active job queue" do
152
+ expect {
153
+ ActivityNotification::Mailer.send_batch_notification_email(test_target, notifications).deliver_later
154
+ }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
155
+ end
156
+ end
157
+ end
105
158
  end
@@ -265,6 +265,17 @@ describe ActivityNotification::Notification, type: :model do
265
265
  expect(notifications.first).to eq(@notification_2)
266
266
  end
267
267
  end
268
+
269
+ context 'with custom_filter options' do
270
+ it "works with filtered_by_options scope" do
271
+ notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: ["key = ?", @key_1] })
272
+ expect(notifications.size).to eq(1)
273
+ expect(notifications.first).to eq(@notification_1)
274
+ notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: { key: @key_2 } })
275
+ expect(notifications.size).to eq(1)
276
+ expect(notifications.first).to eq(@notification_2)
277
+ end
278
+ end
268
279
 
269
280
  context 'with no options' do
270
281
  it "works with filtered_by_options scope" do
@@ -4,7 +4,7 @@ class User < ActiveRecord::Base
4
4
  has_many :articles, dependent: :destroy
5
5
  has_one :admin, dependent: :destroy
6
6
 
7
- acts_as_target email: :email, email_allowed: :confirmed_at, printable_name: :name
7
+ acts_as_target email: :email, email_allowed: :confirmed_at, batch_email_allowed: :confirmed_at, printable_name: :name
8
8
  acts_as_notifier printable_name: :name
9
9
 
10
10
  def admin?
@@ -25,6 +25,8 @@
25
25
  <div class="header_notification_wrapper">
26
26
  <% if user_signed_in? %>
27
27
  <%= render_notifications_of current_user, fallback: :default, index_content: :with_attributes %>
28
+ <%#= render_notifications_of current_user, fallback: :default, index_content: :unopened_with_attributes, reverse: true %>
29
+ <%#= render_notifications_of current_user, fallback: :default_without_grouping, index_content: :with_attributes, with_group_members: true %>
28
30
  <% end %>
29
31
  </div>
30
32
  <div class="header_menu_wrapper">
@@ -10,4 +10,10 @@ class ActivityNotification::MailerPreview < ActionMailer::Preview
10
10
  ActivityNotification::Mailer.send_notification_email(target_notification)
11
11
  end
12
12
 
13
+ def send_batch_notification_email
14
+ target = User.find_by_name('Ichiro')
15
+ target_notifications = target.notification_index_with_attributes(filtered_by_key: 'comment.default')
16
+ ActivityNotification::Mailer.send_batch_notification_email(target, target_notifications)
17
+ end
18
+
13
19
  end
@@ -33,7 +33,7 @@ describe ActivityNotification::ActsAsTarget do
33
33
  describe ".available_target_options" do
34
34
  it "returns list of available options in acts_as_target" do
35
35
  expect(dummy_model_class.available_target_options)
36
- .to eq([:email, :email_allowed, :devise_resource, :printable_notification_target_name, :printable_name])
36
+ .to eq([:email, :email_allowed, :batch_email_allowed, :devise_resource, :printable_notification_target_name, :printable_name])
37
37
  end
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activity_notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Yamazaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-06 00:00:00.000000000 Z
11
+ date: 2016-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -78,14 +78,14 @@ dependencies:
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: 1.3.11
81
+ version: 1.3.12
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: 1.3.11
88
+ version: 1.3.12
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: rspec-rails
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -193,9 +193,12 @@ files:
193
193
  - app/controllers/activity_notification/notifications_controller.rb
194
194
  - app/controllers/activity_notification/notifications_with_devise_controller.rb
195
195
  - app/mailers/activity_notification/mailer.rb
196
+ - app/views/activity_notification/mailer/default/batch_default.html.erb
197
+ - app/views/activity_notification/mailer/default/batch_default.text.erb
196
198
  - app/views/activity_notification/mailer/default/default.html.erb
197
199
  - app/views/activity_notification/mailer/default/default.text.erb
198
200
  - app/views/activity_notification/notifications/default/_default.html.erb
201
+ - app/views/activity_notification/notifications/default/_default_without_grouping.html.erb
199
202
  - app/views/activity_notification/notifications/default/_index.html.erb
200
203
  - app/views/activity_notification/notifications/default/destroy.js.erb
201
204
  - app/views/activity_notification/notifications/default/index.html.erb