Skip to content
This repository

Please provide a working example of overriding LinkRenderer #158

Closed
ghost opened this Issue August 15, 2011 · 16 comments

11 participants

Mislav Marohnić Jason Rust Avishai Weiss Owen Jonathan Brave Dick Helmut Michael Juskewycz Tomas Aleksiejunas Robert L. Carpenter jferrercas thehungrycoder
Deleted user

The content you are editing has changed. Reload the page and try again.

I can override LinkRenderer in the Git version, but when I call it I get a NotImplementedError for the #url method. It's not clear what the "correct" way to override this functionality is, but overriding #to_html alone does not appear to be sufficient.

If you could add an article on how to do this, it would really help. Thanks!

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Mislav Marohnić
Owner
Jason Rust

FWIW, here's an example in my rails 3 app that does google-like pagination. Based off mislav's comment I inherited my custom renderer from the WP::ActionView::LinkRenderer and all the NotImplemented errors disappeared.

# Only show the next 5 subsequent pages. i.e. <- prev 2,3,4,5,6 next ->
module SearchPagination
  class LinkRenderer < WillPaginate::ActionView::LinkRenderer
    protected
    def windowed_page_numbers
      inner_window = @options[:inner_window].to_i
      window_from = current_page
      window_to = current_page + inner_window

      # adjust upper limit if out of bounds
      window_to = total_pages if window_to > total_pages
      left = (window_from..window_to).to_a
      left << :gap if total_pages > window_to
      left
    end
  end
end
Avishai Weiss

Where would you put such a file?

Jason Rust

I just put it in app/helpers and then called the pagination helper with the renderer, i.e.

will_paginate @results, :renderer => SearchPagination::LinkRenderer
Owen

I created a link renderer to generate markup to work with the Twitter Bootstrap CSS. The renderer is available in a Gist here: https://gist.github.com/1248807

module BootstrapPaginationHelper
    class LinkRenderer < WillPaginate::ActionView::LinkRenderer
        protected

      def page_number(page)
        unless page == current_page
          link(page, page, :rel => rel_value(page))
        else
          link(page, "#", :class => 'active')
        end
      end

      def gap
        text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
        %(<li class="disabled"><a>#{text}</a></li>)
      end

      def next_page
        num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
        previous_or_next_page(num, @options[:next_label], 'next')
      end

      def previous_or_next_page(page, text, classname)
        if page
          link(text, page, :class => classname)
        else
          link(text, "#", :class => classname + ' disabled')
        end
      end

      def html_container(html)
        tag(:div, tag(:ul, html), container_attributes)
      end

    private

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

                unless target == "#"
                    attributes[:href] = target
                end

        classname = attributes[:class]
        attributes.delete(:classname)
        tag(:li, tag(:a, text, attributes), :class => classname)
        end
    end
end
Jonathan

@oparrish I had a slightly different version here, but ever since I upgraded to 3.0.2 it just stopped working. Thanks for sharing your code, now my renderer works flawlessly.

Brave Dick

/+ bootstrap pagination layout for default

Tomas Aleksiejunas

How to load oparrish module as default renderer for will_paginate?

This doesnt work:
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'BootstrapPaginationHelper::LinkRenderer'

Tomas Aleksiejunas

Thanks oparish, I got it working in the view with :renderer parameter. I just want to use it as default renderer for all will_paginate calls like <%=will_paginate @something %>

Robert L. Carpenter

@tomukas https://gist.github.com/1562185

I forked @oparrish 's gist and added a few files showing how I do this. I basically wrote a helper method and stuck it in application_helper.rb that calls will_paginate with the right renderer. That way when the code changes I can just tweak the interface in one place.

jferrercas

@oparrish You are the man!! Thank you

thehungrycoder

@oparrish and @robacarp you both saved my night. was searching like these for hours. thanks a lot

Mislav Marohnić
Owner

No example yet, but a start of a wiki page: https://github.com/mislav/will_paginate/wiki/Link-renderer

Mislav Marohnić mislav closed this January 09, 2013
Tom Brown herestomwiththeweather referenced this issue in oscurrency/oscurrency July 13, 2013
Closed

bootstrap: style pagination links #402

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.