Full Phoenix Support for Rummage. It can be used for searching, sorting and paginating collections in phoenix.
Rummage.Phoenixis a support framework for
Phoenixthat can be used to manipulate
Phoenixcollections and
Ectomodels with Search, Sort and Paginate operations.
It accomplishes the above operations by using
Rummage.Ecto, to paginate
Ectoqueries and adds Phoenix and HTML support to views and controllers. For information on how to configure
Rummage.Ectovisit this page.
The best part about rummage is that all the three operations:
Search,
Sortand
Paginateintegrate seamlessly and can be configured separately. To check out their seamless integration, please check the information below.
NOTE:
Rummageis 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
Rummageto use your
Searchmodule for that model. This is why Rummage has been made configurable.
Search, Sort and Paginate seamlessly in Phoenix!
This is available in Hex, the package can be installed as:
Add
rummage_phoenixto your list of dependencies in
mix.exs:
def deps do [ {:rummage_phoenix, "~> 1.2.0"} ] end
default_per_page)
Rumamge.Phoenixcan be configured globally with a
default_per_pagevalue (which can be overriden for a model). This is NOT the preferred way to set
default_per_pageas 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_pagefor all the models, add it to
modelfunction in
web.ex
Add
rummage_phoenixconfig to your list of configs in
dev.exs:
config :rummage_phoenix, Rummage.Phoenix, default_per_page: 5
Rummage.Controllerin to controller module:
defmodule MyApp.ProductController do use MyApp.Web, :controller use Rummage.Phoenix.Controller# More code below....
end
indexaction in the controller:
def index(conn, params) do {query, rummage} = Product |> Rummage.Ecto.rummage(params["rummage"])products = Repo.all(query) render conn, "index.html", products: products, rummage: rummage
end
searchpath in the
router.ex(no need to define the action):
scope "/", MyApp do pipe_through :browser # Use the default browser stackget "/", PageController, :index resources "/products", ProductController
end
Doing this itself will allow you to search, sort and paginate by updating
paramson the request. Please check the screenshots below for details
Rummage.Viewin to a view module:
defmodule MyApp.ProductView do use MyApp.Web, :view use Rummage.Phoenix.View# More code below...
end
Note: If you get a "MyApp.Router.Helpers is not available" exception, you can provide your router with:
defmodule MyApp.ProductView do use MyApp.Web, :view use Rummage.Phoenix.View, helpers: MyApp.Web.Router.Helpers# More code below...
end
or through the config:
config :rummage_phoenix, Rummage.Phoenix, [ default_helpers: MyApp.Web.Router.Helpers, ]
Note: If the path helper name is incorrect, you can specify it with:
defmodule MyApp.ProductView do use MyApp.Web, :view use Rummage.Phoenix.View, struct: "special_product" # will become special_product_path# More code below...
end
index.html.eexto render
Rummagepagination links (Make sure that you pass
rummageto the views from the
indexaction in the controller) :
Reload and this is how your page should look:
index.html.eexwith sort links (Make sure that the headers are actual columns in the table in the database.)
Replace this:
elixir Name Price Category
With:
elixir
OR for Sort by associations:
elixir
Reload and this is how your page should look with sortable links instead of just table headers:
NOTE: Currently working on adding better elements to the views, soon the text arrow in the sort links will be replaced by an icon
index.html.eexwith searchable fields:
OR for Search by associations:
Reload and your page should look somewhat like this:
Rummageis that all the three hooks/operations integrate seamlessly without affecting each other's functionality and therefore, you have a page looking somewhat like this:
Default
Custom pagination params