Redirecting to the appropriate URL for some type of notification I have a polymorphic notification model with action attribute. When user clicks on a notification he/she should be redirected to given page the notification refers to through notifiable.

What is the best way to refactor the following method?

_notification.html.erb

&lt;%= link_to checking_decreasing_user_notifications_path(current_user, notification: notification) do %&gt;


notifications controller

def checking_decreasing
  notification = Notification.find(params[:notification])
  current_user.decreasing_comment_notification_number(notification)
  redirect_to notification_redirection_path(notification)
end

def notification_redirection_path(notification)
  notifiable_type = notification.notifiable_type
  notifiable_id = notification.notifiable_id
  action = notification.action
  if action == "commented"
    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"
              product_lead = ProductLead.find(notifiable_id)
              product_id = product_lead.product_id
            when "ProductCustomer"
              product_customer = ProductCustomer.find(notifiable_id)
              product_id = product_customer.product_id
              product_product_customer_path(product_id, notifiable_id, anchor: "comment-panel") #/products/#{product_id}/#{notifiable_type}/#{notifiable_id}#comment-panel"
            end
  elsif action == "invited"
    if notifiable_type == "Product"
      product_path(notifiable_id, anchor: "product-invitation-well")
    elsif notifiable_type == "ProductCustomer"
      product_customer = ProductCustomer.find(notifiable_id)
      product_id = product_customer.product_id
      product_product_customer_path(product_id, notifiable_id)
    end
  elsif action == "accepted"
    if notifiable_type == "Product" #team member invitation
      product_product_owner_panels_path(notifiable_id)
    elsif notifiable_type == "ProductCustomer" #referencer invitation
      product_customer = ProductCustomer.find(notifiable_id)
      product_id = product_customer.product_id
      product_product_owner_panels_path(product_id)
    end
  elsif action == "wrote"
    product_customer = ProductCustomer.find(notifiable_id)
    product_id = product_customer.product_id
    product_product_customer_path(product_id, notifiable_id)
  end
end
Suggestions:


Break long methods into simple parts;
Avoid unnecessary local variables;
Use single quotes for strings without interpolation;
Move repeated parts of code into separate methods.


Code:

def notification_redirection_path(notification)
  notifiable_type = notification.notifiable_type
  notifiable_id = notification.notifiable_id

  case notification.action
  when 'commented'
    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(*lead_params(notifiable_id), anchor: 'comment-panel')#{}"/products/#{product_id}/#{notifiable_type}/#{notifiable_id}#comment-panel"
    when 'ProductCustomer'
      product_product_customer_path(*customer_params(notifiable_id), anchor: 'comment-panel') #/products/#{product_id}/#{notifiable_type}/#{notifiable_id}#comment-panel"
    end
  when 'invited'
    if notifiable_type == 'Product'
      product_path(notifiable_id, anchor: 'product-invitation-well')
    elsif notifiable_type == 'ProductCustomer'
      product_product_customer_path(*customer_params(notifiable_id))
    end
  when 'accepted'
    if notifiable_type == 'Product' #team member invitation
      product_product_owner_panels_path(notifiable_id)
    elsif notifiable_type == 'ProductCustomer' #referencer invitation
      product_product_owner_panels_path(*customer_params(notifiable_id).first)
    end
  when 'wrote'
    product_product_customer_path(*customer_params(notifiable_id))
  end
end

private

def customer_params(notifiable_id)
  [ProductCustomer.find(notifiable_id).product_id, notifiable_id]
end

def lead_params(notifiable_id)
  [ProductLead.find(notifiable_id).product_id, notifiable_id]
end