From 0bfc7ba3fdcfc42d39c8e2f976229b42332644ec Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Tue, 12 Mar 2019 21:07:21 +0800 Subject: [PATCH 1/2] Add option to hide pagination for single page --- lib/scrivener/html.ex | 30 ++++++++++++++++++++---------- test/scrivener/html_test.exs | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/scrivener/html.ex b/lib/scrivener/html.ex index b8e50ba..34311e3 100644 --- a/lib/scrivener/html.ex +++ b/lib/scrivener/html.ex @@ -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 + + unless hide_single_result do + # 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 + ) + else + Phoenix.HTML.raw(nil) + end end def pagination_links(%Scrivener.Page{} = paginator), diff --git a/test/scrivener/html_test.exs b/test/scrivener/html_test.exs index cda67fc..480c910 100644 --- a/test/scrivener/html_test.exs +++ b/test/scrivener/html_test.exs @@ -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) == + "" + end end describe "Phoenix conn()" do From 014ef3c7c9d0f6e2da81eed5f6648e47346cc29c Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 2 May 2019 21:27:22 +0800 Subject: [PATCH 2/2] Minor fix based on feedback --- lib/scrivener/html.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/scrivener/html.ex b/lib/scrivener/html.ex index 34311e3..7cb0835 100644 --- a/lib/scrivener/html.ex +++ b/lib/scrivener/html.ex @@ -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, @@ -119,7 +119,9 @@ defmodule Scrivener.HTML do hide_single_result = opts[:hide_single] && paginator.total_pages < 2 - unless hide_single_result do + 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], @@ -128,8 +130,6 @@ defmodule Scrivener.HTML do page_param: merged_opts[:page_param], params: params ) - else - Phoenix.HTML.raw(nil) end end