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

Filter by has_one association field #96

Closed
akrisanov opened this issue Oct 18, 2018 · 6 comments
Closed

Filter by has_one association field #96

akrisanov opened this issue Oct 18, 2018 · 6 comments

Comments

@akrisanov
Copy link

I have two models – User and Credential. My admin UI shows a list of users and has some filters on the sidebar. It's pretty easy to add a filter for a model field, but I didn't find any examples in the documentation explaining filtration mechanism for associations. Is it possible to filter user records by credential.email without hacking do_paginate_users/2 much?

@doc """
Paginate the list of users using filtrex
filters.

## Examples

    iex> list_users(%{})
    %{users: [%User{}], ...}
"""
@spec paginate_users(map) :: {:ok, map} | {:error, any}
def paginate_users(params \\ %{}) do
  params =
    params
    |> Map.put_new("sort_direction", "desc")
    |> Map.put_new("sort_field", "inserted_at")

  {:ok, sort_direction} = Map.fetch(params, "sort_direction")
  {:ok, sort_field} = Map.fetch(params, "sort_field")

  with {:ok, filter} <- Filtrex.parse_params(filter_config(:users), params["user"] || %{}),
       %Scrivener.Page{} = page <- do_paginate_users(filter, params) do
    {:ok,
     %{
       users: page.entries,
       page_number: page.page_number,
       page_size: page.page_size,
       total_pages: page.total_pages,
       total_entries: page.total_entries,
       distance: @pagination_distance,
       sort_field: sort_field,
       sort_direction: sort_direction
     }}
  else
    {:error, error} -> {:error, error}
    error -> {:error, error}
  end
end

defp do_paginate_users(filter, params) do
  User
  |> Filtrex.query(filter)
  |> order_by(^sort(params))
  |> preload(:credential)
  |> paginate(Repo, params, @pagination)
end

defp filter_config(:users) do
  defconfig do
    text :username
    date :inserted_at
  end
end

Maybe I need to extend filter_config/1 and/or Filtrex.parse_params(filter_config(:users), params["user"] || %{})?

I'm facing almost the same problem when trying to order records.

@zberkom
Copy link
Contributor

zberkom commented Oct 18, 2018

@akrisanov Hey, thanks for the question! We have added support for this, but it hasn't made it's way to master yet. I'll work on adding it in early next week.

@chevinbrown
Copy link

@akrisanov will this help?
rcdilorenzo/filtrex#56

@zberkom
Copy link
Contributor

zberkom commented Oct 29, 2018

@akrisanov @chevinbrown I have an open PR to add an example to the Readme as well. #97

@chevinbrown
Copy link

@zberkom will ecto 3 change this?

@zberkom
Copy link
Contributor

zberkom commented Oct 29, 2018

@chevinbrown I have yet to investigate how ecto 3 will effect things. It's on my list to do though :). In the meantime if you have any insight or ideas on how it might change things, I'm all ears.

@darinwilson
Copy link
Contributor

@chevinbrown @zberkom The code in #97 should still work with Ecto 3 🙂

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

No branches or pull requests

4 participants