activity_notification 1.5.1 → 1.6.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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/README.md +51 -3
  4. data/app/controllers/activity_notification/notifications_controller.rb +1 -1
  5. data/app/controllers/activity_notification/subscriptions_controller.rb +2 -1
  6. data/app/views/activity_notification/notifications/default/_default.html.erb +10 -10
  7. data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +10 -10
  8. data/app/views/activity_notification/notifications/default/_index.html.erb +3 -3
  9. data/app/views/activity_notification/notifications/default/index.html.erb +7 -7
  10. data/app/views/activity_notification/notifications/default/open.js.erb +2 -2
  11. data/app/views/activity_notification/notifications/default/open_all.js.erb +2 -2
  12. data/app/views/activity_notification/notifications/default/show.html.erb +2 -2
  13. data/app/views/activity_notification/optional_targets/default/base/_default.text.erb +1 -1
  14. data/app/views/activity_notification/optional_targets/default/slack/_default.text.erb +1 -1
  15. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +2 -2
  16. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +5 -5
  17. data/app/views/activity_notification/subscriptions/default/show.html.erb +1 -1
  18. data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +1 -1
  19. data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +1 -1
  20. data/app/views/activity_notification/subscriptions/default/subscribe_to_optional_target.js.erb +1 -1
  21. data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +1 -1
  22. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +1 -1
  23. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_optional_target.js.erb +1 -1
  24. data/lib/activity_notification/apis/subscription_api.rb +2 -2
  25. data/lib/activity_notification/controllers/devise_authentication_controller.rb +16 -0
  26. data/lib/activity_notification/helpers/view_helpers.rb +113 -27
  27. data/lib/activity_notification/models/concerns/subscriber.rb +2 -1
  28. data/lib/activity_notification/models/concerns/target.rb +11 -0
  29. data/lib/activity_notification/rails/routes.rb +169 -40
  30. data/lib/activity_notification/roles/acts_as_target.rb +45 -8
  31. data/lib/activity_notification/version.rb +1 -1
  32. data/spec/controllers/notifications_with_devise_controller_spec.rb +15 -0
  33. data/spec/controllers/subscriptions_controller_shared_examples.rb +4 -3
  34. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +15 -0
  35. data/spec/helpers/view_helpers_spec.rb +1 -1
  36. data/spec/rails_app/app/controllers/articles_controller.rb +2 -0
  37. data/spec/rails_app/app/models/admin.rb +2 -0
  38. data/spec/rails_app/app/views/activity_notification/optional_targets/admins/amazon_sns/comment/_default.text.erb +1 -1
  39. data/spec/rails_app/app/views/articles/index.html.erb +36 -32
  40. data/spec/rails_app/app/views/layouts/_header.html.erb +9 -1
  41. data/spec/rails_app/config/routes.rb +5 -0
  42. metadata +2 -2
@@ -37,7 +37,7 @@ module ActivityNotification
37
37
  # * :devise_resource
38
38
  # * Integrated resource with devise authentication.
39
39
  # This parameter is a optional since `self` is used as default value.
40
- # You also have to configure routing for devise inroutes.rb
40
+ # You also have to configure routing for devise in routes.rb
41
41
  # @example No :devise_resource is needed when notification target is the same as authenticated resource
42
42
  # # config/routes.rb
43
43
  # devise_for :users
@@ -68,6 +68,42 @@ module ActivityNotification
68
68
  # devise_resource: :user
69
69
  # end
70
70
  #
71
+ # * :current_devise_target
72
+ # * Current authenticated target by devise authentication.
73
+ # This parameter is a optional since `current_<devise_resource_name>` is used as default value.
74
+ # In addition, this parameter is only needed when :devise_default_route in your route.rb is enabled.
75
+ # You also have to configure routing for devise in routes.rb
76
+ # @example No :current_devise_target is needed when notification target is the same as authenticated resource
77
+ # # config/routes.rb
78
+ # devise_for :users
79
+ # notify_to :users
80
+ #
81
+ # # app/models/user.rb
82
+ # class User < ActiveRecord::Base
83
+ # devise :database_authenticatable, :registerable, :confirmable
84
+ # acts_as_target email: :email, email_allowed: :confirmed_at
85
+ # end
86
+ #
87
+ # @example Send Admin model and use associated User model with devise authentication
88
+ # # config/routes.rb
89
+ # devise_for :users
90
+ # notify_to :admins, with_devise: :users
91
+ #
92
+ # # app/models/user.rb
93
+ # class User < ActiveRecord::Base
94
+ # devise :database_authenticatable, :registerable, :confirmable
95
+ # end
96
+ #
97
+ # # app/models/admin.rb
98
+ # class Admin < ActiveRecord::Base
99
+ # belongs_to :user
100
+ # validates :user, presence: true
101
+ # acts_as_notification_target email: :email,
102
+ # email_allowed: ->(admin, key) { admin.user.confirmed_at.present? },
103
+ # devise_resource: :user,
104
+ # current_devise_target: ->(current_user) { current_user.admin }
105
+ # end
106
+ #
71
107
  # * :printable_name or :printable_notification_target_name
