feat(store): add recur and recur_mode task fields
Problem: the task schema has no fields for storing recurrence rules. Solution: add recur and recur_mode to the Task class, known_fields, task_to_table, table_to_task, and the add() signature.
This commit is contained in:
parent
c2310f2659
commit
e69e93f536
2 changed files with 50 additions and 1 deletions
|
|
@ -7,6 +7,8 @@ local config = require('pending.config')
|
||||||
---@field category? string
|
---@field category? string
|
||||||
---@field priority integer
|
---@field priority integer
|
||||||
---@field due? string
|
---@field due? string
|
||||||
|
---@field recur? string
|
||||||
|
---@field recur_mode? 'scheduled'|'completion'
|
||||||
---@field entry string
|
---@field entry string
|
||||||
---@field modified string
|
---@field modified string
|
||||||
---@field end? string
|
---@field end? string
|
||||||
|
|
@ -56,6 +58,8 @@ local known_fields = {
|
||||||
category = true,
|
category = true,
|
||||||
priority = true,
|
priority = true,
|
||||||
due = true,
|
due = true,
|
||||||
|
recur = true,
|
||||||
|
recur_mode = true,
|
||||||
entry = true,
|
entry = true,
|
||||||
modified = true,
|
modified = true,
|
||||||
['end'] = true,
|
['end'] = true,
|
||||||
|
|
@ -81,6 +85,12 @@ local function task_to_table(task)
|
||||||
if task.due then
|
if task.due then
|
||||||
t.due = task.due
|
t.due = task.due
|
||||||
end
|
end
|
||||||
|
if task.recur then
|
||||||
|
t.recur = task.recur
|
||||||
|
end
|
||||||
|
if task.recur_mode then
|
||||||
|
t.recur_mode = task.recur_mode
|
||||||
|
end
|
||||||
if task['end'] then
|
if task['end'] then
|
||||||
t['end'] = task['end']
|
t['end'] = task['end']
|
||||||
end
|
end
|
||||||
|
|
@ -105,6 +115,8 @@ local function table_to_task(t)
|
||||||
category = t.category,
|
category = t.category,
|
||||||
priority = t.priority or 0,
|
priority = t.priority or 0,
|
||||||
due = t.due,
|
due = t.due,
|
||||||
|
recur = t.recur,
|
||||||
|
recur_mode = t.recur_mode,
|
||||||
entry = t.entry,
|
entry = t.entry,
|
||||||
modified = t.modified,
|
modified = t.modified,
|
||||||
['end'] = t['end'],
|
['end'] = t['end'],
|
||||||
|
|
@ -224,7 +236,7 @@ function M.get(id)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param fields { description: string, status?: string, category?: string, priority?: integer, due?: string, order?: integer, _extra?: table }
|
---@param fields { description: string, status?: string, category?: string, priority?: integer, due?: string, recur?: string, recur_mode?: string, order?: integer, _extra?: table }
|
||||||
---@return pending.Task
|
---@return pending.Task
|
||||||
function M.add(fields)
|
function M.add(fields)
|
||||||
local data = M.data()
|
local data = M.data()
|
||||||
|
|
@ -236,6 +248,8 @@ function M.add(fields)
|
||||||
category = fields.category or config.get().default_category,
|
category = fields.category or config.get().default_category,
|
||||||
priority = fields.priority or 0,
|
priority = fields.priority or 0,
|
||||||
due = fields.due,
|
due = fields.due,
|
||||||
|
recur = fields.recur,
|
||||||
|
recur_mode = fields.recur_mode,
|
||||||
entry = now,
|
entry = now,
|
||||||
modified = now,
|
modified = now,
|
||||||
['end'] = nil,
|
['end'] = nil,
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,41 @@ describe('store', function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('recurrence fields', function()
|
||||||
|
it('persists recur and recur_mode through round-trip', function()
|
||||||
|
store.load()
|
||||||
|
store.add({ description = 'Recurring', recur = 'weekly', recur_mode = 'scheduled' })
|
||||||
|
store.save()
|
||||||
|
store.unload()
|
||||||
|
store.load()
|
||||||
|
local task = store.get(1)
|
||||||
|
assert.are.equal('weekly', task.recur)
|
||||||
|
assert.are.equal('scheduled', task.recur_mode)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('persists recur without recur_mode', function()
|
||||||
|
store.load()
|
||||||
|
store.add({ description = 'Simple recur', recur = 'daily' })
|
||||||
|
store.save()
|
||||||
|
store.unload()
|
||||||
|
store.load()
|
||||||
|
local task = store.get(1)
|
||||||
|
assert.are.equal('daily', task.recur)
|
||||||
|
assert.is_nil(task.recur_mode)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('omits recur fields when not set', function()
|
||||||
|
store.load()
|
||||||
|
store.add({ description = 'No recur' })
|
||||||
|
store.save()
|
||||||
|
store.unload()
|
||||||
|
store.load()
|
||||||
|
local task = store.get(1)
|
||||||
|
assert.is_nil(task.recur)
|
||||||
|
assert.is_nil(task.recur_mode)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('active_tasks', function()
|
describe('active_tasks', function()
|
||||||
it('excludes deleted tasks', function()
|
it('excludes deleted tasks', function()
|
||||||
store.load()
|
store.load()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue