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

Select field in search form #60

Open
fjahr opened this issue Mar 28, 2018 · 2 comments
Open

Select field in search form #60

fjahr opened this issue Mar 28, 2018 · 2 comments
Assignees

Comments

@fjahr
Copy link

fjahr commented Mar 28, 2018

Hi,

I would like to have a select field in the search form with some predefined options. I have been looking into the implementation of search_form but it does not seem to be possible at the moment. Unless I am missing something?

If it is possible (maybe through a workaround) I would be helpful for any pointers on how to achieve this and may add some documentation for it when I get it to work.

If it is not possible I would possibly work on implementing this. Would you be interested in merging such a PR? It would be interesting to get a hint on how you would like this to be implemented in that case.

Thanks!

@ericsullivan
Copy link
Contributor

I had the same issue and that's where the newest PRs came from (#59 & annkissam/rummage_ecto#66)

They work (I'm using them in production code), but they need to be documented. Basically, it gets rid of the search_form and you can use your own. I think @aditya7iyengar would like to document it better before officially releasing it, so I'd say you don't need to work on implementing this, but it might be tricky to use. In short, if gives the sort and search fields a name, which gets used in the view. It translates those name into a query in a rummage module you'll create for each schema that you want to rummage. That makes the views much simpler, and you can then use your own search form. I'll try to describe it briefly:

in mix.exs

{:rummage_ecto, "~> 1.2", git: "https://github.com/aditya7iyengar/rummage_ecto"},
{:rummage_phoenix, "~> 1.2", git: "https://github.com/aditya7iyengar/rummage_phoenix", branch: "scope_whitelist"},

in your index:

  <%= form_for @rummage.changeset, your_controller_path(@conn, :index), [as: "rummage", method: :get], fn f -> %>
    <%= inputs_for f, :search, fn f -> %>
      <div class="form-group">
        <%= label f, :first_name, "First Name", class: "control-label" %>
        <%= text_input f, :first_name, class: "form-control" %>
      </div>
    <% end %>

    <div class="form-group">
      <%= submit "Search", class: "btn btn-default" %>
    </div>
  <% end %>

in your_controller:

  {rummage, records} = YourApp.Rummage.YourSchema.rummage(params["rummage"], query: query)

  render(conn, "index.html", records: records, rummage: rummage)
defmodule YourApp.Rummage.YourSchema do
  use Rummage.Schema,
    paginate: YourApp.Rummage.Paginate,
    sort: YourApp.Rummage.YourSchema.Sort,
    search: YourApp.Rummage.YourSchema.Search,
    schema: YourApp.YourSchema
end

defmodule YourApp.Rummage.YourSchema.Sort do
  use Rummage.Schema.Sort,
    default_name: "email",
    handlers: [
      first_name: %{ci: true},
      last_name: %{ci: true},
      email: %{ci: true},
    ]
end

defmodule YourApp.Rummage.YourSchema.Search do
  use Rummage.Schema.Search,
    handlers: [
      first_name: %{search_type: :ilike},
      last_name: %{search_type: :ilike},
      email: %{search_type: :ilike},
    ]
end

For all the rummage schema's there's an example here: https://github.com/aditya7iyengar/rummage_ecto/pull/66/files#diff-b99205d171d4be5eb3ae3715078ec7cb

@fjahr
Copy link
Author

fjahr commented Mar 30, 2018

Thanks a lot @ericsullivan ! Your hints were a great starting point. I got it to work now but it was a bit tricky as you mentioned.

Maybe it is helpful that I point out what tripped me up for testing or if anyone else is trying this:

  1. The form_for example you provided did not work for me because it did not nest the parameters properly. Instead of ["rummage"]["search"]["first_name"] it results in ["rummage"]["first_name"]. In general reusing the f variable should not be reused but replaced by s or so in the nested search form. But that gave me an error. I ended up providing an explicit name: "rummage[search][first_name]" on the input to get it to work for now but I guess the rummage changeset needs to be implemented differently to get this to work properly. I did not have time to look into it further so far.
  2. The change_assoc_to_a_one_member_list method did not handle the parameters in my basic case properly: From how I used it I got rummage = %{"search" => %{"field" => "my search text"}} which leads to a FunctionClauseError (Access.get/3). Since you already mention in the comment that the function is temporary I ended up commenting it out because I don't worry about search in assocs for now.

So a bit hacky but it works for now. I will follow the development of the new api and contribute when I can.

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

3 participants