I have a polymorphic notification model. When user gets a notification he can get on the page of the notifiable to check out what happened exactly. For instance invited you to join product
or commented on a post
. On the top of this I also have a polymorphic comment model. So user can get a notification not only for commented on a post
but also for commented on a product customer
or commented on a product
, which of course just makes the whole situation more complex.
So the flow is the following:
If the user clicks on the notification, the link redirects to the custom checking_decreasing
action to check the notification and decrease the notification number if necessary, and then it redirects to the desired page with anchor.
As you see my notification_redirection_path
action looks terrible and I'm not even sure if it should be in application controller. Any ideas how I could refactor this code?
notifications index page (there is also a json version for dropdown notifications)
<%= link_to checking_decreasing_user_notifications_path(
current_user,
notifiable_type: notification.notifiable_type,
notifiable_id: notification.notifiable_id,
notification_action: notification.action
) do %>
notifications_controller
def checking_decreasing
current_user.decreasing_comment_notification_number(
params[:notifiable_type],
params[:notifiable_id]
)
redirect_to notification_redirection_path(
params[:notifiable_type],
params[:notifiable_id],
params[:notification_action]
)
end
application controller
def notification_redirection_path(notifiable_type, notifiable_id, action)
if action == "commented"
if notifiable_type == "ProductCustomer"
product_customer = ProductCustomer.find(notifiable_id)
product_id = product_customer.product_id
elsif notifiable_type == "ProductLead"
product_lead = ProductLead.find(notifiable_id)
product_id = product_lead.product_id
end
route = case notifiable_type
when "Post"
posts_path(anchor: "post_#{notifiable_id}")#{}"/posts#post_#{notifiable_id}"
when "Product"
product_path(notifiable_id, anchor: "comment-panel")#/products/#{notifiable_id}#comment-panel"
when "ProductLead"
product_product_lead_path(product_id, notifiable_id, anchor: "comment-panel")#{}"/products/#{product_id}/#{notifiable_type}/#{notifiable_id}#comment-panel"
when "ProductCustomer"
product_product_customer_path(product_id, notifiable_id, anchor: "comment-panel") #/products/#{product_id}/#{notifiable_type}/#{notifiable_id}#comment-panel"
end
elsif action == "invited"
product_path(notifiable_id, anchor: "product-invitation-well")
elsif action == "accepted"
product_product_users_path(notifiable_id)
end
end