Skip to content

Commit ec9283a

Browse files
authored
Add option to hide pagination for single page result (mgwidmann#86)
Add option to hide pagination for single page result
2 parents 6358658 + 014ef3c commit ec9283a

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

lib/scrivener/html.ex

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Scrivener.HTML do
22
use Phoenix.HTML
3-
@defaults [view_style: :bootstrap, action: :index, page_param: :page]
3+
@defaults [view_style: :bootstrap, action: :index, page_param: :page, hide_single: false]
44
@view_styles [:bootstrap, :semantic, :foundation, :bootstrap_v4, :materialize, :bulma]
55
@raw_defaults [
66
distance: 5,
@@ -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+
if hide_single_result do
123+
Phoenix.HTML.raw(nil)
124+
else
125+
# Ensure ordering so pattern matching is reliable
126+
_pagination_links(paginator,
127+
view_style: merged_opts[:view_style],
128+
path: path,
129+
args: [conn, merged_opts[:action]] ++ args,
130+
page_param: merged_opts[:page_param],
131+
params: params
132+
)
133+
end
124134
end
125135

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

test/scrivener/html_test.exs

Lines changed: 21 additions & 0 deletions
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)