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 %>

Open

<% 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 %>

Open

<% 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

- <%= link_to "Open all", open_all_notifications_path_for(@target, parameters), method: :post, remote: true %> + <%= link_to "Open all", open_all_notifications_path_for(@target, parameters.slice(:routing_scope, :devise_default_routes)), method: :post, remote: true %> <% if @target.class.subscription_enabled? %> - <%= link_to "Subscriptions", subscriptions_path_for(@target) %> + <%= link_to "Subscriptions", subscriptions_path_for(@target, parameters.slice(:routing_scope, :devise_default_routes)) %> <% end %>

<%= yield :notification_index %>
- <%= link_to notifications_path_for(@target) do %> + <%= link_to notifications_path_for(@target, parameters.slice(:routing_scope, :devise_default_routes)) do %>