Last active
August 29, 2015 14:20
-
-
Save jaykaycodes/050fd220999afb72210d to your computer and use it in GitHub Desktop.
Extend will_paginate to work with Materialize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/will_paginate.rb | |
module WillPaginate | |
module ActionView | |
def will_paginate(collection = nil, options = {}) | |
options[:renderer] ||= MaterializeLinkRenderer | |
super.try :html_safe | |
end | |
class MaterializeLinkRenderer < LinkRenderer | |
protected | |
def html_container(html) | |
tag :ul, html, container_attributes | |
end | |
def page_number(page) | |
tag :li, | |
link(page, page, rel: rel_value(page)), | |
class: (page == current_page ? 'active' : 'waves-effect') | |
end | |
def previous_or_next_page(page, text, classname) | |
c = classname[0..3] | |
tag :li, | |
link(tag(:i, nil, class: "mdi-navigation-chevron-#{c == 'next' ? 'right' : 'left'}"), page || '#!'), | |
class: [c, classname, ('disabled' unless page)].join(' ') | |
end | |
def gap | |
text = @template.will_paginate_translate(:page_gap) { '…' } | |
%(<li><span class="gap">#{text}</span></li>) | |
end | |
end | |
end | |
end |
I fixed it by replacing the content of the will_paginate method with this:
options, collection = collection, nil if collection.is_a? Hash
collection ||= infer_collection_from_controller
options = options.symbolize_keys
options[:renderer] ||= MaterializeLinkRenderer
super(collection, options)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I add the 'will_paginate' gem and put this file into the initializers directory.
But, when using <%= will_paginate %> I get this error:
undefined method `total_pages' for nil:NilClass
on line 7
Am I using it wrong?
I'm new to rails and working through railstutorial.org - except using Materialise instead of Bootstrap. I'm up to chapter 9.