Skip to content

Commit

Permalink
Refactor lazily calculating total_entries
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Oct 27, 2017
1 parent 1489f3a commit a4ac5ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ defimpl Scrivener.Paginater, for: Ecto.Query do

@spec paginate(Ecto.Query.t, Scrivener.Config.t) :: Scrivener.Page.t
def paginate(query, %Config{page_size: page_size, page_number: page_number, module: repo, caller: caller, options: options}) do
options = options || []
total_entries = options[:total_entries] || total_entries(query, repo, caller)

total_entries = Keyword.get_lazy(options, :total_entries, fn -> total_entries(query, repo, caller) end)

%Page{
page_size: page_size,
page_number: page_number,
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Scrivener.Ecto.Mixfile do
def project do
[
app: :scrivener_ecto,
version: "1.3.1-dev",
version: "1.3.0-dev",
elixir: "~> 1.3",
elixirc_paths: elixirc_paths(Mix.env),
package: package(),
Expand Down Expand Up @@ -41,7 +41,7 @@ defmodule Scrivener.Ecto.Mixfile do

defp deps do
[
{:scrivener, "~> 2.3"},
{:scrivener, "~> 2.4"},
{:ecto, "~> 2.0"},
{:dialyxir, "~> 0.5.0", only: :dev},
{:earmark, ">= 0.0.0", only: :dev},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"ex_spec": {:hex, :ex_spec, "1.0.0", "b1e791072fecbf80c725adf45e7cbdf3d96af3765638a1f1547824706ece4bc9", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.13.3", "c277cfb2a9c5034d445a722494c13359e361d344ef6f25d604c2353185682bfc", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"scrivener": {:hex, :scrivener, "2.3.0", "16b1d744202d47233798205447b35592d96a209241c566304f84ddef63c718b2", [:mix], [], "hexpm"}}
"scrivener": {:hex, :scrivener, "2.4.0", "c9431804b13ac6a5c4b01eb32188c1ff926898a2d684244d021706841f022e66", [:mix], [], "hexpm"}}
20 changes: 19 additions & 1 deletion test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do

test "can be provided the caller as a map" do
create_posts()

parent = self()

task = Task.async(fn ->
Expand All @@ -173,6 +174,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
end

test "will respect the max_page_size configuration" do
create_posts()

page =
Post
|> Post.published
Expand All @@ -182,12 +185,15 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
end

test "will respect the total_entries configuration" do
create_posts()

config = %Scrivener.Config{
module: Scrivener.Ecto.Repo,
page_number: 2,
page_size: 4,
options: [total_entries: 130]
}

page =
Post
|> Post.published
Expand All @@ -196,6 +202,17 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page.total_entries == 130
end

test "will respect total_entries passed to paginate" do
create_posts()

page =
Post
|> Post.published
|> Scrivener.Ecto.Repo.paginate(options: [total_entries: 130])

assert page.total_entries == 130
end

test "can be used on a table with any primary key" do
create_key_values()

Expand Down Expand Up @@ -264,7 +281,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
config = %Scrivener.Config{
module: Scrivener.Ecto.Repo,
page_number: 2,
page_size: 4
page_size: 4,
options: []
}

page =
Expand Down

0 comments on commit a4ac5ca

Please sign in to comment.