From 48ad3a30da7197452ffe29c52ee755643074d0c3 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Mon, 27 Aug 2018 20:10:53 +0200 Subject: [PATCH] fix --- apps/controllers/PostController.lua | 5 ++--- apps/models/PostModel.lua | 4 ++-- info/controllers/IndexController.lua | 10 +++++----- info/controllers/TocController.lua | 4 +--- info/controllers/UserController.lua | 5 ++--- info/models/CategoryModel.lua | 4 ++-- info/models/SectionsModel.lua | 4 ++-- info/models/UserModel.lua | 4 ++-- silk/BaseController.lua | 20 +++++++++++++------- silk/BaseModel.lua | 2 +- silk/BaseObject.lua | 9 ++++++++- silk/DBHelper.lua | 2 +- silk/Router.lua | 2 +- silk/Template.lua | 2 +- 14 files changed, 43 insertions(+), 34 deletions(-) diff --git a/apps/controllers/PostController.lua b/apps/controllers/PostController.lua index 9bd7aca..d3d25d3 100644 --- a/apps/controllers/PostController.lua +++ b/apps/controllers/PostController.lua @@ -1,8 +1,7 @@ -PostController = BaseController:extends{ - class = "PostController", +BaseController:subclass("PostController", { registry = {}, models = { "post" } -} +}) function PostController:index(...) local args = {...} diff --git a/apps/models/PostModel.lua b/apps/models/PostModel.lua index 0e300e1..1902edd 100644 --- a/apps/models/PostModel.lua +++ b/apps/models/PostModel.lua @@ -1,8 +1,8 @@ -PostModel = BaseModel:extends{ +BaseModel:subclass("PostModel", { registry = {}, name = "post", fields = { cid = "NUMERIC", content = "TEXT" } -} \ No newline at end of file +}) \ No newline at end of file diff --git a/info/controllers/IndexController.lua b/info/controllers/IndexController.lua index 4a86f1a..54727bd 100644 --- a/info/controllers/IndexController.lua +++ b/info/controllers/IndexController.lua @@ -1,8 +1,8 @@ -IndexController = BaseController:extends{ - class = "IndexController", - registry = {}, - models = { "sections", "category" } -} + BaseController:subclass("IndexController", + { + registry = {}, + models = { "sections", "category" } + }) local sectionsByCid = function(db, id) local cond = { exp = { ["="] = { cid = id } } , order = { start = "DESC" } } diff --git a/info/controllers/TocController.lua b/info/controllers/TocController.lua index eea9d1f..c888984 100644 --- a/info/controllers/TocController.lua +++ b/info/controllers/TocController.lua @@ -1,6 +1,4 @@ -TocController = BaseController:extends{ - class = "TocController" -} +BaseController:subclass("TocController") function TocController:index(...) return true diff --git a/info/controllers/UserController.lua b/info/controllers/UserController.lua index 73723fe..1bea4a4 100644 --- a/info/controllers/UserController.lua +++ b/info/controllers/UserController.lua @@ -1,7 +1,6 @@ -UserController = BaseController:extends{ - class = "UserController", +BaseController:subclass("UserController",{ models = {"user"} -} +}) function UserController:index(...) local args = {...} diff --git a/info/models/CategoryModel.lua b/info/models/CategoryModel.lua index 3ddf774..7ca81ae 100644 --- a/info/models/CategoryModel.lua +++ b/info/models/CategoryModel.lua @@ -1,4 +1,4 @@ -CategoryModel = BaseModel:extends{ +BaseModel:subclass("CategoryModel",{ registry = {}, name = "cv_cat", fields = { @@ -6,4 +6,4 @@ CategoryModel = BaseModel:extends{ name = "TEXT", pid = "NUMERIC" } -} \ No newline at end of file +}) \ No newline at end of file diff --git a/info/models/SectionsModel.lua b/info/models/SectionsModel.lua index 5a4fc94..5de6ce1 100644 --- a/info/models/SectionsModel.lua +++ b/info/models/SectionsModel.lua @@ -1,4 +1,4 @@ -SectionsModel = BaseModel:extends{ +BaseModel:subclass("SectionsModel",{ name = "cv_sections", fields = { title = "TEXT", @@ -10,4 +10,4 @@ SectionsModel = BaseModel:extends{ publish = "NUMERIC", cid = "NUMERIC", } -} \ No newline at end of file +}) \ No newline at end of file diff --git a/info/models/UserModel.lua b/info/models/UserModel.lua index e157945..a4e1a11 100644 --- a/info/models/UserModel.lua +++ b/info/models/UserModel.lua @@ -1,4 +1,4 @@ -UserModel = BaseModel:extends{ +BaseModel:subclass("UserModel",{ registry = {}, name = "user", fields = { @@ -9,4 +9,4 @@ UserModel = BaseModel:extends{ email = "TEXT", url = "TEXT" } -} \ No newline at end of file +}) \ No newline at end of file diff --git a/silk/BaseController.lua b/silk/BaseController.lua index d88593c..ccb1e8d 100644 --- a/silk/BaseController.lua +++ b/silk/BaseController.lua @@ -1,9 +1,11 @@ -- create class -BaseController = BaseObject:extends{ - class="BaseController", - registry = {}, - models = {}, - main = false } +BaseObject:subclass("BaseController", + { + registry = {}, + models = {}, + main = false + }) + -- set the name here in each subclasses function BaseController:initialize() for k, v in pairs(self.models) do @@ -24,6 +26,10 @@ function BaseController:initialize() self.template = Template:new{registry = self.registry} end +function BaseController:print(...) + return self:actionnotfound("print") +end + function BaseController:redirect(url) std.status(301, "Moved Permanently") std.custom_header("Content-Type","text/html") @@ -64,7 +70,7 @@ function BaseController:modelnotfound(...) end -- The not found controller -NotfoundController = BaseController:extends{ registry = {}, models = {} } +BaseController:subclass("NotfoundController",{ registry = {}, models = {} }) function NotfoundController:index(...) local args = {...} @@ -74,7 +80,7 @@ function NotfoundController:index(...) end -- The asset controller for the static file -AssetController = BaseController:extends{name= "AssetController",registry={}, models={}} +BaseController:subclass("AssetController", {registry={}, models={}}) function AssetController:index(...) local args = {...} return self:get(table.unpack(args)) diff --git a/silk/BaseModel.lua b/silk/BaseModel.lua index 563979a..ee00ba5 100644 --- a/silk/BaseModel.lua +++ b/silk/BaseModel.lua @@ -1,6 +1,6 @@ -- create class -BaseModel = BaseObject:extends{class="BaseModel",registry = {}} +BaseObject:subclass("BaseModel", {registry = {}}) function BaseModel:initialize() self.db = self.registry.db diff --git a/silk/BaseObject.lua b/silk/BaseObject.lua index 360d4bd..4cd9e7b 100644 --- a/silk/BaseObject.lua +++ b/silk/BaseObject.lua @@ -1,5 +1,8 @@ BaseObject = Object:extends{registry = {}, class="BaseObject"} - +function BaseObject:subclass(name, args) + _G[name] = self:extends(args) + _G[name].class = name +end function BaseObject:log(msg, level) level = level or "INFO" @@ -12,6 +15,10 @@ function BaseObject:debug(msg) self:log(msg, "DEBUG") end +function BaseObject:print() + print(self.class) +end + function BaseObject:error(msg, trace) html() echo(msg) diff --git a/silk/DBHelper.lua b/silk/DBHelper.lua index be5de5a..e9d0ab9 100644 --- a/silk/DBHelper.lua +++ b/silk/DBHelper.lua @@ -2,7 +2,7 @@ sqlite = modules.sqlite() if sqlite == nil then return 0 end -- create class -DBHelper = BaseObject:extends{db=nil, class='DBHelper'} + BaseObject:subclass("DBHelper",{db={}}) function DBHelper:createTable(tbl, m) if self:available(tbl) then return true end diff --git a/silk/Router.lua b/silk/Router.lua index f4d61a7..6884baa 100644 --- a/silk/Router.lua +++ b/silk/Router.lua @@ -1,6 +1,6 @@ --define the class -Router = BaseObject:extends{class="Router",registry = {}} +BaseObject:subclass ("Router",{registry = {}}) function Router:setPath(path) self.path = path end diff --git a/silk/Template.lua b/silk/Template.lua index fe2a8cb..ae9004b 100644 --- a/silk/Template.lua +++ b/silk/Template.lua @@ -1,5 +1,5 @@ -- create class -Template = BaseObject:extends{class="Template",registry = {}} + BaseObject:subclass("Template",{registry = {}}) function Template:initialize() self.vars = {}