If you're looking for full Phoenix support, Rummage.Phoenix uses Rummage.Ecto and adds HTML and Controller support
to it. You can check Rummage.Phoenix out by clicking here
Please refer for CHANGELOG for version specific changes
Rummage.Ecto is a light weight, but powerful framework that can be used to alter Ecto queries with Search, Sort and Paginate operations.
It accomplishes the above operations by using Hooks, which are modules that implement Rummage.Ecto.Hook behavior.
Each operation: Search, Sort and Paginate have their hooks defined in Rummage. By doing this, Rummage is completely
configurable.
For example, if you don't like one of the hooks of Rummage, but you do like the other two, you can configure Rummage to not use it and write your own custom
hook.
NOTE: Rummage is not like Ransack, and it doesn't intend to be like Ransack. It doesn't define functions based on search parameters.
If you'd like to have something like that, you can always configure Rummage to use your Search module for that model. This
is why Rummage has been made configurable.
To see an example usage of rummage, check this repository.
This package is available in Hex, and can be installed as:
-
Add
rummage_ectoto your list of dependencies inmix.exs:def deps do [{:rummage_ecto, "~> 2.0.0-rc.0"}] end
- Using Rummage.Phoenix: Part 2
- Using the Rummage Search hook
- Using the Rummage Sort hook
- Writing a Custom Rummage.Ecto Hook
- Writing a Custom Rummage.Phoenix HTML helper
- Hooks are modules (that use
Rummage.Ecto.Hook) and implement callbacks forRummage.Ecto.Hookbehaviour. Each ecto operation which can transform the query is defined by aHook. Hooks haverun/2function using which they can transform anEcto.Queryablevariable and haveformat_params/3function using which they can transform params passed to them throughrummage_ecto
-
NOTE: This is Optional. If no configuration is provided,
Rummagewill use default hooks andAppName.Repoas the repo -
If you want to override any of the
Rummagedefault hooks, addrummage_ectoconfig to your list of configs indev.exs:config :rummage_ecto, Rummage.Ecto, search: MyApp.SearchModule
-
For configuring a repo:
config :rummage_ecto, Rummage.Ecto, repo: MyApp.Repo # This can be overridden per model basis, if need be.
-
Other config options are:
repo,sort,paginate,per_page -
Rummage.Ectocan be configured globally with aper_pagevalue (which can be overridden for a model). If you want to set differentper_pagefor different the models, add it tomodel.exsfile while usingRummage.Ectoas shown in the Advanced Usage Section.
Rummage.Ecto comes with a lot of powerful features which are available right away,
without writing a whole lot of code.
Below are the ways Rummage.Ecto can be used:
- Add the
Repoof your app and the desiredper_page(if using Rummage's Pagination) to therummage_ectoconfiguration inconfig.exs:
config :rummage_ecto, Rummage.Ecto,
repo: MyApp.Repo,
per_page: 10- And you should be able to use
Rummage.Ectowith anyEctomodel.
- If you'd like to override any of
Rummage's default hooks with your custom hook, add theCustomHookof your app with the desired operation to therummage_ectoconfiguration inconfig.exs:
config :rummage_ecto, Rummage.Ecto,
repo: MyApp.Repo,
search: MyApp.SearchModule,
paginate: MyApp.PaginateModule- When using
Rummage.Ectowith an app that has multipleRepos, or when there's a need to configureRepoper model basis, it can be passed along with with the call toRummage.Ecto. This overrides the default repo set in the configuration:
{queryable, rummage} = Product
|> Rummage.Ecto.rummage(rummage, repo: MyApp.Repo2)- And you should be able to use
Rummage.EctowithProductmodel which is in a differentRepothan the default one.
- Setting up the application above will allow us to do the following:
rummage = %{
search: %{field_1 => %{search_type: :like, search_term: "field_!"}},
sort: %{field: :field1, order: :asc},
paginate: %{per_page: 5, page: 1}
}
{queryable, rummage} = Product
|> Rummage.Ecto.rummage(rummage)
products = queryable
|> Product.another_operation # <-- Since `Rummage` is Ecto, we can pipe the result queryable into another queryable operation.
|> Repo.all- Rummage responds to
paramswith keys:search,sortand/orpaginate. It doesn't need to have all the keys, or any keys for that matter. If invalid keys are passed, they won't alter any operations in rummage. Here's an example ofRummageparams:
rummage = %{
search: %{field_1 => %{search_type: :like, search_term: "field_!"}},
sort: %{field: :field1, order: :asc},
paginate: %{per_page: 5, page: 1}
}