padrino / padrino-recipes

A collection of padrino templates and plugins
http://www.padrinorb.com
292 stars 50 forks source link

will_paginate: overriding LinkRenderer #49

Closed silentvick closed 11 years ago

silentvick commented 11 years ago

Hi,

Can somebody show me an example of overriding LinkRenderer class for padrino _willpaginate plugin?

I've tried something like this:

module FriendlyPagination
  class LinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
    #...
  end
end

And I get a NotImplementedError. I'm using padrino 0.10.7 and will_paginate 3.0.4

dariocravero commented 11 years ago

Hi @silentvick,

Sorry for the late reply... What about doing this?

module WillPaginate
  module ViewHelpers
    class LinkRenderer
      # ...
    end
  end
end

Put that into lib/will_paginate_link_rendered.rb and you should be good to go...

Let me know if that works for you :)

silentvick commented 11 years ago

Well, padrino starts with no errors. But in this case the url method just ignored. I tried to make links more friendly (for example: /news/page/2 instead of /news?page=2)

This code works for me but I don't know whether it is good solution or not:

module FriendlyPagination
  class LinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

    def prepare(collection, options, template)
      @link_path = options.delete(:link_path)
      super
    end    

    # copied from WillPaginate::Sinatra::Helpers
    protected
      def url(page)
        str = @link_path ? @link_path : File.join(request.script_name.to_s, request.path_info) 

        params = request.GET
        params.update @options[:params] if @options[:params]
        unless params.empty?
          str += '?'.concat( build_query(params) )
        end
        str
      end

      def request
        @template.request
      end

      def build_query(params)
        Rack::Utils.build_nested_query params
      end

  end
end

And then I use renderer option like this:

= will_paginate @news, :renderer => FriendlyPagination::LinkRenderer, :link_path => '/news'

or there is a way to set custom renderer as a default one

Anyway, thanks for your answer, @dariocravero!

P.S: sorry for my English.

dariocravero commented 11 years ago

Hey @silentvick,

I think the approach of setting the default renderer sounds like the cleanest of them all. Way better than stepping over it :)

Thanks for putting your answer back here for future reference! Darío