Skip to content

Commit e454897

Browse files
author
Nikhil R
committed
refactor(ES): Move logic to controller
1 parent 63dbc44 commit e454897

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

test/dev-app/controllers/test.lua

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,58 @@ function test.index(page)
1616
end
1717

1818
function test.elasticfunctions(page)
19+
20+
function table.val_to_str ( v )
21+
22+
if "string" == type( v ) then
23+
v = string.gsub( v, "\n", "\\n" )
24+
25+
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
26+
return "'" .. v .. "'"
27+
end
28+
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
29+
else
30+
return "table" == type( v ) and table.tostring( v ) or
31+
tostring( v )
32+
end
33+
34+
end
35+
36+
function table.key_to_str ( k )
37+
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
38+
return k
39+
else
40+
return "[" .. table.val_to_str( k ) .. "]"
41+
end
42+
end
43+
44+
function table.tostring( tbl )
45+
local result, done = {}, {}
46+
for k, v in ipairs( tbl ) do
47+
table.insert( result, table.val_to_str( v ) )
48+
done[ k ] = true
49+
end
50+
51+
for k, v in pairs( tbl ) do
52+
if not done[ k ] then
53+
table.insert( result,
54+
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
55+
end
56+
end
57+
58+
return "{" .. table.concat( result, "," ) .. "}"
59+
end
60+
1961
local doc = {name = "test"}
2062
local contact = es_model.new("test")
2163
contact.name = "Tester 123"
22-
contact.save{id = 101, body = doc, routing = "[email protected]"}
64+
local msg = contact.save{id = 101, body = doc, routing = "[email protected]"}
2365
if (page.POST) then msg = contact.search{q = page.POST.search} end
24-
page:render('elastic', {msg = msg})
66+
if type(msg) == "table" then
67+
page:render('elastic',{ msg = table.tostring(msg) })
68+
else
69+
page:render('elastic',{ msg = msg })
70+
end
2571
end
2672

2773
--This will be recovered once I reorganize the mailer module

test/dev-app/views/test/elastic.lp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,5 @@
77
</form>
88

99
<?lua
10-
11-
function table.val_to_str ( v )
12-
13-
if "string" == type( v ) then
14-
v = string.gsub( v, "\n", "\\n" )
15-
16-
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
17-
return "'" .. v .. "'"
18-
end
19-
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
20-
else
21-
return "table" == type( v ) and table.tostring( v ) or
22-
tostring( v )
23-
end
24-
25-
end
26-
27-
function table.key_to_str ( k )
28-
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
29-
return k
30-
else
31-
return "[" .. table.val_to_str( k ) .. "]"
32-
end
33-
end
34-
35-
function table.tostring( tbl )
36-
local result, done = {}, {}
37-
for k, v in ipairs( tbl ) do
38-
table.insert( result, table.val_to_str( v ) )
39-
done[ k ] = true
40-
end
41-
42-
for k, v in pairs( tbl ) do
43-
if not done[ k ] then
44-
table.insert( result,
45-
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
46-
end
47-
end
48-
49-
return "{" .. table.concat( result, "," ) .. "}"
50-
end
51-
52-
if type(msg) == "table" then
53-
page:print(table.tostring(msg))
54-
else
55-
page:print(msg)
56-
end
57-
10+
page:print(msg)
5811
?>

0 commit comments

Comments
 (0)