Searching, Sorting and Pagination in Elixir & Phoenix with Rummage: Part 2

Now that we have set up an application to work with, let’s see how to add Rummage to it. In this part, we will add Rummage.Ecto to our application.


By now, you should have a functioning Phoenix application with a model, view and controller, and its routes defined in the web/router.ex. If you don’t have a Phoenix application setup, I suggest you go to part 1 of this series and follow the instructions on for the setup. The index page of your application should look somewhat like this:

Index page without Rummage

Context: The app that we are working on has products which belong to categories. Therefore, category field on the index page shown above is actually the category_name of the category that a product belongs to.

Adding rummage_ecto to the application

Let us start by adding rummage_ecto in the list of deps to the application’s mix.exs.

Add rummage_ecto to mix.exs

Now, run mix deps.get to fetch rummage_ecto package. Since I have already fetched this dependency, mine is using a locally cached version.

Fetch the package, rummage_ecto, from hex.

Using Rummage in an Ecto model

Now that our application has rummage_ecto as a dependency, it’s time to configure it:

Configure Rummage.Ecto with a default repo and a default per page

we can use it’s features in an Ecto model by adding the following code:

Use Rummage.Ecto to make use of Rummage’s features

Make sure to provide a Repo (that corresponds to the Ecto model that you’re adding Rummage to)either in the config or as an option with the use statement. Same with the per_page variable. For the purposes of this demo, I’ll keep per_page to 2. (So, I don’t have to add a lot of products to show pagination).

NOTE: If you don’t provide any Repo in either the config or the model, then Rummage will default it to AppName.Repo. Similarly, if you don’t provide a per_page, then Rummage will default it to 10. So in this app, we could do without providing a Repo, as our Repo module is “AppName”.Repo (RummageExample.Repo).

Trying out Rummage.Ecto

Now, your Ecto model has Rummage.Ecto support. If you want to play around with Rummage params, open up an iex shell, you can run the following rummage commands:

Search with Rummage without associations
Search with association, where category_name is a field in the parent of Product.
Sort without association
Sort with association where category_name is a field in the parent of Products

You can also use these params with each other in the same Rummage call. Rummage will execute these calls in the order: Search > Sort > Paginate.

Using all Search, Sort and Paginate params in a single Rummage call.

You can also use Rummage with only two hooks: (Example: Search and Sort)

Rummage with Search and Sort opertions

Adding a Rummage call to the controller

Now that we have a way to Search, Sort and Paginate Ecto queries, we can add this to the index action of product_controller.ex to perform rummage operations on the index page.

add rummage call to index action

This will allow us to pass rummage parameters as our browser request parameters.

Now try opening the products index page. You should see a paginate page, depending upon your per_page value.

paginated index

If you pass a url with rummage params:

Url with rummage params

It should give a result that corresponds to the given rummage params:

Paginated and Sorted list of products

Just play around with the url parameters and see everything that you can do. One of the best parts about using Rummage is that all three operations: Search, Sort and Paginate work together and integrate seamlessly.

Now that we have added Rummage.Ecto to our Phoenix project, we will add the view support (Rummage.Phoenix) in the next part.

Click here to go to the previous part.


Up Next:

Coming Soon..

  • Searching, Sorting and Pagination with Elixir & Phoenix: Part 4
  • Advanced Rummage: Utilizing the Rummage Search hook
  • Advanced Rummage: Utilizing the Rummage Sort hook
  • Advanced Rummage: Writing a Custom Rummage.Ecto Hook
  • Advanced Rummage: Writing a Custom Rummage.Phoenix HTML helper
  • Using Rummage with other Libraries: Kerosene
  • Using Rummage with other Libraries: Scrivener
Show your support

Clapping shows how much you appreciated Adi Iyengar’s story.