72
108
  # * Printable notification target name.
73
109
  # This parameter is a optional since `ActivityNotification::Common.printable_name` is used as default value.
@@ -85,19 +121,20 @@ module ActivityNotification
85
121
  # end
86
122
  #
87
123
  # @param [Hash] options Options for notifiable model configuration
88
- # @option options [Symbol, Proc, String] :email (nil) Email address to send notification email
89
- # @option options [Symbol, Proc, Boolean] :email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends notification email to this target
90
- # @option options [Symbol, Proc, Boolean] :batch_email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends batch notification email to this target
91
- # @option options [Symbol, Proc, Boolean] :subscription_allowed (ActivityNotification.config.subscription_enabled) Whether activity_notification manages subscriptions of this target
92
- # @option options [Symbol, Proc, Object] :devise_resource (nil) Integrated resource with devise authentication
93
- # @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notification target name
124
+ # @option options [Symbol, Proc, String] :email (nil) Email address to send notification email
125
+ # @option options [Symbol, Proc, Boolean] :email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends notification email to this target
126
+ # @option options [Symbol, Proc, Boolean] :batch_email_allowed (ActivityNotification.config.email_enabled) Whether activity_notification sends batch notification email to this target
127
+ # @option options [Symbol, Proc, Boolean] :subscription_allowed (ActivityNotification.config.subscription_enabled) Whether activity_notification manages subscriptions of this target
128
+ # @option options [Symbol, Proc, Object] :devise_resource (->(model) { model }) Integrated resource with devise authentication
129
+ # @option options [Symbol, Proc, Object] :current_devise_target (->(current_resource) { current_resource }) Current authenticated target by devise authentication
130
+ # @option options [Symbol, Proc, String] :printable_name (ActivityNotification::Common.printable_name) Printable notification target name
94
131
  # @return [Hash] Configured parameters as target model
95
132
  def acts_as_target(options = {})
96
133
  include Target
97
134
 
98
135
  options[:printable_notification_target_name] ||= options.delete(:printable_name)
99
136
  options[:batch_notification_email_allowed] ||= options.delete(:batch_email_allowed)
100
- acts_as_params = set_acts_as_parameters([:email, :email_allowed, :subscription_allowed, :devise_resource], options, "notification_")
137
+ acts_as_params = set_acts_as_parameters([:email, :email_allowed, :subscription_allowed, :devise_resource, :current_devise_target], options, "notification_")
101
138
  .merge set_acts_as_parameters([:batch_notification_email_allowed, :printable_notification_target_name], options)
102
139
  include Subscriber if subscription_enabled?
103
140
  acts_as_params
@@ -1,3 +1,3 @@
1
1
  module ActivityNotification
2
- VERSION = "1.5.1"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -78,6 +78,21 @@ describe ActivityNotification::NotificationsWithDeviseController, type: :control
78
78
  end
79
79
  end
80
80
 
81
+ context "without target_id and (typed_target)_id parameters for devise integrated controller with devise_type option" do
82
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
83
+
84
+ describe "GET #index" do
85
+ before do
86
+ sign_in test_target.user
87
+ get_with_compatibility :index, target_params, valid_session
88
+ end
89
+
90
+ it "returns 200 as http status code" do
91
+ expect(response.status).to eq(200)
92
+ end
93
+ end
94
+ end
95
+
81
96
  private
82
97
 
83
98
  def get_with_compatibility action, params, session
@@ -273,9 +273,10 @@ shared_examples_for :subscription_controller do
273
273
 
274
274
  it "creates new subscription of the target" do
275
275
  expect(test_target.subscriptions.reload.size).to eq(1)
