From 161f76a3b9f6a3073b7dd397db6b26f431fc8734 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Wed, 8 May 2013 22:57:51 +0300 Subject: optional automatic removal of empty collections --- acf/model/init.lua | 2 +- acf/model/node.lua | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'acf') diff --git a/acf/model/init.lua b/acf/model/init.lua index 895ed62..f325053 100644 --- a/acf/model/init.lua +++ b/acf/model/init.lua @@ -195,7 +195,7 @@ function Collection:init(params, itype) assert(self.type) self.itype = itype or node.Collection - self.iparams = {required=self.required} + self.iparams = {required=self.required, destroy=self.destroy} self.dtype = 'collection' self.widget = self.dtype diff --git a/acf/model/node.lua b/acf/model/node.lua index afc3e0f..47a15c7 100644 --- a/acf/model/node.lua +++ b/acf/model/node.lua @@ -117,8 +117,11 @@ function Collection:init(context, params) function mt.members() return mt.txn:get(mt.addr, 'table') or {} end function mt.validate() - if params.required and #mt.members() == 0 then - raise(mt.path, 'Collection cannot be empty') + if #mt.members() > 0 then return end + if params.required then raise(mt.path, 'Collection cannot be empty') end + if params.destroy then + mt.txn:set(mt.addr) + validate(mt.parent) end end -- cgit v1.2.3