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 --- src/guff/models/post.cr | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'src') 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