From 4ee569a4707e3d40c44125f3b933b9220d507c1d Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 9 Mar 2016 10:05:30 -0500 Subject: refactor set_tags --- TODO.md | 1 + src/guff/models/post.cr | 42 ++++++++++++++---------------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/TODO.md b/TODO.md index 158d072..f8543db 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,4 @@ TODO * readonly-db connection (using `PRAGMA query_only`) * per-site public/ directories * history (in `PostModel#update_post`) +* triggers for removing unused `post_tag` and `tag` entries diff --git a/src/guff/models/post.cr b/src/guff/models/post.cr index 560b36d..9a740bc 100644 --- a/src/guff/models/post.cr +++ b/src/guff/models/post.cr @@ -395,7 +395,6 @@ module Guff site_id: site_id, post_id: post_id, tags: tags, - use_transaction: false, ) end end @@ -422,23 +421,6 @@ module Guff site_id = nil : Int?, post_id = nil : Int?, tags = [] of String : Array(String)?, - use_transaction = true : Bool - ) - if use_transaction - transaction do - raw_set_tags(site_id, post_id, tags) - end - else - raw_set_tags(site_id, post_id, tags) - end - - nil - end - - private def raw_set_tags( - site_id = nil : Int?, - post_id = nil : Int?, - tags = [] of String : Array(String)?, ) return unless post_id && tags @@ -448,19 +430,23 @@ module Guff "post_id": post_id.to_s, } - # delete existing post tags - query(:set_tags_delete, args, nil) + transaction do + # delete existing post tags + query(:set_tags_delete, args, nil) - if tags.size > 0 - @models.tag.add_tags(tags) + if tags.size > 0 + @models.tag.add_tags(tags) - # add new post tags - query(:set_tags_insert, args, { - "tags": tags.map { |tag| - "'" + @db.quote(tag) + "'" - }.join(','), - }) + # add new post tags + query(:set_tags_insert, args, { + "tags": tags.map { |tag| + "'" + @db.quote(tag) + "'" + }.join(','), + }) + end end + + nil end end end -- cgit v1.2.3