Skip to content

Commit

Permalink
refactoring of default element ids for helpers and deprecation:
Browse files Browse the repository at this point in the history
- default ids are now constant also in different processes and compilant also with oder HTML versions
- simpler deprecation code
  • Loading branch information
ddnexus committed Jan 13, 2019
1 parent cef823c commit e35a9ae
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
10 changes: 5 additions & 5 deletions lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def pagy_bootstrap_nav(pagy)
: %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
%(<nav class="pagy-nav-bootstrap pagy-bootstrap-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
end
Pagy.deprecate self, :pagy_nav_bootstrap, :pagy_bootstrap_nav
deprecate :pagy_nav_bootstrap, :pagy_bootstrap_nav

# Compact pagination for bootstrap: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_bootstrap_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_bootstrap_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<nav id="#{id}" class="pagy-nav-compact-bootstrap pagy-bootstrap-compact-nav pagination" role="navigation" aria-label="pager">)
Expand All @@ -41,11 +41,11 @@ def pagy_bootstrap_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_bootstrap, :pagy_bootstrap_compact_nav
deprecate :pagy_nav_compact_bootstrap, :pagy_bootstrap_compact_nav

# Responsive pagination for bootstrap: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_bootstrap_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_bootstrap_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive

tags['before'] = +'<ul class="pagination">'
Expand All @@ -63,7 +63,7 @@ def pagy_bootstrap_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-bootstrap pagy-bootstrap-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_bootstrap, :pagy_bootstrap_responsive_nav
deprecate :pagy_nav_responsive_bootstrap, :pagy_bootstrap_responsive_nav

end
end
10 changes: 5 additions & 5 deletions lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def pagy_bulma_nav(pagy)
html << '</ul>'
%(<nav class="pagy-nav-bulma pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
end
Pagy.deprecate self, :pagy_nav_bulma, :pagy_bulma_nav
deprecate :pagy_nav_bulma, :pagy_bulma_nav

# Compact pagination for Bulma: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_bulma_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_bulma_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<nav id="#{id}" class="pagy-nav-compact-bulma pagy-bulma-compact-nav" role="navigation" aria-label="pagination">)
Expand All @@ -43,11 +43,11 @@ def pagy_bulma_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_bulma, :pagy_bulma_compact_nav
deprecate :pagy_nav_compact_bulma, :pagy_bulma_compact_nav

# Responsive pagination for Bulma: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_bulma_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_bulma_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

tags['before'] = +(p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
Expand All @@ -65,7 +65,7 @@ def pagy_bulma_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-bulma pagy-bulma-responsive-nav pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_bulma, :pagy_bulma_responsive_nav
deprecate :pagy_nav_responsive_bulma, :pagy_bulma_responsive_nav

end
end
10 changes: 5 additions & 5 deletions lib/pagy/extras/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def pagy_foundation_nav(pagy)
: %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
%(<nav class="pagy-nav-foundation pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
end
Pagy.deprecate self, :pagy_nav_foundation, :pagy_foundation_nav
deprecate :pagy_nav_foundation, :pagy_foundation_nav

# Compact pagination for Foundation: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_foundation_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_foundation_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<nav id="#{id}" class="pagy-nav-compact-foundation pagy-foundation-compact-nav" role="navigation" aria-label="Pagination">)
Expand All @@ -41,11 +41,11 @@ def pagy_foundation_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_foundation, :pagy_foundation_compact_nav
deprecate :pagy_nav_compact_foundation, :pagy_foundation_compact_nav

# Responsive pagination for Foundation: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_foundation_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_foundation_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

tags['before'] = +'<ul class="pagination">'
Expand All @@ -63,7 +63,7 @@ def pagy_foundation_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-foundation pagy-foundation-responsive-nav" aria-label="Pagination"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_foundation, :pagy_foundation_responsive_nav
deprecate :pagy_nav_responsive_foundation, :pagy_foundation_responsive_nav

end
end
2 changes: 1 addition & 1 deletion lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def pagy_url_for_with_items(page, pagy)
alias_method :pagy_url_for, :pagy_url_for_with_items

# Return the items selector HTML. For example "Show [20] items per page"
def pagy_items_selector(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_items_selector(pagy, id=pagy_id)
pagy = pagy.clone; p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"

html = +%(<span id="#{id}">)
Expand Down
10 changes: 5 additions & 5 deletions lib/pagy/extras/materialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def pagy_materialize_nav(pagy)
: %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
%(<div class="pagy-nav-materialize pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></div>)
end
Pagy.deprecate self, :pagy_nav_materialize, :pagy_materialize_nav
deprecate :pagy_nav_materialize, :pagy_materialize_nav

# Compact pagination for materialize: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_materialize_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_materialize_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<div id="#{id}" class="pagy-nav-compact-materialize pagy-materialize-compact-nav pagination" role="navigation" aria-label="pager">)
Expand All @@ -42,11 +42,11 @@ def pagy_materialize_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
html << %(</ul></div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_materialize, :pagy_materialize_compact_nav
deprecate :pagy_nav_compact_materialize, :pagy_materialize_compact_nav

# Responsive pagination for Materialize: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_materialize_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_materialize_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

tags['before'] = +'<ul class="pagination">'
Expand All @@ -64,7 +64,7 @@ def pagy_materialize_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<div id="#{id}" class="pagy-nav-responsive-materialize pagy-materialize-responsive-nav pagination" role="navigation" aria-label="pager"></div>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_materialize, :pagy_materialize_responsive_nav
deprecate :pagy_nav_responsive_materialize, :pagy_materialize_responsive_nav

end
end
8 changes: 4 additions & 4 deletions lib/pagy/extras/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Frontend

# Plain compact pagination: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_plain_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_plain_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<nav id="#{id}" class="pagy-nav-compact pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
Expand All @@ -25,11 +25,11 @@ def pagy_plain_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
html << %(</nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact, :pagy_plain_compact_nav
deprecate :pagy_nav_compact, :pagy_plain_compact_nav

# Plain responsive pagination: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_plain_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_plain_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

tags['before'] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
Expand All @@ -45,7 +45,7 @@ def pagy_plain_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive, :pagy_plain_responsive_nav
deprecate :pagy_nav_responsive, :pagy_plain_responsive_nav

end
end
10 changes: 5 additions & 5 deletions lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def pagy_semantic_nav(pagy)
: %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
%(<div class="pagy-nav-semantic pagy-semantic-nav ui pagination menu" aria-label="pager">#{html}</div>)
end
Pagy.deprecate self, :pagy_nav_semantic, :pagy_semantic_nav
deprecate :pagy_nav_semantic, :pagy_semantic_nav

# Compact pagination for semantic: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_semantic_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_semantic_compact_nav(pagy, id=pagy_id)
html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<div id="#{id}" class="pagy-nav-compact-semantic pagy-semantic-compact-nav ui compact menu" role="navigation" aria-label="pager">)
Expand All @@ -40,11 +40,11 @@ def pagy_semantic_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
: %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
html << %(</div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_semantic, :pagy_semantic_compact_nav
deprecate :pagy_nav_compact_semantic, :pagy_semantic_compact_nav

# Responsive pagination for semantic: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_semantic_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
def pagy_semantic_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.responsive

tags['before'] = (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
Expand All @@ -60,7 +60,7 @@ def pagy_semantic_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<div id="#{id}" class="pagy-nav-responsive-semantic pagy-semantic-responsive-nav ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_semantic, :pagy_semantic_responsive_nav
deprecate :pagy_nav_responsive_semantic, :pagy_semantic_responsive_nav

end
end
20 changes: 13 additions & 7 deletions lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'json'
require 'digest'

class Pagy

Expand All @@ -27,19 +28,24 @@ def responsive
end
end

def self.deprecate(mod, old_meth, new_meth)
mod.send(:define_method, old_meth) do |pagy, id=caller(1,1)[0].hash.to_s|
Warning.warn "WARNING: The ##{old_meth} pagy helper method is deprecated and will be removed in 2.0; please use ##{new_meth} instead. More info at https://github.com/ddnexus/pagy/blob/master/DEPRECATIONS.md\n"
mod.instance_method(new_meth).arity == 1 ? send(new_meth, pagy) : send(new_meth, pagy, id)
end
end

module Frontend

def pagy_json_tag(*args)
%(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
end

def pagy_id
# SHA1 is the fastest on modern ruby
"pagy-#{Digest::SHA1.hexdigest(caller(2..2)[0].split(':in')[0])}"
end

def self.deprecate(old_meth, new_meth)
send(:define_method, old_meth) do |pagy, id=pagy_id|
Warning.warn "WARNING: The ##{old_meth} pagy helper method is deprecated and will be removed in 2.0; please use ##{new_meth} instead. More info at https://github.com/ddnexus/pagy/blob/master/DEPRECATIONS.md\n"
method(new_meth).arity == 1 ? send(new_meth, pagy) : send(new_meth, pagy, id)
end
end

end

end

0 comments on commit e35a9ae

Please sign in to comment.