Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Seems the documentation needs to be updated when it comes to using a custom filter module #17

Open
acrolink opened this issue Jan 8, 2022 · 0 comments

Comments

@acrolink
Copy link

acrolink commented Jan 8, 2022

After considerable trial and error, these are the correct instructions for this to work:

// patient_filters.ex

defmodule PatientFilters do
  use Filterable.DSL
  use Filterable.Ecto.Helpers
  use Filterable
  import Ecto.Query

  field(:name)
  field(:gender)

  paginateable(per_page: 10)

  filterable do
    @options param: :name
    filter name(query, value) do
      query |> where([u], ilike(u.name, ^"%#{value}%"))
    end

    @options param: :gender
    filter gender(query, value) do
      query |> where(gender: ^value)
    end
  end
end

And in controller:

  def index(conn, _params) do
    with {:ok, query, filter_values} <- PatientFilters.apply_filters(Patient, _params),
         patients <- Repo.all(query),
         do: render(conn, "index.json", patients: patients, meta: filter_values)
  end
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant