Files
grecia/app/controllers/notifications_controller.rb
Javi Martín a64a290392 Extract commentable_path to an initializer
By doing so and including it in ActionDispatch::Routing::UrlFor, we make
it available in controllers, helpers and specs, and so we can remove the
duplication we had there with methods dealing with the same problem.

Even if monkey-patching is ugly, using a different module and executing
ActionDispatch::Routing::UrlFor.send(:include, MyModule) wouldn't make
the method available in the controller.
2018-09-17 20:28:55 +02:00

48 lines
1.1 KiB
Ruby

class NotificationsController < ApplicationController
include CustomUrlsHelper
before_action :authenticate_user!
skip_authorization_check
respond_to :html, :js
def index
@notifications = current_user.notifications.unread
end
def show
@notification = current_user.notifications.find(params[:id])
@notification.mark_as_read
redirect_to linkable_resource_path(@notification)
end
def read
@notifications = current_user.notifications.read
end
def mark_all_as_read
current_user.notifications.unread.each { |notification| notification.mark_as_read }
redirect_to notifications_path
end
def mark_as_read
@notification = current_user.notifications.find(params[:id])
@notification.mark_as_read
end
def mark_as_unread
@notification = current_user.notifications.find(params[:id])
@notification.mark_as_unread
end
private
def linkable_resource_path(notification)
if notification.linkable_resource.is_a?(AdminNotification)
notification.linkable_resource.link || notifications_path
else
polymorphic_hierarchy_path(notification.linkable_resource)
end
end
end