Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Please provide a working example of overriding LinkRenderer #158

Closed
ghost opened this Issue · 16 comments
@ghost

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!

@mislav
Owner
@jrust

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

Where would you put such a file?

@jrust

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

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

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
@jnwelzel

@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.

@bravedick

/+ bootstrap pagination layout for default

@tomukas

How to load oparrish module as default renderer for will_paginate?

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

@tomukas

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 %>

@robacarp

@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
Owner

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

@mislav mislav closed this
@herestomwiththeweather herestomwiththeweather referenced this issue in oscurrency/oscurrency
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.