Skip to content

Commit

Permalink
Add default routes with devise integration - #64
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Nov 5, 2018
1 parent 11d113a commit cd7d7b2
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 53 deletions.
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", parameters[:devise_default_routes] ? open_all_notifications_path : open_all_notifications_path_for(@target), method: :post, remote: true %>
<% if @target.class.subscription_enabled? %>
<%= link_to "Subscriptions", subscriptions_path_for(@target) %>
<%= link_to "Subscriptions", parameters[:devise_default_routes] ? subscriptions_path : subscriptions_path_for(@target) %>
<% end %>
</p>
</div>
<div class="notifications">
<%= yield :notification_index %>
</div>
<%= link_to notifications_path_for(@target) do %>
<%= link_to parameters[:devise_default_routes] ? notifications_path : notifications_path_for(@target) do %>
<div class="notification_link_wrapper">
<p class="notification_link">
See notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def authenticate_devise_resource!
end
end

# Sets @target instance variable from request parameters.
# This method override super (ActivityNotiication::CommonController#set_target)
# to set devise authenticated target when the target_id params is not specified.
# @api protected
# @return [Object] Target instance (Returns HTTP 400 when request parameters are not enough)
def set_target
target_type = params[:target_type]
if params[:target_id].blank? && params["#{target_type.to_resource_name}_id"].blank?
target_class = target_type.to_model_class
current_resource_method_name = "current_#{params[:devise_type].to_resource_name}"
params[:target_id] = target_class.resolve_current_devise_target(send(current_resource_method_name))
render(plain: "403 Forbidden: Unauthenticated as default target", status: 403) and return if params[:target_id].blank?
end
super
end

# Authenticate the target of requested notification with authenticated devise resource.
# @api protected
# @todo Needs to call authenticate method by more secure way
Expand Down
3 changes: 2 additions & 1 deletion lib/activity_notification/helpers/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def render_notification(notifications, options = {})
# @option options [String] :notification_layout (nil) Layout template name of the notification index content
# @option options [String] :fallback (nil) Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.
# @option options [String] :partial ('index') Partial template name of the partial index
# @option options [Boolean] :devise_default_routes (false) Partial template name of the partial index
# @option options [String] :layout (nil) Layout template name of the partial index
# @option options [Integer] :limit (nil) Limit to query for notifications
# @option options [Boolean] :reverse (false) If notification index will be ordered as earliest first
Expand All @@ -51,7 +52,7 @@ def render_notification(notifications, options = {})
# @option options [String] :filtered_by_key (nil) Key of the notification for filter
# @option options [Array] :custom_filter (nil) Custom notification filter (e.g. ["created_at >= ?", time.hour.ago])
# @return [String] Rendered view or text as string
def render_notification_of target, options = {}
def render_notification_of(target, options = {})
return unless target.is_a? ActivityNotification::Target

# Prepare content for notifications index
Expand Down
11 changes: 11 additions & 0 deletions lib/activity_notification/models/concerns/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Target
:_batch_notification_email_allowed,
:_notification_subscription_allowed,
:_notification_devise_resource,
:_notification_current_devise_target,
:_printable_notification_target_name
set_target_class_defaults
end
Expand All @@ -39,6 +40,7 @@ def set_target_class_defaults
self._batch_notification_email_allowed = ActivityNotification.config.email_enabled
self._notification_subscription_allowed = ActivityNotification.config.subscription_enabled
self._notification_devise_resource = ->(model) { model }
self._notification_current_devise_target = ->(current_resource) { current_resource }
self._printable_notification_target_name = :printable_name
nil
end
Expand Down Expand Up @@ -130,6 +132,15 @@ def send_batch_unopened_notification_email(options = {})
}.to_h
end

# Resolves current authenticated target by devise authentication from current resource signed in with Devise.
# This method is able to be overriden.
#
# @param [Object] current_resource Current resource signed in with Devise
# @return [Object] Current authenticated target by devise authentication
def resolve_current_devise_target(current_resource)
_notification_current_devise_target.call(current_resource)
end

# Returns if subscription management is allowed for this target type.
# @return [Boolean] If subscription management is allowed for this target type
def subscription_enabled?
Expand Down
Loading

0 comments on commit cd7d7b2

Please sign in to comment.