Skip to content

Commit

Permalink
Add option to hide pagination for single page result (mgwidmann#86)
Browse files Browse the repository at this point in the history
Add option to hide pagination for single page result
  • Loading branch information
mgwidmann authored Jun 12, 2019
2 parents 6358658 + 014ef3c commit ec9283a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/scrivener/html.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Scrivener.HTML do
use Phoenix.HTML
@defaults [view_style: :bootstrap, action: :index, page_param: :page]
@defaults [view_style: :bootstrap, action: :index, page_param: :page, hide_single: false]
@view_styles [:bootstrap, :semantic, :foundation, :bootstrap_v4, :materialize, :bulma]
@raw_defaults [
distance: 5,
Expand Down Expand Up @@ -107,20 +107,30 @@ defmodule Scrivener.HTML do
view_style:
opts[:view_style] || Application.get_env(:scrivener_html, :view_style, :bootstrap)
)
|> Keyword.merge(
hide_single:
opts[:hide_single] || Application.get_env(:scrivener_html, :hide_single, false)
)

merged_opts = Keyword.merge(@defaults, opts)

path = opts[:path] || find_path_fn(conn && paginator.entries, args)
params = Keyword.drop(opts, Keyword.keys(@defaults) ++ [:path])

# Ensure ordering so pattern matching is reliable
_pagination_links(paginator,
view_style: merged_opts[:view_style],
path: path,
args: [conn, merged_opts[:action]] ++ args,
page_param: merged_opts[:page_param],
params: params
)
params = Keyword.drop(opts, Keyword.keys(@defaults) ++ [:path, :hide_single])

hide_single_result = opts[:hide_single] && paginator.total_pages < 2

if hide_single_result do
Phoenix.HTML.raw(nil)
else
# Ensure ordering so pattern matching is reliable
_pagination_links(paginator,
view_style: merged_opts[:view_style],
path: path,
args: [conn, merged_opts[:action]] ++ args,
page_param: merged_opts[:page_param],
params: params
)
end
end

def pagination_links(%Scrivener.Page{} = paginator),
Expand Down
21 changes: 21 additions & 0 deletions test/scrivener/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,27 @@ defmodule Scrivener.HTMLTest do
html = HTML.pagination_links(%Page{total_pages: 2, page_number: 2}, q: [name: "joe"])
assert Phoenix.HTML.safe_to_string(html) =~ ~r(q\[name\]=joe)
end

test "hide single page result from option" do
html =
HTML.pagination_links(%Page{total_pages: 1, page_number: 1},
q: [name: "joe"],
hide_single: true
)

assert Phoenix.HTML.safe_to_string(html) == ""
end

test "show pagination when there are multiple pages" do
html =
HTML.pagination_links(%Page{total_pages: 2, page_number: 1},
q: [name: "joe"],
hide_single: true
)

assert Phoenix.HTML.safe_to_string(html) ==
"<nav><ul class=\"pagination\"><li class=\"active\"><a class=\"\">1</a></li><li class=\"\"><a class=\"\" href=\"?q[name]=joe&amp;page=2\" rel=\"next\">2</a></li><li class=\"\"><a class=\"\" href=\"?q[name]=joe&amp;page=2\" rel=\"next\">&gt;&gt;</a></li></ul></nav>"
end
end

describe "Phoenix conn()" do
Expand Down

0 comments on commit ec9283a

Please sign in to comment.