276
- expect(test_target.subscriptions.reload.first.key).to eq("new_subscription_key")
277
- expect(test_target.subscriptions.reload.first.subscribing_to_optional_target?("base1")).to be_truthy
278
- expect(test_target.subscriptions.reload.first.subscribing_to_optional_target?("base2")).to be_falsey
276
+ created_subscription = test_target.subscriptions.reload.first
277
+ expect(created_subscription.key).to eq("new_subscription_key")
278
+ expect(created_subscription.subscribing_to_optional_target?("base1")).to be_truthy
279
+ expect(created_subscription.subscribing_to_optional_target?("base2")).to be_falsey
279
280
  end
280
281
 
281
282
  it "redirects to :index" do
@@ -78,6 +78,21 @@ describe ActivityNotification::SubscriptionsWithDeviseController, type: :control
78
78
  end
79
79
  end
80
80
 
81
+ context "without target_id and (typed_target)_id parameters for devise integrated controller with devise_type option" do
82
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
83
+
84
+ describe "GET #index" do
85
+ before do
86
+ sign_in test_target.user
87
+ get_with_compatibility :index, target_params, valid_session
88
+ end
89
+
90
+ it "returns 200 as http status code" do
91
+ expect(response.status).to eq(200)
92
+ end
93
+ end
94
+ end
95
+
81
96
  private
82
97
 
83
98
  def get_with_compatibility action, params, session
@@ -42,7 +42,7 @@ describe ActivityNotification::ViewHelpers, type: :helper do
42
42
  expect(render_notification notification, fallback: :default)
43
43
  .to eq(
44
44
  render partial: 'activity_notification/notifications/default/default',
45
- locals: { notification: notification }
45
+ locals: { notification: notification, parameters: {} }
46
46
  )
47
47
  end
48
48
 
@@ -4,6 +4,8 @@ class ArticlesController < ApplicationController
4
4
 
5
5
  # GET /articles
6
6
  def index
7
+ @exists_user_notification_routes = respond_to?('user_notification_path')
8
+ @exists_admin_notification_routes = respond_to?('admin_notification_path')
7
9
  @articles = Article.all.includes(:user)
8
10
  end
9
11
 
@@ -6,6 +6,7 @@ unless ENV['AN_TEST_DB'] == 'mongodb'
6
6
  acts_as_notification_target email_allowed: false,
7
7
  subscription_allowed: true,
8
8
  devise_resource: :user,
9
+ current_devise_target: ->(current_user) { current_user.admin },
9
10
  printable_name: ->(admin) { "admin (#{admin.user.name})" }
10
11
  end
11
12
  else
@@ -25,6 +26,7 @@ else
25
26
  acts_as_notification_target email_allowed: false,
26
27
  subscription_allowed: true,
27
28
  devise_resource: :user,
29
+ current_devise_target: ->(current_user) { current_user.admin },
28
30
  printable_name: ->(admin) { "admin (#{admin.user.name})" }
29
31
  end
30
32
  end
@@ -5,6 +5,6 @@ Dear <%= @target.printable_target_name %>
5
5
  <%= @notification.notifier.present? ? @notification.notifier.printable_notifier_name : 'Someone' %> notified you of <%= @notification.notifiable.printable_notifiable_name(@notification.target) %><%= " in #{@notification.group.printable_group_name}" if @notification.group.present? %>.
6
6
 
7
7
  <%= "Move to notified #{@notification.notifiable.printable_type.downcase}:" %>
8
- <%= move_notification_url_for(@notification, open: true) %>
8
+ <%= move_notification_url_for(@notification, parameters.slice(:routing_scope, :devise_default_routes).merge(open: true)) %>
9
9
 
10
10
  Thank you!
@@ -1,38 +1,42 @@
1
- <section>
2
- <h1>Listing Users</h1>
3
- <% User.all.each do |user| %>
4
- <div class="list_wrapper">
5
- <div class="list_image"></div>
6
- <div class="list_description_wrapper">
7
- <p class="list_description">
8
- <span><%= user.name %></span> · <%= user.email %><br>
9
- <%= link_to 'Notifications', user_notifications_path(user) %>
10
- <% if User.subscription_enabled? %>
11
- <%= link_to 'Subscriptions', user_subscriptions_path(user) %>
12
- <% end %>
13
- </p>
1
+ <% if @exists_user_notification_routes %>
2
+ <section>
3
+ <h1>Listing Users</h1>
4
+ <% User.all.each do |user| %>
5
+ <div class="list_wrapper">
6
+ <div class="list_image"></div>
7
+ <div class="list_description_wrapper">
8
+ <p class="list_description">
9
+ <span><%= user.name %></span> · <%= user.email %><br>
10
+ <%= link_to 'Notifications', user_notifications_path(user) %>
11
+ <% if User.subscription_enabled? %>
12
+ <%= link_to 'Subscriptions', user_subscriptions_path(user) %>
13
+ <% end %>
14
+ </p>
15
+ </div>
14
16
  </div>
