forked from tfwright/live_admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.exs
549 lines (450 loc) · 13.5 KB
/
dev.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
Logger.configure(level: :debug)
pg_url = System.get_env("PG_URL") || "postgres:postgres@127.0.0.1"
Application.put_env(:live_admin, Demo.Repo,
url: "ecto://#{pg_url}/phx_admin_dev"
)
defmodule Demo.Repo do
use Ecto.Repo, otp_app: :live_admin, adapter: Ecto.Adapters.Postgres
def prefixes, do: ["public", "alt"]
end
_ = Ecto.Adapters.Postgres.storage_up(Demo.Repo.config())
Application.put_env(:live_admin, DemoWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
live_view: [signing_salt: "hMegieSe"],
http: [port: System.get_env("PORT") || 4000],
debug_errors: true,
check_origin: false,
watchers: [
npm: ["run", "watch", cd: "assets"],
npx: [
"tailwindcss",
"--input=css/app.css",
"--output=../dist/css/app.css",
"--postcss",
"--watch",
cd: "assets"
],
npx: [
"tailwindcss",
"--input=css/default_overrides.css",
"--output=../dist/css/default_overrides.css",
"--postcss",
"--watch",
cd: "assets"
]
],
live_reload: [
patterns: [
~r"dist/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"lib/live_admin/components/.*(ex)$",
~r"lib/live_admin/templates/.*/.*(ex)$",
~r"lib/live_admin/.*(ex)$"
]
],
pubsub_server: Demo.PubSub
)
Application.put_env(:live_admin, :ecto_repo, Demo.Repo)
Application.put_env(:live_admin, :immutable_fields, [:inserted_at])
Application.put_env(:live_admin, :css_overrides, {DemoWeb.Renderer, :render_css, []})
Application.put_env(:live_admin, :gettext_backend, Demo.Gettext)
defmodule DemoWeb.Renderer do
use Phoenix.HTML
def render_field(record, field, _session) do
record
|> Map.fetch!(field)
|> case do
bool when is_boolean(bool) ->
if bool, do: "Yes", else: "No"
date = %Date{} ->
Calendar.strftime(date, "%a, %B %d %Y")
_ ->
record
|> Map.fetch!(field)
|> case do
val when is_binary(val) -> val
val -> inspect(val, pretty: true)
end
end
end
def render_css(%{metadata: %{"css_theme" => "dark"}}) do
"""
body {
background-color: #444;
color: #fff;
}
.nav {
background-color: #444;
}
.resource__action--btn {
background-color: #aaa;
border-color: #fff;
color: #000;
}
.resource__action--btn:hover {
background-color: #ccc;
border-color: #iii;
}
.resource__menu--drop nav {
color: #000;
background-color: #ccc;
border-color: #iii;
}
.resource__header {
background-color: #bbb;
color: #000
}
.nav a:hover {
background-color: #ccc;
}
.toast__container--error {
border-color: violet;
}
.toast__container--success {
border-color: chartreuse;
}
"""
end
def render_css(_) do
__DIR__
|> Path.join("dist/css/default_overrides.css")
|> File.read!()
end
end
defmodule DemoWeb.PageController do
import Plug.Conn
def init(opts), do: opts
def call(conn, :index) do
content(conn, """
<h2>LiveAdmin Dev</h2>
<a href="/admin">Users</a>
<a href="/posts-admin">Posts</a>
""")
end
defp content(conn, content) do
conn
|> put_resp_header("content-type", "text/html")
|> send_resp(200, "<!doctype html><html><body>#{content}</body></html>")
end
end
defmodule Demo.Accounts.User.Settings.Config do
use Ecto.Schema
@primary_key false
embedded_schema do
field :key, :string
field :val, :string
field :good, :boolean
field :legal, :boolean
end
end
defmodule Demo.Accounts.User.Settings do
use Ecto.Schema
embedded_schema do
field :some_option, :string
embeds_many :configs, __MODULE__.Config, on_replace: :delete
end
end
defmodule Demo.Accounts.User.Profile do
use Ecto.Schema
use LiveAdmin.Resource, create_with: false
schema "user_profiles" do
belongs_to :user, Demo.Accounts.User, type: :binary_id
end
end
defmodule Demo.Accounts.User do
use Ecto.Schema
@primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field :name, :string
field :email, :string
field :active, :boolean
field :birth_date, :date
field :stars_count, :integer
field :private_data, :map
field :encrypted_password, :string
field :status, Ecto.Enum, values: [:active, :suspended]
field :roles, {:array, Ecto.Enum}, values: [:admin, :staff]
field :rating, :float
field :password, :string, virtual: true
embeds_one :settings, Demo.Accounts.User.Settings, on_replace: :delete
has_many :posts, Demo.Posts.Post
timestamps(updated_at: false)
end
end
defmodule Demo.Posts.Post.Version do
use Ecto.Schema
@primary_key false
embedded_schema do
field :body, :string
field :tags, {:array, :string}
timestamps(updated_at: false)
end
end
defmodule Demo.Posts.Post do
use Ecto.Schema
use LiveAdmin.Resource,
immutable_fields: [:disabled_user_id],
tasks: [:fail],
validate_with: :validate,
update_with: :update,
ecto_repo: Demo.Repo
import Ecto.Changeset
schema "posts" do
field :title, :string
field :body, :string
field :tags, {:array, :string}, default: []
field :categories, {:array, Ecto.Enum}, values: [:personal, :work]
field :status, Ecto.Enum, values: [:draft, :archived, :live]
field :metadata, :map
embeds_many :previous_versions, __MODULE__.Version, on_replace: :delete
belongs_to :user, Demo.Accounts.User, type: :binary_id
belongs_to :disabled_user, Demo.Accounts.User, type: :binary_id
timestamps(updated_at: false)
end
def fail(_) do
{:error, "failed"}
end
def validate(changeset, _) do
changeset
|> Ecto.Changeset.validate_required([:title, :body, :user_id])
|> Ecto.Changeset.validate_length(:title, max: 10, message: "cannot be longer than 10 characters")
end
def update(record, params, _) do
record
|> Ecto.Changeset.cast(params, [:title, :body, :user_id, :inserted_at, :tags, :categories])
|> Ecto.Changeset.cast_embed(:previous_versions, with: fn version, params ->
Ecto.Changeset.cast(version, params, [:body, :tags, :inserted_at])
end)
|> Ecto.Changeset.validate_required([:title, :body, :user_id, :inserted_at])
|> Ecto.Changeset.validate_length(:title, max: 10, message: "cannot be longer than 10 characters")
|> Ecto.Changeset.validate_change(:title, fn _, new_title ->
if !String.contains?(new_title, record.title) do
[title: "must contain original"]
else
[]
end
end)
|> Demo.Repo.update()
end
end
defmodule Demo.Populator do
import Ecto.Query
alias Demo.Repo
def reset do
teardown()
run()
end
def run do
Enum.each(1..100, fn _ ->
%Demo.Accounts.User{
name: Faker.Person.name(),
email: "#{Ecto.UUID.generate()}@example.com",
settings: %{},
active: true,
birth_date: ~D[1999-12-31],
stars_count: Enum.random(0..100),
private_data: %{},
encrypted_password: :crypto.strong_rand_bytes(16) |> Base.encode16(),
posts: [
%Demo.Posts.Post{
title: Faker.Lorem.paragraph(1) |> String.slice(0..9),
body: Faker.Lorem.paragraphs() |> Enum.join("\n\n"),
disabled_user: get_user_if(:rand.uniform(2) == 1)
}
]
}
|> Demo.Repo.insert!()
end)
end
defp teardown do
Repo.delete_all(Demo.Accounts.User)
Repo.delete_all(Demo.Posts.Post)
Repo.delete_all(Demo.Accounts.User.Profile)
end
defp get_user_if(true), do: from(Demo.Accounts.User, order_by: fragment("RANDOM()"), limit: 1) |> Demo.Repo.one()
defp get_user_if(false), do: nil
end
defmodule DemoWeb.CreateUserForm do
use Phoenix.LiveComponent
use Phoenix.HTML
import LiveAdmin, only: [route_with_params: 2]
import LiveAdmin.ErrorHelpers
@impl true
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:changeset, Ecto.Changeset.change(%Demo.Accounts.User{}))
{:ok, socket}
end
@impl true
def render(assigns) do
assigns = assign(assigns, :enabled, Enum.empty?(assigns.changeset.errors))
~H"""
<div>
<.form
:let={f}
for={@changeset}
as={:params}
phx-change="validate"
phx-submit="create"
phx-target={@myself}
class="resource__form"
>
<div class={"field__group"}>
<%= label(f, :name, class: "field__label") %>
<%= textarea(f, :name, rows: 1, class: "field__text") %>
<%= error_tag(f, :name) %>
</div>
<div class={"field__group"}>
<%= label(f, :email, class: "field__label") %>
<%= textarea(f, :email, rows: 1, class: "field__text") %>
<%= error_tag(f, :email) %>
</div>
<div class={"field__group"}>
<%= label(f, :password, class: "field__label") %>
<%= password_input(f, :password, class: "field__text", value: input_value(f, :password)) %>
<%= error_tag(f, :password) %>
</div>
<div class={"field__group"}>
<%= label(f, :password_confirmation, class: "field__label") %>
<%= password_input(f, :password_confirmation, class: "field__text") %>
<%= error_tag(f, :password_confirmation) %>
</div>
<div class="form__actions">
<%= submit("Save",
class: "resource__action#{if !@enabled, do: "--disabled", else: "--btn"}",
disabled: !@enabled
) %>
</div>
</.form>
</div>
"""
end
@impl true
def handle_event(
"validate",
%{"params" => params},
%{assigns: %{changeset: changeset}} = socket
) do
changeset =
changeset.data
|> Ecto.Changeset.cast(params, [:name, :email, :password])
|> Ecto.Changeset.validate_required([:name, :email, :password])
|> Ecto.Changeset.validate_confirmation(:password)
|> Map.put(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end
@impl true
def handle_event("create", %{"params" => params}, socket = %{assigns: assigns}) do
socket =
%Demo.Accounts.User{}
|> Ecto.Changeset.cast(params, [:name, :email, :stars_count, :roles])
|> Ecto.Changeset.validate_required([:name, :email])
|> Ecto.Changeset.unique_constraint(:email)
|> Demo.Repo.insert(prefix: assigns.prefix)
|> case do
{:ok, _} -> push_redirect(socket, to: route_with_params(assigns, params: [prefix: assigns.prefix]))
{:error, changeset} -> assign(socket, changeset: changeset)
end
{:noreply, socket}
end
end
defmodule DemoWeb.UserAdmin do
use LiveAdmin.Resource,
schema: Demo.Accounts.User,
hidden_fields: [:private_data],
immutable_fields: [:encrypted_password, :inserted_at],
components: [new: DemoWeb.CreateUserForm],
label_with: :name,
actions: [:deactivate, :fake, :fake],
tasks: [:regenerate_passwords],
render_with: :render_field
def deactivate(user, _) do
user
|> Ecto.Changeset.change(active: false)
|> Demo.Repo.update()
|> case do
{:ok, user} -> {:ok, user}
error -> error
end
end
def render_field(user, :email, _) do
Phoenix.HTML.Link.link(user.email, to: "mailto:\"#{user.name}\"<#{user.email}>")
end
def render_field(record, field, session) do
DemoWeb.Renderer.render_field(record, field, session)
end
def regenerate_passwords(_) do
Demo.Accounts.User
|> Demo.Repo.all()
|> Enum.each(fn user ->
user
|> Ecto.Changeset.change(encrypted_password: :crypto.strong_rand_bytes(16) |> Base.encode16())
|> Demo.Repo.update()
end)
{:ok, "updated"}
end
end
defmodule DemoWeb.PostsAdmin.Home do
use Phoenix.LiveComponent
@impl true
def render(assigns) do
~H"""
<div class="flex h-full items-center justify-center">
<div class="w-1/2">
This is only for managing posts
</div>
</div>
"""
end
end
defmodule DemoWeb.Router do
use Phoenix.Router
import LiveAdmin.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug :fetch_session
plug :user_id_stub
end
scope "/" do
pipe_through :browser
get "/", DemoWeb.PageController, :index
live_admin "/admin", title: "DevAdmin" do
admin_resource "/users/profiles", Demo.Accounts.User.Profile
admin_resource "/users", DemoWeb.UserAdmin
end
live_admin "/posts-admin", components: [home: DemoWeb.PostsAdmin.Home] do
admin_resource "/posts", Demo.Posts.Post
admin_resource "/users", DemoWeb.UserAdmin
end
end
defp user_id_stub(conn, _) do
Plug.Conn.assign(conn, :user_id, 1)
end
end
defmodule DemoWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :live_admin
socket "/live", Phoenix.LiveView.Socket
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
plug Plug.Session,
store: :cookie,
key: "_live_view_key",
signing_salt: "/VEDsdfsffMnp5"
plug DemoWeb.Router
end
defmodule Demo.Gettext do
use Gettext, otp_app: :demo, priv: "dev/gettext"
def locales, do: ["en", "tr"]
end
Application.put_env(:phoenix, :serve_endpoints, true)
Application.ensure_all_started(:os_mon)
Task.async(fn ->
children = [Demo.Repo, DemoWeb.Endpoint, {Phoenix.PubSub, name: Demo.PubSub, adapter: Phoenix.PubSub.PG2}]
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Demo.Populator.reset()
Process.sleep(:infinity)
end)
|> Task.await(:infinity)