-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added headers extra #141
added headers extra #141
Conversation
@grosser, @bquorning, @gamafranco, @excid3, @cseelus, @MarieS1415, @NARKOZ, @twnaing... You feedback would be appreciated! |
| `semantic` | Add nav, responsive and compact helpers for the Semantic UI CSS [pagination component](https://semantic-ui.com/collections/menu.html) | [semantic.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/semantic.rb), [documentation](extras/semantic.md) | | ||
| `support` | Extra support for features like: incremental, infinite, auto-scroll pagination | [support.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/support.rb), [documentation](extras/support.md) | | ||
| `trim` | Remove the `page=1` param from links | [trim.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/trim.rb), [documentation](extras/trim.md) | | ||
| Extra | Description | Links | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could avoid the giant diff / make blame simler by not aligning the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, but a non-aligned source makes me sick :)
pagy, records = pagy(collection, vars) # any pagy_* backend constructor works | ||
pagy_headers_merge(pagy) | ||
render json: records | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be more universal to do
def render(*args, &block)
pagy_headers_merge(@pagy) if @pagy
super
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true! This one is a nice minimalist way to just add the headers automatically that works also with existing code. The other one is more a suggestion about encapsulating/decluttering. I will use both suggestions. Thanks
docs/extras/headers.md
Outdated
|
||
## Files | ||
|
||
This extra is composed of 1 small file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment is kinda redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true. I will remove the stupid comments from all the extras and leave only the links to the files. Thanks
lib/pagy/extras/headers.rb
Outdated
|
||
def pagy_headers(pagy) | ||
hash = pagy_headers_hash(pagy) | ||
{ 'Links' => hash[:links].map{|rel, link| %(<#{link}>; rel="#{rel}")}.join(', ') }.tap do |h| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more readable/performant to not use .tap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably... I will try
lib/pagy/extras/headers.rb
Outdated
rels = { first: 1, prev: pagy.prev, next: pagy.next }.tap{ |r| r[:last] = pagy.last unless countless } | ||
a, b = pagy_url_for(Frontend::MARKER, pagy, :url).split(Frontend::MARKER, 2) | ||
links = Hash[rels.map{|rel, n|[rel, "#{a}#{n}#{b}"] if n}.compact] | ||
{ links: links }.tap do |h| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, prefer no tap
lib/pagy/extras/headers.rb
Outdated
def pagy_headers_hash(pagy) | ||
countless = defined?(Pagy::Countless) && pagy.is_a?(Pagy::Countless) | ||
rels = { first: 1, prev: pagy.prev, next: pagy.next }.tap{ |r| r[:last] = pagy.last unless countless } | ||
a, b = pagy_url_for(Frontend::MARKER, pagy, :url).split(Frontend::MARKER, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got some more descriptive names than a/b ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like part_a
and part_b
? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was hoping they have a meaning like url/query "MARKER" is also vague :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, MARKER means... it's just a MARKER 😄 ... used to split the string into 2 parts and wrap them around the page number. That was faster than using sub
to replace MARKER with the page number (at least in pagy_link_proc
), but the 2 parts are just 2 strings without any logical meaning. It's difficult to give them a meaningful name.
lib/pagy/extras/headers.rb
Outdated
countless = defined?(Pagy::Countless) && pagy.is_a?(Pagy::Countless) | ||
rels = { first: 1, prev: pagy.prev, next: pagy.next }.tap{ |r| r[:last] = pagy.last unless countless } | ||
a, b = pagy_url_for(Frontend::MARKER, pagy, :url).split(Frontend::MARKER, 2) | ||
links = Hash[rels.map{|rel, n|[rel, "#{a}#{n}#{b}"] if n}.compact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if none of them is nil this will be nil ?
[].compact!
=> nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure to understand "If none of them": did you mean "if all them are nil"?
BTW, there will be always at least the first page link (even if it is empty), so that should not happen... however I will try to avoid asumptions. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if no nil
is removed the result will be nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't get your comment at all :/ (maybe my morning-dumbness... or just dumbness)
How [].compact!
relates to the code there? Is that a suggestion or a warning?
@@ -10,10 +10,23 @@ class Pagy | |||
# see https://ddnexus.github.io/pagy/api/frontend#i18n | |||
I18n = eval(Pagy.root.join('locales', 'utils', 'i18n.rb').read) #rubocop:disable Security/Eval | |||
|
|||
module Helpers | |||
# This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...) | |||
def pagy_url_for(page, pagy, path_or_url=:path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def pagy_url_for(page, pagy, path_or_url=:path) | |
def pagy_url_for(page, pagy, path_or_url: :path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nicer, but Pagy is ruby 1.9+ compatible since version 2 (I needed to use it in legacy apps with legacy envs)
lib/pagy/frontend.rb
Outdated
module Helpers | ||
# This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...) | ||
def pagy_url_for(page, pagy, path_or_url=:path) | ||
p_vars = pagy.vars; params = request.GET.merge(p_vars[:page_param].to_s => page).merge!(p_vars[:params]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can avoid a merge:
p_vars = pagy.vars; params = request.GET.merge(p_vars[:page_param].to_s => page).merge!(p_vars[:params]) | |
p_vars = pagy.vars; params = request.GET.merge(p_vars[:params]) | |
params[p_vars[:page_param].to_s] = page |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we didn't notice that for so long! Thank
test/pagy/extras/headers_test.rb
Outdated
end | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra newlines ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks pretty useable and nice that it's opt-in
bcab431
to
50773df
Compare
… code improvements; updated doc and tests
50773df
to
25aadeb
Compare
Didn't see this until now. Works great. Thanks 👏 |
Add RFC-8288 compilant HTTP response headers (and other helpers) useful for API pagination.
Pagy::Countless
) regardless the type of collection