You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31-29Lines changed: 31 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,11 @@ without the need to touch the current codebase. It was inspired by django's love
15
15
-[Custom Configurations](#configurations)
16
16
-[Customize the Side Menu](#side-menu)
17
17
-[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)
20
20
-[Custom Form Fields](#custom-form-fields)
21
+
-[Customize the Queries](#customize-the-queries)
21
22
-[Extensions](#extensions)
22
-
-[Customize the Query](#customize-the-query)
23
23
-[Embedded Schemas and JSON Fields](#embedded-schemas-and-json-fields)
24
24
-[Searching Records](#search)
25
25
-[Authorizing Access To Resources](#authorization)
@@ -316,7 +316,7 @@ The maps have the following keys:
316
316
-`:assigns` (optional) to hold the assigns for the template. Default to an empty list.
317
317
-`:order` is the order of the page among other pages in the side menu.
318
318
319
-
### Index page
319
+
### Index pages
320
320
321
321
The `index/1` function takes a schema and must return a keyword list of fields and their options.
322
322
@@ -390,7 +390,7 @@ end
390
390
```
391
391
392
392
393
-
### Form Page
393
+
### Form Pages
394
394
395
395
Kaffy treats the show and edit pages as one, the form page.
396
396
@@ -511,6 +511,32 @@ defmodule MyApp.Kaffy.URLField do
511
511
end
512
512
```
513
513
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
+
defmoduleMyApp.Blog.PostAdmindo
521
+
defcustom_index_query(_conn, _schema, query) do
522
+
from(r in query, preload: [:tags])
523
+
end
524
+
525
+
defcustom_show_query(_conn, _schema, query) do
526
+
caseuser_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
+
514
540
### Extensions
515
541
516
542
Extensions allow you to define custom css, javascript, and html.
@@ -552,30 +578,6 @@ config :kaffy,
552
578
553
579
You can check [this issue](https://github.com/aesmail/kaffy/issues/54) to see an example which uses extensions with custom fields.
554
580
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
-
defmoduleMyApp.Blog.PostAdmindo
561
-
defcustom_index_query(_conn, _schema, query) do
562
-
from(r in query, preload: [:tags])
563
-
end
564
-
565
-
defcustom_show_query(_conn, _schema, query) do
566
-
caseuser_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
-
579
581
### Embedded Schemas and JSON Fields
580
582
581
583
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