Skip to content
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

Add option to hide pagination for single page result #86

Merged
merged 2 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
shulhi marked this conversation as resolved.
Show resolved Hide resolved
)

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