Last active
August 29, 2015 14:23
-
-
Save dux/228ed03b8d038337ee3c to your computer and use it in GitHub Desktop.
Super simple Ruby/Rails/Sinatra pagination
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
def paginate(list) | |
ret = ['<div class="paginate"><div>'] | |
if list.paginate_page > 0 | |
url = Url.current | |
list.paginate_page == 1 ? url.delete(list.paginate_var) : url.qs(list.paginate_var, list.paginate_page) | |
ret.push %[<a href="#{url.relative}">←</a>] | |
else | |
ret.push %[<span>←</span>] | |
end | |
ret.push %[<i>#{list.paginate_page.or('•')}</i>] | |
if list.paginate_per_page == list.length | |
url = Url.current | |
url.qs(list.paginate_var, list.paginate_page+2) | |
ret.push %[<a href="#{url.relative}">→</a>] | |
else | |
ret.push %[<span>→</span>] | |
end | |
ret.push '</div></div>' | |
ret.join('') | |
end |
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
# include in model | |
module SimplePaginate | |
def paginate(per_page=20, page_var=:page) | |
page = Lux.params[page_var] || 1 | |
page = page.to_i - 1 | |
ret = where({}).limit(per_page).offset(page * per_page).all.to_a | |
ret.define_singleton_method(:paginate_var) do; page_var ;end | |
ret.define_singleton_method(:paginate_page) do; page ;end | |
ret.define_singleton_method(:paginate_per_page) do; per_page ;end | |
ret | |
end | |
end |
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
.paginate { | |
clear: both; | |
padding:20px 0; | |
text-align:center; | |
> div { | |
border:1px solid #ddd; | |
display:inline-block; | |
border-radius:4px; | |
margin:0 auto; | |
i, span, a { display:inline-block; width:40px; padding:10px; text-align:center; text-decoration:none; color:#aaa; } | |
i { border-left:1px solid #eee; border-right:1px solid #eee; } | |
a { | |
color:#444; | |
&:hover { background-color: #f7f7f7; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment