Rummage.Phoenix

Build StatusHex Versionhex.pm downloadsHex docsdocsMIT licensed

Rummage.Phoenix is a support framework for Phoenix that can be used to manipulate Phoenix collections and Ecto models with Search, Sort and Paginate operations.

It accomplishes the above operations by using Rummage.Ecto, to paginate Ecto queries and adds Phoenix and HTML support to views and controllers. For information on how to configure Rummage.Ecto visit this page.

The best part about rummage is that all the three operations: Search, Sort and Paginate integrate seamlessly and can be configured separately. To check out their seamless integration, please check the information below.

NOTE: Rummage is not like Ransack, and doesn't intend to be either. It doesn't add functions based on search params. If you'd like to have that for a model, you can always configure Rummage to use your Search module for that model. This is why Rummage has been made configurable.


Search, Sort and Paginate seamlessly in Phoenix!

phoenix all together


Installation

This is available in Hex, the package can be installed as:

Blogs

Current Blogs:

Coming up next:

Configuration (Optional, Not the preferred way to set default_per_page)

Rumamge.Phoenix can be configured globally with a default_per_page value (which can be overriden for a model). This is NOT the preferred way to set default_per_page as it might lead to conflicts. It is recommended to do it per model as show below in the Initial Setup section. If you want to set default_per_page for all the models, add it to model function in web.ex

Usage (The screenshots correspond to version 0.6.0, soon there will be screenshots for version 1.0.0)

Initial Setup

  defmodule MyApp.Product do
    use MyApp.Web, :model
    use Rummage.Ecto

    # More code below....
  end
  defmodule MyApp.ProductController do
    use MyApp.Web, :controller
    use Rummage.Phoenix.Controller

    # More code below....
  end
  def index(conn, params) do
    {query, rummage} = Product
      |> Product.rummage(params["rummage"])

    products = Repo.all(query)

    render conn, "index.html",
      products: products,
      rummage: rummage
  end
  scope "/", MyApp do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    resources "/products", ProductController
  end

Doing this itself will allow you to search, sort and paginate by updating params on the request. Please check the screenshots below for details

Using Rummage.ViewHelpers

  defmodule MyApp.ProductView do
    use MyApp.Web, :view
    use Rummage.Phoenix.View

    # More code below...
  end
  <%= pagination_link(@conn, @rummage) %>

Reload and this is how your page should look:

phoenix pagination

Replace this:

    <th>Name</th>
    <th>Price</th>
    <th>Category</th>

With:

    <th><%= sort_link @conn, @rummage, [field: :name, ci: true] %></th>
    <th><%= sort_link @conn, @rummage, [field: :price] %></th>

OR for Sort by associations:

    <th><%= sort_link @conn, @rummage, [field: :category_name, name: "Category Name", assoc: ["category"]] %></th>

Reload and this is how your page should look with sortable links instead of just table headers:

phoenix sorting

NOTE: Currently working on adding better elements to the views, soon the text arrow in the sort links will be replaced by an icon

  <%= search_form(@conn, @rummage, [fields:
  [
    name: %{label: "Search by Product Name", search_type: "ilike"},
    price: %{label: "Search by Price", search_type: "eq"},
  ], button_class: "btn",
  ]) %>

OR for Search by associations:

  <%= search_form(@conn, @rummage, [fields:
  [
    category_name: %{label: "Search by Category Name", search_type: "ilike", assoc: ["category"]}
  ], button_class: "btn",
  ]) %>

Reload and your page should look somewhat like this: phoenix searching

phoenix all together

More Screenshots

Before rummage

before pagination

After Pagination:

after pagination

custom pagination paramscustom pagination page

After Pagination View:

After Sort:

custom sort params asccustom sort page asc

custom sort params desccustom sort page desc

After Sort View:

phoenix sorting

After Search:

custom search paramscustom search page

After Search View:

phoenix searching