{"id":31,"date":"2007-04-24T21:53:22","date_gmt":"2007-04-25T01:53:22","guid":{"rendered":"http:\/\/danielroop.com\/blog\/2007\/04\/24\/current_page\/"},"modified":"2008-02-20T21:34:36","modified_gmt":"2008-02-21T01:34:36","slug":"current_page","status":"publish","type":"post","link":"http:\/\/danielroop.com\/blog\/2007\/04\/24\/current_page\/","title":{"rendered":"current_page?"},"content":{"rendered":"<p>This weekend I found myself caught in a love\/hate relationship with a certain method in rails.  I love this method because it uses some sort of mind control to know exactly when to show a link and when to show text.  This method I speak of is UrlHelper.link_to_unless_current.  My beef is not with this method directly, but the underlying UrlHelper.current_page?.  <\/p>\n<p>What dissapponted me about this method was the inability to exclude certain parameters from the check.  The reason why this is important is because I may not want the &#8220;page&#8221; variable to be considered when verifying if the current url is the one in the link_to statement.  So I ended up adding an overriding the current_page? method and this is what I ended up with:<\/p>\n<pre>\r\n<code>\r\n  def current_page?(options, excluding = {})\r\n    url_string = CGI.escapeHTML(url_for(options))\r\n    request = @controller.request\r\n\r\n    if url_string =~ \/^\\w+:\\\/\\\/\/\r\n      url_string == \"#{request.protocol}#{request.host_with_port}#{request.request_uri}\"\r\n    else\r\n      parameters = request.parameters\r\n\r\n      excluding.each do |key|\r\n        parameters.delete(key)\r\n      end\r\n\r\n      request_string = CGI.escapeHTML(url_for(parameters))\r\n      url_string == request_string\r\n    end\r\n  end\r\n<\/code>\r\n<\/pre>\n<p>I basically copied and pasted the current_page? code from the rails repository.  With a few modifications.  First I grabbed the collection of request.parameters and removed any items that are in the excluding list.  Then I rebuilt the url using the url_for method.<\/p>\n<p>With this modification it made it very easy to make link_to_unless_current_with_paging:<\/p>\n<pre><code>\r\n  def link_to_unless_current_with_paging(name, options = {}, html_options = {}, *parameters_for_method_reference, &block)\r\n    link_to_unless current_page?(options, ['page']), name, options, html_options, *parameters_for_method_reference, &block\r\n  end\r\n<\/code><\/pre>\n<p>Hope this helps someone else out there that is getting frustrated with link_to_unless_current.  What would be really nice is if this worked its way into the repository.  Maybe I will get the nerve to make a patch, and be rejected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This weekend I found myself caught in a love\/hate relationship with a certain method in rails. I love this method because it uses some sort of mind control to know exactly when to show a link and when to show &hellip; <a href=\"http:\/\/danielroop.com\/blog\/2007\/04\/24\/current_page\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[22,20],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-programming","tag-rails","tag-ruby"],"_links":{"self":[{"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/danielroop.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}