Coder Social home page Coder Social logo

Select field in search form about rummage_phoenix HOT 2 OPEN

fjahr avatar fjahr commented on May 19, 2024
Select field in search form

from rummage_phoenix.

Comments (2)

ericsullivan avatar ericsullivan commented on May 19, 2024 2

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

from rummage_phoenix.

fjahr avatar fjahr commented on May 19, 2024

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.

from rummage_phoenix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.