From a6c954326758d37010ebd3f725900c33debc61ff Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Tue, 17 Dec 2013 16:56:34 +0200 Subject: model: dynamically determine if field is editable --- acf2/model/field.lua | 14 +++++++++++--- acf2/model/model.lua | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'acf2/model') diff --git a/acf2/model/field.lua b/acf2/model/field.lua index 777a311..7a7595a 100644 --- a/acf2/model/field.lua +++ b/acf2/model/field.lua @@ -54,10 +54,16 @@ end M.Field = class(M.Member) function M.Field:init(params) + if not params then params = {} end + util.setdefault(params, 'editable', not params.compute) + super(self, M.Field):init(params) if self.compute then self.addr = node.null_addr end - if self.editable == nil then self.editable = not self.compute end + + if type(self.editable) ~= 'function' then + function self.editable(context) return params.editable end + end self.condition = M.conv_filter(self.condition) @@ -93,7 +99,7 @@ function M.Field:meta(context) local res = super(self, M.Field):meta(context) res.type = self.dtype - res.editable = self.editable + res.editable = self:editable(context) res.condition = self.condition res.required = self.required res.default = self.default @@ -156,7 +162,9 @@ function M.Field:normalize(context, value) return value end function M.Field:validate(context, value) end function M.Field:save(context, value) - if not self.editable then raise(context.path, 'Is not editable') end + if not self:editable(context) then + raise(context.path, 'Is not editable') + end self:_save(context, self:_validate(context, value)) end diff --git a/acf2/model/model.lua b/acf2/model/model.lua index 7d026da..a7609b0 100644 --- a/acf2/model/model.lua +++ b/acf2/model/model.lua @@ -201,7 +201,7 @@ function M.Model:init(context) function mt.validate() for _, f in ipairs(_members(Field)) do - if f.editable then + if f:editable() then if mt.match(f.condition or {}) then f:validate_saved() else f:_save() end end -- cgit v1.2.3