Please provide a working example of overriding LinkRenderer #158

Closed
ghost opened this Issue Aug 15, 2011 · 20 comments

Comments

Projects
None yet
@ghost

ghost commented Aug 15, 2011

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

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Aug 15, 2011

Owner

Yes, I am aware that previous ways of implementing link renderers are no longer valid. I plan to write docs for this next week. In the meantime, use the source :) See how WP::ActionView::LinkRenderer is implemented!

Owner

mislav commented Aug 15, 2011

Yes, I am aware that previous ways of implementing link renderers are no longer valid. I plan to write docs for this next week. In the meantime, use the source :) See how WP::ActionView::LinkRenderer is implemented!

@jrust

This comment has been minimized.

Show comment
Hide comment
@jrust

jrust Sep 8, 2011

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

jrust commented Sep 8, 2011

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

This comment has been minimized.

Show comment
Hide comment
@avishai

avishai Sep 9, 2011

Where would you put such a file?

avishai commented Sep 9, 2011

Where would you put such a file?

@jrust

This comment has been minimized.

Show comment
Hide comment
@jrust

jrust Sep 9, 2011

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

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

jrust commented Sep 9, 2011

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

This comment has been minimized.

Show comment
Hide comment
@oparrish

oparrish Sep 28, 2011

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

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

This comment has been minimized.

Show comment
Hide comment
@jnwelzel

jnwelzel Nov 3, 2011

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

jnwelzel commented Nov 3, 2011

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

@bravecow

This comment has been minimized.

Show comment
Hide comment
@bravecow

This comment has been minimized.

Show comment
Hide comment
@bravecow

bravecow Nov 18, 2011

/+ bootstrap pagination layout for default

/+ bootstrap pagination layout for default

@hemju

This comment has been minimized.

Show comment
Hide comment

hemju commented Dec 12, 2011

@oparrish thx!

@tomukas

This comment has been minimized.

Show comment
Hide comment
@tomukas

tomukas Dec 14, 2011

How to load oparrish module as default renderer for will_paginate?

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

tomukas commented Dec 14, 2011

How to load oparrish module as default renderer for will_paginate?

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

@oparrish

This comment has been minimized.

Show comment
Hide comment
@tomukas

This comment has been minimized.

Show comment
Hide comment
@tomukas

tomukas Dec 14, 2011

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

tomukas commented Dec 14, 2011

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

This comment has been minimized.

Show comment
Hide comment
@robacarp

robacarp Jan 4, 2012

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

robacarp commented Jan 4, 2012

@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

This comment has been minimized.

Show comment
Hide comment
@jferrercas

jferrercas Mar 8, 2012

@oparrish You are the man!! Thank you

@oparrish You are the man!! Thank you

@thehungrycoder

This comment has been minimized.

Show comment
Hide comment
@thehungrycoder

thehungrycoder Apr 10, 2012

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

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

@mislav

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Jan 10, 2013

Owner

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

Owner

mislav commented Jan 10, 2013

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

@mislav mislav closed this Jan 10, 2013

@herestomwiththeweather herestomwiththeweather referenced this issue in oscurrency/oscurrency Jul 13, 2013

Closed

bootstrap: style pagination links #402

@maxfwerner

This comment has been minimized.

Show comment
Hide comment
@maxfwerner

maxfwerner Nov 26, 2015

I created a gist with a fully working rails AJAX paginator with bootstrap 4 styling applied (should work for bootstrap 3 too). You can find it here:

https://gist.github.com/maxfwerner/edcb89db77e9e2a3f521

I created a gist with a fully working rails AJAX paginator with bootstrap 4 styling applied (should work for bootstrap 3 too). You can find it here:

https://gist.github.com/maxfwerner/edcb89db77e9e2a3f521

@zandroid

This comment has been minimized.

Show comment
Hide comment
@zandroid

zandroid Oct 9, 2016

There is an example of custom renderer for Bootstrap 4 (alpha 4) https://gist.github.com/zandroid/8ed85b518fd3ce441f0864477aa7eb86

zandroid commented Oct 9, 2016

There is an example of custom renderer for Bootstrap 4 (alpha 4) https://gist.github.com/zandroid/8ed85b518fd3ce441f0864477aa7eb86

@el-yomi

This comment has been minimized.

Show comment
Hide comment
@el-yomi

el-yomi Mar 17, 2017

Here is an example of custom renderer for Bulma (0.3.2)
bulma_pagination_helper.rb
`

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

    def html_container(html)
      tag(:nav, tag(:ul, html, class: "pagination-list"), class: "pagination is-centered")
    end

    def previous_or_next_page(page, text, classname)
      if page
        tag(:li, link(text, page, class: "pagination-link"))
      else
        tag(:li, tag(:a, text, class: "pagination-link"), class: "is-disabled")
      end
    end

    def page_number(page)
      unless page == current_page
        tag(:li, link(page, page, class: "pagination-link"))
      else
        tag(:li, tag(:a, page, class: "pagination-link is-current"))
      end
    end

    def gap
      tag(:li, tag(:span, '&hellip;', class: "pagination-ellipsis"))
    end

  end

  def bulma_will_paginate(collection = nil, options = {})
    options, collection = collection, nil if collection.is_a? Hash
    options = options.merge(
      renderer: BulmaPaginationHelper::LinkRenderer,
    )
    will_paginate(collection, options)
  end

end

`

Example of usage: just bulma_will_paginate

el-yomi commented Mar 17, 2017

Here is an example of custom renderer for Bulma (0.3.2)
bulma_pagination_helper.rb
`

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

    def html_container(html)
      tag(:nav, tag(:ul, html, class: "pagination-list"), class: "pagination is-centered")
    end

    def previous_or_next_page(page, text, classname)
      if page
        tag(:li, link(text, page, class: "pagination-link"))
      else
        tag(:li, tag(:a, text, class: "pagination-link"), class: "is-disabled")
      end
    end

    def page_number(page)
      unless page == current_page
        tag(:li, link(page, page, class: "pagination-link"))
      else
        tag(:li, tag(:a, page, class: "pagination-link is-current"))
      end
    end

    def gap
      tag(:li, tag(:span, '&hellip;', class: "pagination-ellipsis"))
    end

  end

  def bulma_will_paginate(collection = nil, options = {})
    options, collection = collection, nil if collection.is_a? Hash
    options = options.merge(
      renderer: BulmaPaginationHelper::LinkRenderer,
    )
    will_paginate(collection, options)
  end

end

`

Example of usage: just bulma_will_paginate

@blairanderson

This comment has been minimized.

Show comment
Hide comment
@blairanderson

blairanderson Jun 21, 2017

here is a tutorial for creating a custom will_paginate renderer

here is a tutorial for creating a custom will_paginate renderer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment