Skip to content

Commit bbbdad7

Browse files
authored
Merge branch 'master' into feature/dynamic-primary-keys
2 parents 77bc367 + a2e20b0 commit bbbdad7

File tree

8 files changed

+75
-4
lines changed

8 files changed

+75
-4
lines changed

lib/kaffy/resource_admin.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@ defmodule Kaffy.ResourceAdmin do
1313
All functions are optional.
1414
"""
1515

16+
@doc """
17+
Allows a description to be injected into the index view of a resource's index
18+
view. This is helpful when you want to provide information to the user about the index page.
19+
Return value can be a string, or {:safe, string) tuple. If :safe tuple is used, HTML will be rendered.
20+
21+
`index_description/1` takes a schema and must return a `String.t()` or tuple `{:safe, String.t()}`.
22+
23+
If `index_description/1` is not defined, Kaffy will return nil.
24+
25+
## Examples:
26+
27+
```elixir
28+
def index_description(resource) do
29+
"This will show up on the index page."
30+
end
31+
32+
def index_description(resource) do
33+
{:safe, "This will show up on the index page. <b>This will be bold!</b>."}
34+
end
35+
```
36+
"""
37+
def index_description(resource) do
38+
Utils.get_assigned_value_or_default(
39+
resource,
40+
:index_description,
41+
ResourceSchema.index_description(resource)
42+
)
43+
end
44+
1645
@doc """
1746
`index/1` takes the schema module and should return a keyword list of fields and
1847
their options.

lib/kaffy/resource_query.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ defmodule Kaffy.ResourceQuery do
135135

136136
Enum.reduce(search_fields, query, fn
137137
{association, fields}, q ->
138-
query = from(s in q, join: a in assoc(s, ^association))
138+
query = from(s in q, left_join: a in assoc(s, ^association))
139139

140140
Enum.reduce(fields, query, fn f, current_query ->
141141
from([..., r] in current_query,

lib/kaffy/resource_schema.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ defmodule Kaffy.ResourceSchema do
1919
end
2020
end
2121

22+
def index_description(_schema), do: nil
23+
2224
def index_fields(schema) do
2325
Keyword.drop(fields(schema), fields_to_be_removed(schema))
2426
end
@@ -311,6 +313,9 @@ defmodule Kaffy.ResourceSchema do
311313
{_f, %{type: :map}} ->
312314
true
313315

316+
{_f, %{type: {:array, _}}} ->
317+
true
318+
314319
f when is_atom(f) ->
315320
f == :map
316321

lib/kaffy/utils.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ defmodule Kaffy.Utils do
99
env(:admin_title, "Kaffy")
1010
end
1111

12+
@doc """
13+
Returns the :admin_footer config if present, otherwise returns default copyright.
14+
"""
15+
@spec footer() :: String.t() | {:safe, String.t()}
16+
def footer() do
17+
env(:admin_footer, "Copyright © 2022 Kaffy. All rights reserved.")
18+
end
19+
1220
@doc """
1321
Returns the static path to the asset.
1422
"""
@@ -355,7 +363,8 @@ defmodule Kaffy.Utils do
355363
stylesheets: stylesheets ++ acc.stylesheets,
356364
javascripts: javascripts ++ acc.javascripts
357365
}
358-
end)
366+
end
367+
)
359368
end
360369

361370
defp env(key, default \\ nil) do

lib/kaffy_web/templates/layout/app.html.eex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@
155155

156156
<footer class="footer">
157157
<div class="d-sm-flex justify-content-center justify-content-sm-between">
158-
<span class="text-muted text-center text-sm-left d-block d-sm-inline-block">Copyright © 2022 Kaffy. All rights reserved.</span>
158+
<%= if copyright = Kaffy.Utils.footer() do %>
159+
<span class="text-muted text-center text-sm-left d-block d-sm-inline-block"><%= copyright %></span>
160+
<% end %>
159161
</div>
160162
</footer>
161163

lib/kaffy_web/templates/resource/index.html.eex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323
<div class="card-body table-responsive w-100">
2424
<div class="card-description">
25+
<%= if index_description = Kaffy.ResourceAdmin.index_description(@my_resource) do %>
26+
<div class="row">
27+
<div class="col-auto mr-auto">
28+
<p><%= index_description %></p>
29+
</div>
30+
</div>
31+
<% end %>
32+
2533
<div class="row">
2634

2735
<div class="col-auto mr-auto">

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Kaffy.MixProject do
22
use Mix.Project
33

44
@source_url "https://github.com/aesmail/kaffy"
5-
@version "0.9.5-rc.0"
5+
@version "0.10.0-rc.0"
66

77
def project do
88
[

test/resource_admin_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ defmodule Kaffy.ResourceAdminTest do
88

99
defmodule CactusAdmin do
1010
def plural_name(_), do: "Cacti"
11+
def index_description(_), do: {:safe, "Person Admin <b>Description</b>"}
1112
end
1213

1314
defmodule Person do
1415
end
1516

1617
defmodule PersonAdmin do
18+
def index_description(_), do: "Person Admin Description"
1719
end
1820

1921
defmodule PetAdmin do
@@ -94,4 +96,20 @@ defmodule Kaffy.ResourceAdminTest do
9496
assert ResourceAdmin.deserialize_id([schema: Owner, admin: OwnerETFAdmin], "g2gCYQFhAg") == [person_id: 1, pet_id: 2]
9597
end
9698
end
99+
100+
describe "index_description/1" do
101+
test "nil if index_description is not defined as function in admin" do
102+
refute ResourceAdmin.index_description(schema: Nested.Node, admin: NestedNodeAdmin)
103+
end
104+
105+
test "string if index_description is defined as function in admin" do
106+
assert ResourceAdmin.index_description(schema: Person, admin: PersonAdmin) ==
107+
"Person Admin Description"
108+
end
109+
110+
test "{:safe, html} if index_description is defined as function in admin" do
111+
assert ResourceAdmin.index_description(schema: Cactus, admin: CactusAdmin) ==
112+
{:safe, "Person Admin <b>Description</b>"}
113+
end
114+
end
97115
end

0 commit comments

Comments
 (0)