Skip to content

Commit 0dd35bb

Browse files
authored
Merge pull request #133 from tensiondriven/update-readme-preload-assoc-132
Update README to make it more obvious how to customize the queries.
2 parents c81c1d8 + f92efb3 commit 0dd35bb

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

README.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ without the need to touch the current codebase. It was inspired by django's love
1515
- [Custom Configurations](#configurations)
1616
- [Customize the Side Menu](#side-menu)
1717
- [Customize the Dashboard Page](#dashboard-page)
18-
- [Customize the Index Page](#index-page)
19-
- [Customize the Form Page](#form-page)
18+
- [Customize the Index Pages](#index-page)
19+
- [Customize the Form Pages](#form-page)
2020
- [Custom Form Fields](#custom-form-fields)
21+
- [Customize the Queries](#customize-the-queries)
2122
- [Extensions](#extensions)
22-
- [Customize the Query](#customize-the-query)
2323
- [Embedded Schemas and JSON Fields](#embedded-schemas-and-json-fields)
2424
- [Searching Records](#search)
2525
- [Authorizing Access To Resources](#authorization)
@@ -316,7 +316,7 @@ The maps have the following keys:
316316
- `:assigns` (optional) to hold the assigns for the template. Default to an empty list.
317317
- `:order` is the order of the page among other pages in the side menu.
318318

319-
### Index page
319+
### Index pages
320320

321321
The `index/1` function takes a schema and must return a keyword list of fields and their options.
322322

@@ -390,7 +390,7 @@ end
390390
```
391391

392392

393-
### Form Page
393+
### Form Pages
394394

395395
Kaffy treats the show and edit pages as one, the form page.
396396

@@ -511,6 +511,32 @@ defmodule MyApp.Kaffy.URLField do
511511
end
512512
```
513513

514+
515+
### Customize the Queries
516+
517+
By default Kaffy does a simple Ecto query to retrieve records. You can customize the queries used by Kaffy by using `custom_index_query` and `custom_show_query`. This allows you to preload associations to display associated data on your pages, for example. Attempting to access an association without preloading it first will result in a `Ecto.Association.NotLoaded` exception.
518+
519+
```elixir
520+
defmodule MyApp.Blog.PostAdmin do
521+
def custom_index_query(_conn, _schema, query) do
522+
from(r in query, preload: [:tags])
523+
end
524+
525+
def custom_show_query(_conn, _schema, query) do
526+
case user_is_admin?(conn) do
527+
true -> from(r in query, preload: [:history])
528+
false -> query
529+
end
530+
end
531+
end
532+
```
533+
534+
The `custom_index_query/3` function takes a conn, the schema, and the query to customize, and it must return a query.
535+
It is called when fetching the resources for the index page.
536+
537+
The `custom_show_query/3` is identifical to `custom_index_query/3`, but works when fetching a single resource in the show/edit page.
538+
539+
514540
### Extensions
515541

516542
Extensions allow you to define custom css, javascript, and html.
@@ -552,30 +578,6 @@ config :kaffy,
552578

553579
You can check [this issue](https://github.com/aesmail/kaffy/issues/54) to see an example which uses extensions with custom fields.
554580

555-
### Customize the Query
556-
557-
You can customize the query in case you need it to be more than just simple fetching.
558-
559-
```elixir
560-
defmodule MyApp.Blog.PostAdmin do
561-
def custom_index_query(_conn, _schema, query) do
562-
from(r in query, preload: [:tags])
563-
end
564-
565-
def custom_show_query(_conn, _schema, query) do
566-
case user_is_admin?(conn) do
567-
true -> from(r in query, preload: [:history])
568-
false -> query
569-
end
570-
end
571-
end
572-
```
573-
574-
The `custom_index_query/3` function takes a conn, the schema, and the query to customize, and it must return a query.
575-
It is called when fetching the resources for the index page.
576-
577-
The `custom_show_query/3` is identifical to `custom_index_query/3`, but works when fetching a single resource in the show/edit page.
578-
579581
### Embedded Schemas and JSON Fields
580582

581583
Kaffy has support for ecto's [embedded schemas](https://hexdocs.pm/ecto/Ecto.Schema.html#embedded_schema/1) and json fields. When you define a field as a `:map`, Kaffy will automatically display a textarea with a placeholder to hint that JSON content is expected. When you have an embedded schema, Kaffy will try to render each field inline with the form of the parent schema.

0 commit comments

Comments
 (0)