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

Override total_entries and total_pages #46

Closed
coreyk opened this issue Jan 10, 2017 · 5 comments
Closed

Override total_entries and total_pages #46

coreyk opened this issue Jan 10, 2017 · 5 comments

Comments

@coreyk
Copy link

coreyk commented Jan 10, 2017

I have a use case where i need to pass the total entries and total pages from a more complex query.

How can I override total_entries and total_pages from Scrivener.Ecto?
This doesn't work (from the Scrivener.Ecto README):

def index(conn, params) do
  page =
    MyApp.Person
    |> where([p], p.age > 30)
    |> order_by(desc: :age)
    |> preload(:friends)
    |> MyApp.Repo.paginate(params)

  render conn, :index,
    people: page.entries,
    page_number: page.page_number,
    page_size: page.page_size,
    total_pages: new_page_total, # NEW TOTAL PAGES, CALCULATED ELSEWHERE
    total_entries: new_entry_total # NEW TOTAL ENTRIES, CALCULATED ELSEWHERE
end
@coreyk
Copy link
Author

coreyk commented Jan 10, 2017

I guess if you do this it works:

def index(conn, params) do
  page =
    MyApp.Person
    |> where([p], p.age > 30)
    |> order_by(desc: :age)
    |> preload(:friends)
    |> MyApp.Repo.paginate(params)

  meow = %Scrivener.Page{entries: page.entries,
    page_number: page.page_number,
    page_size: page.page_size,
    total_pages: new_total_pages,
    total_entries: new_total_entries}

    render conn, :index, properties: meow

@mgwidmann
Copy link
Owner

Yes you could do that. However, I suppose this is more for the Scrivener project to tackle than here.
I'd just update the values rather than creating a new page...

render conn, :index, page: %{page | total_pages: new_total_pages, total_entries: new_total_entries}

But first, think about the fact that you're doing this before you do it. If the paginate function isnt giving you the data you want in those fields, ask yourself if maybe you're doing something wrong. I'd bet that @drewolson likely would want to know more about your use case in changing these values.

@coreyk
Copy link
Author

coreyk commented Jan 11, 2017

The reason for this is to get around the limitation of Scrivener.Ecto when not selecting * and needing an accurate result count.

My queries are more complex than usual, with 6 joins, a where and a select on 10 columns across 7 tables. The count distinct can't be on *.

@mgwidmann
Copy link
Owner

either way, I'm not sure how this project should change to help you. You can already override the total_pages and total_entries like I showed, and then the helper function this project exposes will work just the same...

@drewolson
Copy link
Contributor

@coreyk the newest release of scrivener_ecto uses subquery to produce accurate counts. Please take a look. This issue is unrelated to this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants