Skip to content

Commit 3cd423f

Browse files
author
Olivier Bonnaure
committed
feat: improve core
1 parent 6459323 commit 3cd423f

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

.lua/luaonbeans.lua

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Crate = {}
55
PGRest = {}
66
Rest = {}
77
Surreal = {}
8-
98
Blocks = {}
109

1110
function LoadViewsRecursively(path)
@@ -58,19 +57,19 @@ function Page(view, layout, bindVarsView, bindVarsLayout)
5857
end
5958
end
6059

60+
compiled_view = Views["app/views/" .. view .. ".etlua"]
61+
if variant ~= "" then
62+
compiled_view = Views["app/views/" .. view .. "+" .. variant .. ".etlua"] or compiled_view
63+
end
64+
view = compiled_view(bindVarsView or {})
65+
6166
compiled_layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"]
6267
if variant ~= "" then
6368
compiled_layout = Views["app/views/layouts/" .. layout .. "/index.html+" .. variant .. ".etlua"]
6469
or compiled_layout
6570
end
6671
layout = compiled_layout(bindVarsLayout or {})
6772

68-
compiled_view = Views["app/views/" .. view .. ".etlua"]
69-
if variant ~= "" then
70-
compiled_view = Views["app/views/" .. view .. "+" .. variant .. ".etlua"] or compiled_view
71-
end
72-
view = compiled_view(bindVarsView or {})
73-
7473
local content
7574
if view:find("%%") then
7675
content = layout:gsub("@yield", view:gsub("%%", "%%%%"))
@@ -246,9 +245,10 @@ local function tableSplat(input_list)
246245
end
247246

248247
function DefineRoute(path, method)
249-
if method == "PATCH" then
250-
method = "PUT"
251-
end
248+
if method == "PATCH" then method = "PUT" end
249+
250+
if method == "POST" and Params._method ~= nil then method = Params._method end
251+
252252
local recognized_route = Routes[method]
253253
local route_found = false
254254
local final_route = false
@@ -257,7 +257,7 @@ function DefineRoute(path, method)
257257

258258
Splat = {}
259259
if path == "/" then
260-
recognized_route = recognized_route[""]
260+
recognized_route = recognized_route[""] or recognized_route["@"]
261261
else
262262
for _, value in pairs(string.split(path, "/")) do
263263
if final_route == false then
@@ -266,16 +266,16 @@ function DefineRoute(path, method)
266266
final_route = true
267267
end
268268
recognized_route = recognized_route[value] or recognized_route[value .. "*"]
269-
if recognized_route[""] then
270-
last_route_found = recognized_route[""]
269+
if recognized_route[""] or recognized_route["@"] then
270+
last_route_found = recognized_route[""] or recognized_route["@"]
271271
end
272272
route_found = true
273273
else
274274
route_found = false
275275
if recognized_route[":var"] then
276276
recognized_route = recognized_route[":var"]
277-
if recognized_route[""] then
278-
last_route_found = recognized_route[""]
277+
if recognized_route[""] or recognized_route["@"] then
278+
last_route_found = recognized_route[""] or recognized_route["@"]
279279
end
280280
local parser = Re.compile(recognized_route[":regex"])
281281
local matcher = { parser:search(value) }
@@ -293,7 +293,7 @@ function DefineRoute(path, method)
293293
end
294294

295295
if type(recognized_route) == "table" and route_found then
296-
recognized_route = recognized_route[""]
296+
recognized_route = recognized_route[""] or recognized_route["@"]
297297
else
298298
if route_found == false then
299299
recognized_route = nil
@@ -376,11 +376,12 @@ function GetBodyParams()
376376
end
377377

378378
Params = table.merge(Params, body_Params)
379+
Params = table.merge(Params, DecodeJson(GetBody()) or {})
379380
return body_Params
380381
end
381382

382-
function RedirectTo(path, status)
383-
status = status or 301
383+
function RedirectTo(path)
384+
status = status or 302
384385
SetStatus(status)
385386
SetHeader("Location", path)
386387
end
@@ -477,7 +478,7 @@ function LoadPublicAssetsRecursively(path)
477478
dir:close()
478479
end
479480

480-
RunCommand = function(command)
481+
function RunCommand(command)
481482
if type(command) == "string" then
482483
command = string.split(command)
483484
end
@@ -552,7 +553,7 @@ function RefreshPageForDevMode()
552553
end
553554
end
554555

555-
-- View form errors
556+
-- View Helpers
556557
local function ExtractModelError(inputTable, allowedField)
557558
local messages = {}
558559

@@ -582,9 +583,20 @@ function TextField(name, field, model, options)
582583
options = options or {}
583584
local value = options.novalue == true and "" or model.data[field]
584585
local required = options.required == true and "required" or ""
586+
local type = options.password == true and "password" or "text"
585587

586588
return [[
587589
<label class="font-bold" for="firstname">%s</label>
588-
<input type="text" name="%s" value="%s" %s />%s
589-
]] % { name, field, value or "", required, DisplayErrorFor(model.errors, field, options.error_style) }
590+
<input type="%s" name="%s" value="%s" %s class="bg-white dark:bg-slate-700 text-slate-900 dark:text-slate-100 border border-slate-300 dark:border-slate-600 rounded px-3 py-2 w-full focus:outline-none focus:ring-2 focus:ring-purple-500 dark:focus:ring-purple-400" />%s
591+
]] % { name, type, field, value or "", required, DisplayErrorFor(model.errors, field, options.error_style) }
592+
end
593+
594+
function PasswordField(name, field, model, options)
595+
return TextField(name, field, model, table.merge(options, { password = true }))
596+
end
597+
598+
function CheckboxField(field, model, options)
599+
options = options or {}
600+
return '%s<input type="checkbox" name="%s" %s>' % { DisplayErrorFor(model.errors, field, options.error_style), field, model.data[field] and "checked" or "" }
590601
end
602+
-- / View Helpers

config/routes.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Routes = { ["GET"] = {
2-
[""] = "welcome#index",
3-
["pdf"] = "welcome#pdf",
4-
["chart"] = "welcome#chart",
5-
["redis-incr"] = "welcome#redis_incr"
2+
[""] = { ["@"] = "welcome#index" },
3+
["pdf"] = { ["@"] = "welcome#pdf" },
4+
["chart"] = { ["@"] = "welcome#chart" },
5+
["redis-incr"] = { ["@"] = "welcome#redis_incr" }
66
}
77
}
88

0 commit comments

Comments
 (0)