Skip to content

Commit 0bfc7ba

Browse files
committed
Add option to hide pagination for single page
1 parent 07002d2 commit 0bfc7ba

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

lib/scrivener/html.ex

+20-10
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,30 @@ defmodule Scrivener.HTML do
107107
view_style:
108108
opts[:view_style] || Application.get_env(:scrivener_html, :view_style, :bootstrap)
109109
)
110+
|> Keyword.merge(
111+
hide_single:
112+
opts[:hide_single] || Application.get_env(:scrivener_html, :hide_single, false)
113+
)
110114

111115
merged_opts = Keyword.merge(@defaults, opts)
112116

113117
path = opts[:path] || find_path_fn(conn && paginator.entries, args)
114-
params = Keyword.drop(opts, Keyword.keys(@defaults) ++ [:path])
115-
116-
# Ensure ordering so pattern matching is reliable
117-
_pagination_links(paginator,
118-
view_style: merged_opts[:view_style],
119-
path: path,
120-
args: [conn, merged_opts[:action]] ++ args,
121-
page_param: merged_opts[:page_param],
122-
params: params
123-
)
118+
params = Keyword.drop(opts, Keyword.keys(@defaults) ++ [:path, :hide_single])
119+
120+
hide_single_result = opts[:hide_single] && paginator.total_pages < 2
121+
122+
unless hide_single_result do
123+
# Ensure ordering so pattern matching is reliable
124+
_pagination_links(paginator,
125+
view_style: merged_opts[:view_style],
126+
path: path,
127+
args: [conn, merged_opts[:action]] ++ args,
128+
page_param: merged_opts[:page_param],
129+
params: params
130+
)
131+
else
132+
Phoenix.HTML.raw(nil)
133+
end
124134
end
125135

126136
def pagination_links(%Scrivener.Page{} = paginator),

test/scrivener/html_test.exs

+21
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ defmodule Scrivener.HTMLTest do
293293
html = HTML.pagination_links(%Page{total_pages: 2, page_number: 2}, q: [name: "joe"])
294294
assert Phoenix.HTML.safe_to_string(html) =~ ~r(q\[name\]=joe)
295295
end
296+
297+
test "hide single page result from option" do
298+
html =
299+
HTML.pagination_links(%Page{total_pages: 1, page_number: 1},
300+
q: [name: "joe"],
301+
hide_single: true
302+
)
303+
304+
assert Phoenix.HTML.safe_to_string(html) == ""
305+
end
306+
307+
test "show pagination when there are multiple pages" do
308+
html =
309+
HTML.pagination_links(%Page{total_pages: 2, page_number: 1},
310+
q: [name: "joe"],
311+
hide_single: true
312+
)
313+
314+
assert Phoenix.HTML.safe_to_string(html) ==
315+
"<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>"
316+
end
296317
end
297318

298319
describe "Phoenix conn()" do

0 commit comments

Comments
 (0)