From 529cc9c90080e31cad7e2a599cebffee44f78320 Mon Sep 17 00:00:00 2001
From: simukappu
Date: Sun, 11 Nov 2018 13:14:00 +0900
Subject: [PATCH] Add simple default routes with devise integration - #64
---
README.md | 50 +++++-
.../notifications_controller.rb | 2 +-
.../subscriptions_controller.rb | 3 +-
.../notifications/default/_default.html.erb | 20 +--
.../_default_without_grouping.html.erb | 20 +--
.../notifications/default/_index.html.erb | 6 +-
.../notifications/default/index.html.erb | 14 +-
.../notifications/default/open.js.erb | 4 +-
.../notifications/default/open_all.js.erb | 4 +-
.../notifications/default/show.html.erb | 4 +-
.../default/base/_default.text.erb | 2 +-
.../default/slack/_default.text.erb | 2 +-
.../default/_notification_keys.html.erb | 4 +-
.../default/_subscription.html.erb | 10 +-
.../subscriptions/default/show.html.erb | 2 +-
.../subscriptions/default/subscribe.js.erb | 2 +-
.../default/subscribe_to_email.js.erb | 2 +-
.../subscribe_to_optional_target.js.erb | 2 +-
.../subscriptions/default/unsubscribe.js.erb | 2 +-
.../default/unsubscribe_to_email.js.erb | 2 +-
.../unsubscribe_to_optional_target.js.erb | 2 +-
.../devise_authentication_controller.rb | 16 ++
.../helpers/view_helpers.rb | 140 +++++++++++++---
.../models/concerns/target.rb | 11 ++
lib/activity_notification/rails/routes.rb | 157 +++++++++++++-----
.../roles/acts_as_target.rb | 53 +++++-
...tifications_with_devise_controller_spec.rb | 15 ++
...bscriptions_with_devise_controller_spec.rb | 15 ++
spec/helpers/view_helpers_spec.rb | 2 +-
.../app/controllers/articles_controller.rb | 2 +
spec/rails_app/app/models/admin.rb | 2 +
.../amazon_sns/comment/_default.text.erb | 2 +-
.../app/views/articles/index.html.erb | 68 ++++----
.../app/views/layouts/_header.html.erb | 10 +-
spec/rails_app/config/routes.rb | 5 +
35 files changed, 490 insertions(+), 167 deletions(-)
diff --git a/README.md b/README.md
index d5085f90..18c2b4b6 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,7 @@
- [Advanced notifiable path](#advanced-notifiable-path)
- [Configuring views](#configuring-views)
- [Configuring routes](#configuring-routes)
+ - [Routes with scope](#routes-with-scope)
- [Creating notifications](#creating-notifications)
- [Notification API](#notification-api)
- [Automatic tracked notifications](#automatic-tracked-notifications)
@@ -83,8 +84,9 @@
- [Managing subscriptions](#managing-subscriptions)
- [Customizing subscriptions](#customizing-subscriptions)
- [Integration with Devise](#integration-with-devise)
+ - [Using different model as target](#using-different-model-as-target)
+ - [Configuring simple default routes](#configuring-simple-default-routes)
- [Optional notification targets](#optional-notification-targets)
- - [Configuring optional targets](#configuring-optional-targets)
- [Customizing message format](#customizing-message-format)
- [Amazon SNS as optional target](#amazon-sns-as-optional-target)
- [Slack as optional target](#slack-as-optional-target)
@@ -318,7 +320,22 @@ Rails.application.routes.draw do
end
```
-Then, you can access several pages like *users/1/notifications* and manage open/unopen of notifications using **notifications_controller**.
+Then, you can access several pages like */users/1/notifications* and manage open/unopen of notifications using **notifications_controller**.
+If you use Devise integration and you want to configure simple default routes for authenticated users, see [Configuring simple default routes](#configuring-simple-default-routes).
+
+#### Routes with scope
+
+You can also configure *activity_notification* routes with scope like this:
+
+```ruby
+Rails.application.routes.draw do
+ scope :myscope, as: :myscope do
+ notify_to :users, routing_scope: :myscope
+ end
+end
+```
+
+Then, pages are shown as */myscope/users/1/notifications*.
### Creating notifications
@@ -928,6 +945,8 @@ Then *activity_notification* will use **notifications_with_devise_controller** a
*Hint*: HTTP 403 Forbidden will be returned for unauthorized notifications.
+#### Using different model as target
+
You can also use different model from Devise resource as a target. When you will add this to *config/routes.rb*:
```ruby
@@ -948,7 +967,32 @@ end
```
*activity_notification* will authenticate *:admins* notifications with devise authentication for *:users*.
-In this example *activity_notification* will confirm the *user* who *admin* belongs to with authenticated user by devise.
+In this example, *activity_notification* will confirm *admin* belonging to authenticated *user* by Devise.
+
+#### Configuring simple default routes
+
+You can configure simple default routes for authenticated users, like */notifications* instead of */users/1/notifications*. Use *:devise_default_routes* option like this:
+
+```ruby
+Rails.application.routes.draw do
+ devise_for :users
+ notify_to :users, with_devise: :users, devise_default_routes: true
+end
+```
+
+If you use multiple notification targets with Devise, you can also use this option with scope like this:
+
+```ruby
+Rails.application.routes.draw do
+ devise_for :users
+ # Integrated with devise for different model, and use with scope
+ scope :admins, as: :admins do
+ notify_to :admins, with_devise: :users, devise_default_routes: true, routing_scope: :admins
+ end
+end
+```
+
+Then, you can access */admins/notifications* instead of */admins/1/notifications*.
### Optional notification targets
diff --git a/app/controllers/activity_notification/notifications_controller.rb b/app/controllers/activity_notification/notifications_controller.rb
index 3e880a58..5ac24fc1 100644
--- a/app/controllers/activity_notification/notifications_controller.rb
+++ b/app/controllers/activity_notification/notifications_controller.rb
@@ -135,7 +135,7 @@ def set_index_options
params[:reverse].to_s.to_boolean(false) : nil
with_group_members = params[:with_group_members].present? || params[:without_grouping].present? ?
params[:with_group_members].to_s.to_boolean(false) || params[:without_grouping].to_s.to_boolean(false) : nil
- @index_options = params.permit(:filter, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key)
+ @index_options = params.permit(:filter, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key, :devise_default_routes, :routing_scope)
.to_h.symbolize_keys
.merge(limit: limit, reverse: reverse, with_group_members: with_group_members)
end
diff --git a/app/controllers/activity_notification/subscriptions_controller.rb b/app/controllers/activity_notification/subscriptions_controller.rb
index dbb13ef2..0742d8f8 100644
--- a/app/controllers/activity_notification/subscriptions_controller.rb
+++ b/app/controllers/activity_notification/subscriptions_controller.rb
@@ -52,6 +52,7 @@ def create
# @param [Hash] params Request parameters
# @return [Responce] HTML view as default
def show
+ set_index_options
end
# Deletes a subscription.
@@ -190,7 +191,7 @@ def set_index_options
limit = params[:limit].to_i > 0 ? params[:limit].to_i : nil
reverse = params[:reverse].present? ?
params[:reverse].to_s.to_boolean(false) : nil
- @index_options = params.permit(:filter, :filtered_by_key)
+ @index_options = params.permit(:filter, :filtered_by_key, :devise_default_routes, :routing_scope)
.to_h.symbolize_keys.merge(limit: limit, reverse: reverse)
end
diff --git a/app/views/activity_notification/notifications/default/_default.html.erb b/app/views/activity_notification/notifications/default/_default.html.erb
index 40677db0..9ef8d074 100644
--- a/app/views/activity_notification/notifications/default/_default.html.erb
+++ b/app/views/activity_notification/notifications/default/_default.html.erb
@@ -34,31 +34,31 @@
<% if notification.unopened? %>
- <%= link_to open_notification_path_for(notification, reload: false), method: :post, remote: true, class: "unopened_wrapper" do %>
+ <%= link_to open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(reload: false)), method: :post, remote: true, class: "unopened_wrapper" do %>
<% end %>
- <%= link_to open_notification_path_for(notification, move: true), method: :post do %>
+ <%= link_to open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(move: true)), method: :post do %>
<%= yield :notification_content %>
<% end %>
<% else %>
- <%= link_to move_notification_path_for(notification) do %>
+ <%= link_to move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes)) do %>
<%= yield :notification_content %>
<% end %>
<% end %>
- <%#= link_to "Move", move_notification_path_for(notification) %>
+ <%#= link_to "Move", move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes)) %>
<%# if notification.unopened? %>
- <%#= link_to "Open and move (GET)", move_notification_path_for(notification, open: true) %>
- <%#= link_to "Open and move (POST)", open_notification_path_for(notification, move: true), method: :post %>
- <%#= link_to "Open", open_notification_path_for(notification, index_options: @index_options), method: :post %>
- <%#= link_to "Open (Ajax)", open_notification_path_for(notification, reload: false), method: :post, remote: true %>
+ <%#= link_to "Open and move (GET)", move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(open: true)) %>
+ <%#= link_to "Open and move (POST)", open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(move: true)), method: :post %>
+ <%#= link_to "Open", open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(index_options: @index_options))), method: :post %>
+ <%#= link_to "Open (Ajax)", open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(reload: false)), method: :post, remote: true %>
<%# end %>
- <%#= link_to "Destroy", notification_path_for(notification, index_options: @index_options), method: :delete %>
- <%#= link_to "Destroy (Ajax)", notification_path_for(notification, reload: false), method: :delete, remote: true %>
+ <%#= link_to "Destroy", notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(index_options: @index_options)), method: :delete %>
+ <%#= link_to "Destroy (Ajax)", notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(reload: false)), method: :delete, remote: true %>
diff --git a/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb b/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb
index 6dda8d3d..04999825 100644
--- a/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb
+++ b/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb
@@ -23,31 +23,31 @@
<% if notification.unopened? %>
- <%= link_to open_notification_path_for(notification, reload: false, without_grouping: parameters[:with_group_members]), method: :post, remote: true, class: "unopened_wrapper" do %>
+ <%= link_to open_notification_path_for(notification, parameters.slice(:with_group_members, :routing_scope, :devise_default_routes).merge(reload: false), method: :post, remote: true, class: "unopened_wrapper" do %>
<% end %>
- <%= link_to open_notification_path_for(notification, move: true), method: :post do %>
+ <%= link_to open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(move: true)), method: :post do %>
<%= yield :notification_content %>
<% end %>
<% else %>
- <%= link_to move_notification_path_for(notification) do %>
+ <%= link_to move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes)) do %>
<%= yield :notification_content %>
<% end %>
<% end %>
- <%#= link_to "Move", move_notification_path_for(notification) %>
+ <%#= link_to "Move", move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes)) %>
<%# if notification.unopened? %>
- <%#= link_to "Open and move (GET)", move_notification_path_for(notification, open: true) %>
- <%#= link_to "Open and move (POST)", open_notification_path_for(notification, move: true), method: :post %>
- <%#= link_to "Open", open_notification_path_for(notification, index_options: @index_options), method: :post %>
- <%#= link_to "Open (Ajax)", open_notification_path_for(notification, reload: false, with_group_members: parameters[:with_group_members]), method: :post, remote: true %>
+ <%#= link_to "Open and move (GET)", move_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(open: true)) %>
+ <%#= link_to "Open and move (POST)", open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(move: true)), method: :post %>
+ <%#= link_to "Open", open_notification_path_for(notification, parameters.slice(:routing_scope, :devise_default_routes).merge(index_options: @index_options)), method: :post %>
+ <%#= link_to "Open (Ajax)", open_notification_path_for(notification, parameters.slice(with_group_members:, :routing_scope, :devise_default_routes).merge(reload: false)), method: :post, remote: true %>
<%# end %>
- <%#= link_to "Destroy", notification_path_for(notification, index_options: @index_options), method: :delete %>
- <%#= link_to "Destroy (Ajax)", notification_path_for(notification, reload: false, with_group_members: parameters[:with_group_members]), method: :delete, remote: true %>
+ <%#= link_to "Destroy", notification_path_for(notification, index_options: parameters.slice(:routing_scope, :devise_default_routes).merge(index_options: @index_options)), method: :delete %>
+ <%#= link_to "Destroy (Ajax)", notification_path_for(notification, parameters.slice(with_group_members:, :routing_scope, :devise_default_routes).merge(reload: false)), method: :delete, remote: true %>
diff --git a/app/views/activity_notification/notifications/default/_index.html.erb b/app/views/activity_notification/notifications/default/_index.html.erb
index 13d05832..a565bf86 100644
--- a/app/views/activity_notification/notifications/default/_index.html.erb
+++ b/app/views/activity_notification/notifications/default/_index.html.erb
@@ -12,16 +12,16 @@
Notifications
<%= yield :notification_index %>
- <%= link_to notifications_path_for(@target) do %>
+ <%= link_to notifications_path_for(@target, parameters.slice(:routing_scope, :devise_default_routes)) do %>
See notifications
diff --git a/app/views/activity_notification/notifications/default/index.html.erb b/app/views/activity_notification/notifications/default/index.html.erb
index cbbcd3c5..2e7317fb 100644
--- a/app/views/activity_notification/notifications/default/index.html.erb
+++ b/app/views/activity_notification/notifications/default/index.html.erb
@@ -1,20 +1,20 @@
<% if @index_options[:with_group_members] %>
- <%= render_notification @notifications, fallback: :default_without_grouping, with_group_members: true %>
+ <%= render_notification @notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default_without_grouping, with_group_members: true) %>
<% else %>
- <%= render_notification @notifications, fallback: :default %>
- <%#= render_notification @notifications, fallback: :text %>
+ <%= render_notification @notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default) %>
+ <%#= render_notification @notifications, @index_options.merge(fallback: :text) %>
<% end %>
-<%#= render_notifications_of @target, fallback: :default, index_content: :with_attributes %>
-<%#= render_notifications_of @target, fallback: :default, index_content: :unopened_with_attributes, reverse: true %>
-<%#= render_notifications_of @target, fallback: :default_without_grouping, index_content: :with_attributes, with_group_members: true %>
+<%#= render_notifications_of @target, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default, index_content: :with_attributes) %>
+<%#= render_notifications_of @target, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default, index_content: :unopened_with_attributes, reverse: true) %>
+<%#= render_notifications_of @target, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default_without_grouping, index_content: :with_attributes, with_group_members: true) %>