Skip to content

Commit

Permalink
Add simple default routes with devise integration - #64
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Nov 11, 2018
1 parent d9d9b5d commit 0a8699e
Show file tree
Hide file tree
Showing 35 changed files with 546 additions and 167 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -83,6 +84,9 @@
- [Managing subscriptions](#managing-subscriptions)
- [Customizing subscriptions](#customizing-subscriptions)
- [Integration with Devise](#integration-with-devise)
- [Configuring integration with Devise](#configuring-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)
Expand Down Expand Up @@ -318,7 +322,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

Expand Down Expand Up @@ -914,7 +933,9 @@ If you would like to customize subscription controllers or views, you can use ge

*activity_notification* supports to integrate with devise authentication.

First, add **:with_devise** option in notification routing to *config/routes.rb* for the target:
#### Configuring integration with Devise

Add **:with_devise** option in notification routing to *config/routes.rb* for the target:

```ruby
Rails.application.routes.draw do
Expand All @@ -928,6 +949,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
Expand All @@ -948,7 +971,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, :routing_scope, :devise_default_routes)
.to_h.symbolize_keys
.merge(limit: limit, reverse: reverse, with_group_members: with_group_members)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, :routing_scope, :devise_default_routes)
.to_h.symbolize_keys.merge(limit: limit, reverse: reverse)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@

<div class='<%= "notification_#{notification.id}" %>'>
<% 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 %>
<div class="unopned_circle"></div>
<div class="unopned_description_wrapper">
<p class="unopned_description">Open</p>
</div>
<% 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 %>
<div class="unopened_wrapper"></div>
<% 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 %>

</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@

<div class='<%= "notification_#{notification.id}" %>'>
<% 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 %>
<div class="unopned_circle"></div>
<div class="unopned_description_wrapper">
<p class="unopned_description">Open</p>
</div>
<% 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 %>
<div class="unopened_wrapper"></div>
<% 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 %>

</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
Notifications
</p>
<p class="notification_header_menu">
<%= 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 %>
</p>
</div>
<div class="notifications">
<%= yield :notification_index %>
</div>
<%= link_to notifications_path_for(@target) do %>
<%= link_to notifications_path_for(@target, parameters.slice(:routing_scope, :devise_default_routes)) do %>
<div class="notification_link_wrapper">
<p class="notification_link">
See notifications
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<div class="notification_wrapper">
<div class="notification_header">
<h1>Notifications to <%= @target.printable_target_name %> <%= link_to open_all_notifications_path_for(@target, @index_options), method: :post, remote: true do %><span class="notification_count"><span class="<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>"><%= @target.unopened_notification_count(@index_options) %></span></span><% end %></h1>
<h1>Notifications to <%= @target.printable_target_name %> <%= link_to open_all_notifications_path_for(@target, @index_options.slice(:routing_scope, :devise_default_routes)), method: :post, remote: true do %><span class="notification_count"><span class="<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>"><%= @target.unopened_notification_count(@index_options) %></span></span><% end %></h1>
</div>
<div class="notifications">
<% 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 %>
</div>
</div>

<%#= 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) %>

<style>
.notification_wrapper .notification_header h1 span span{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$(".notification_count").html("<span class=\"<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>\"><%= @target.unopened_notification_count(@index_options) %></span>");
<% if @index_options[:with_group_members] %>
$('<%= ".notification_#{@notification.id}" %>').html("<%= escape_javascript( render_notification(@notification, fallback: :default_without_grouping, with_group_members: true) ) %>");
$('<%= ".notification_#{@notification.id}" %>').html("<%= escape_javascript( render_notification(@notification, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default_without_grouping, with_group_members: true)) ) %>");
<% else %>
$('<%= ".notification_#{@notification.id}" %>').html("<%= escape_javascript( render_notification(@notification, fallback: :default) ) %>");
$('<%= ".notification_#{@notification.id}" %>').html("<%= escape_javascript( render_notification(@notification, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default)) ) %>");
<% end %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$(".notification_count").html("<span class=\"<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>\"><%= @target.unopened_notification_count(@index_options) %></span>");
<% if @index_options[:with_group_members] %>
$(".notifications").html("<%= escape_javascript( render_notification(@notifications, fallback: :default_without_grouping, with_group_members: true) ) %>");
$(".notifications").html("<%= escape_javascript( render_notification(@notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default_without_grouping, with_group_members: true)) ) %>");
<% else %>
$(".notifications").html("<%= escape_javascript( render_notification(@notifications, fallback: :default) ) %>");
$(".notifications").html("<%= escape_javascript( render_notification(@notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default)) ) %>");
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
</div>
<ul>
<div class="notifications">
<%= render_notification(@notification, fallback: :default) %>
<%#= render_notification @notification, fallback: :text %>
<%= render_notification @notification, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default) %>
<%#= render_notification @notification, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :text) %>
</div>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Dear <%= @target.printable_target_name %>
<%= @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? %>.

<%= "Move to notified #{@notification.notifiable.printable_type.downcase}:" %>
<%= move_notification_url_for(@notification, open: true) %>
<%= move_notification_url_for(@notification, parameters.slice(:routing_scope, :devise_default_routes).merge(open: true)) %>

Thank you!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<%= @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? %>.

<%= "Move to notified #{@notification.notifiable.printable_type.downcase}:" %>
<%= move_notification_url_for(@notification, open: true) %>
<%= move_notification_url_for(@notification, parameters.slice(:routing_scope, :devise_default_routes).merge(open: true)) %>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<%= key %>
</h3>
<p>
<%= link_to "Notifications", notifications_path_for(target, filtered_by_key: key) %>
<%= link_to "Notifications", notifications_path_for(target, option_params.merge(filtered_by_key: key)) %>
</p>
</div>
<div class="field_wrapper subscribing">
Expand Down Expand Up @@ -97,7 +97,7 @@
<%#= f.submit "Save" %>
</div>
<%# end %>
<%#= link_to "See notifications", notifications_path_for(target, filtered_by_key: key) %>
<%#= link_to "See notifications", notifications_path_for(target, option_params.merge(filtered_by_key: key)) %>
<br/><br/>
<%# end %>
<%# else %>
Expand Down
Loading

0 comments on commit 0a8699e

Please sign in to comment.