15
- </div>
16
- <% end %>
17
- </section>
17
+ <% end %>
18
+ </section>
19
+ <% end %>
18
20
 
19
- <section>
20
- <h1>Listing Admins</h1>
21
- <% Admin.all.each do |admin| %>
22
- <div class="list_wrapper">
23
- <div class="list_image"></div>
24
- <div class="list_description_wrapper">
25
- <p class="list_description">
26
- <span><%= admin.user.name %></span> · <%= admin.user.email %><br>
27
- <%= link_to 'Notifications', admin_notifications_path(admin) %>
28
- <% if Admin.subscription_enabled? %>
29
- <%= link_to 'Subscriptions', admin_subscriptions_path(admin) %>
30
- <% end %>
31
- </p>
21
+ <% if @exists_admin_notification_routes %>
22
+ <section>
23
+ <h1>Listing Admins</h1>
24
+ <% Admin.all.each do |admin| %>
25
+ <div class="list_wrapper">
26
+ <div class="list_image"></div>
27
+ <div class="list_description_wrapper">
28
+ <p class="list_description">
29
+ <span><%= admin.user.name %></span> · <%= admin.user.email %><br>
30
+ <%= link_to 'Notifications', admin_notifications_path(admin) %>
31
+ <% if Admin.subscription_enabled? %>
32
+ <%= link_to 'Subscriptions', admin_subscriptions_path(admin) %>
33
+ <% end %>
34
+ </p>
35
+ </div>
32
36
  </div>
33
- </div>
34
- <% end %>
35
- </section>
37
+ <% end %>
38
+ </section>
39
+ <% end %>
36
40
 
37
41
  <section>
38
42
  <div class="create_button_wrapper">
@@ -24,12 +24,20 @@
24
24
  </div>
25
25
  <div class="header_notification_wrapper">
26
26
  <% if user_signed_in? %>
27
- <%= render_notifications_of current_user, fallback: :default, index_content: :with_attributes %>
27
+ <%= render_notifications_of current_user, fallback: :default, index_content: :with_attributes, devise_default_routes: respond_to?('notifications_path') %>
28
28
  <%#= render_notifications_of current_user, fallback: :default, index_content: :unopened_with_attributes, reverse: true %>
29
29
  <%#= render_notifications_of current_user, fallback: :default, index_content: :with_attributes, as_latest_group_member: true %>
30
30
  <%#= render_notifications_of current_user, fallback: :default_without_grouping, index_content: :with_attributes, with_group_members: true %>
31
31
  <% end %>
32
32
  </div>
33
+ <% if user_signed_in? and current_user.admin? and respond_to?('admins_notifications_path') %>
34
+ <div class="header_menu_wrapper">
35
+ <p>
36
+ <%= link_to "Admin notifications", admins_notifications_path %> /
37
+ <%= link_to "subscriptions", admins_subscriptions_path %>
38
+ </p>
39
+ </div>
40
+ <% end %>
33
41
  <div class="header_menu_wrapper">
34
42
  <p>
35
43
  <%= link_to 'Preview email', "/rails/mailers" %>
@@ -5,5 +5,10 @@ Rails.application.routes.draw do
5
5
  resources :comments, only: [:create, :destroy]
6
6
 
7
7
  notify_to :users, with_subscription: true
8
+ notify_to :users, with_devise: :users, devise_default_routes: true, with_subscription: true
9
+
8
10
  notify_to :admins, with_devise: :users, with_subscription: true
11
+ scope :admins, as: :admins do
12
+ notify_to :admins, with_devise: :users, devise_default_routes: true, with_subscription: true, routing_scope: :admins
13
+ end
9
14
  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.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Yamazaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-25 00:00:00.000000000 Z
11
+ date: 2018-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties