Sunday, April 17, 2011

Passing target="_top" to will_paginate rails plugin for Facebook iFrame application.

First things first You cannot pass option of target in default will_paginate plugin that is shipped. So you have to modify the plugin.
There is a parameter called "renderer" that you can pass to specify which renderer class to use for will_paginate function. The default is LinkRenderer. In my solution you have to write a new class which will be child of LinkRenderer class. And you will pass this class as a parameter to will_paginate function. Simple.

Here how you define drive the class. The name of file is fb_link_renderer.rb and I placed it under lib folder.

class FBLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

and than you rewrite in link function. The link function is a private function as it is in LinkRenderer class.


def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target
attributes[:target] = "_top"
tag(:a, text, attributes)
end



Here is the complete code for the new class


class FBLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer private def link(text, target, attributes = {}) if target.is_a? Fixnum attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target attributes[:target] = "_top" tag(:a, text, attributes) end end



and here is how you use it


= will_paginate(data, :params => "apps.facebook.com/fb-app-name", :renderer => 'FBLinkRenderer')

No comments:

Post a Comment