From 9ab228fde09d29028362f6e480f48173f9fd8ebf Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sun, 11 Feb 2024 10:11:04 +1100 Subject: [PATCH] Tag posts from the admin area --- slices/admin/commands/auto_tagging/tag.rb | 2 +- slices/admin/commands/posts/update.rb | 6 ++++ slices/admin/repos/post_repo.rb | 38 +++++++++++++++++++-- slices/admin/templates/posts/show.html.slim | 5 ++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/slices/admin/commands/auto_tagging/tag.rb b/slices/admin/commands/auto_tagging/tag.rb index a44c289..10d1b49 100644 --- a/slices/admin/commands/auto_tagging/tag.rb +++ b/slices/admin/commands/auto_tagging/tag.rb @@ -18,7 +18,7 @@ module Admin post_repo.by_content(body_contains: auto_tagging.body_contains) posts.each do |post| - post_repo.tag_post(post_id: post.id, + post_repo.auto_tag_post(post_id: post.id, tag_id: auto_tagging.tag_id) end end diff --git a/slices/admin/commands/posts/update.rb b/slices/admin/commands/posts/update.rb index 76c0a18..28d2d09 100644 --- a/slices/admin/commands/posts/update.rb +++ b/slices/admin/commands/posts/update.rb @@ -11,7 +11,13 @@ module Admin attrs_to_replace = {} attrs_to_replace[:content] = markdown.call(content: params[:body]) if params[:body] + tags = params[:tags].split(",").map(&:strip) + post_repo.update(params[:id], attrs_to_replace) + if tags && !tags.empty? + post_repo.remove_tags(post_id: params[:id]) + post_repo.tag_post(post_id: params[:id], tags: tags) + end end end end diff --git a/slices/admin/repos/post_repo.rb b/slices/admin/repos/post_repo.rb index 8a45a01..b2c9b1f 100644 --- a/slices/admin/repos/post_repo.rb +++ b/slices/admin/repos/post_repo.rb @@ -5,7 +5,32 @@ module Admin class PostRepo < Adamantium::Repo[:posts] commands update: :by_pk - def tag_post(post_id:, tag_id:) + def tag_post(post_id:, tags:) + tags.each do |tag_name| + next if tag_name == "" + + tag = posts.tags.where(label: tag_name).one || + posts + .tags + .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) + .commit + + next if posts + .post_tags + .where( + post_id: post_id, + tag_id: tag[:id] + ).count > 0 + + posts.post_tags.changeset(:create, { + post_id: post_id, + tag_id: tag[:id] + }) + .commit + end + end + + def auto_tag_post(post_id:, tag_id:) return if posts .post_tags .where( @@ -44,7 +69,9 @@ module Admin end def find(id:) - posts.where(id: id).one! + posts + .combine(:tags) + .where(id: id).one! end def delete(id:) @@ -67,6 +94,13 @@ module Admin .published_between(start_date, end_date) .to_a end + + def remove_tags(post_id:) + posts + .post_tags + .where(post_id: post_id) + .delete + end end end end diff --git a/slices/admin/templates/posts/show.html.slim b/slices/admin/templates/posts/show.html.slim index f4b0afa..2596a2e 100644 --- a/slices/admin/templates/posts/show.html.slim +++ b/slices/admin/templates/posts/show.html.slim @@ -28,7 +28,10 @@ article class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 d a href="/post/#{post.slug}" h1= post.name || "💬" form action="/admin/post/#{post.id}/update" method="POST" - textarea name="body" class="text-gray-800 w-full border-blue-200 border-2 rounded p-2" x-data="{ resize: () => { $el.style.height = '5px'; $el.style.height = $el.scrollHeight + 'px' } }" x-init="resize()" @input="resize()" + textarea name="body" class="text-gray-800 w-full border-blue-200 border-2 rounded p-2 mb-4" x-data="{ resize: () => { $el.style.height = '5px'; $el.style.height = $el.scrollHeight + 'px' } }" x-init="resize()" @input="resize()" == markdown_body + fieldset class="mb-4 flex" + label for="tags" class="mr-2" Tags: + input type="text" name="tags" id="tags" class="w-full px-1 border rounded" value="#{post.tags.map(&:label).join(", ")}" button class="rounded bg-blue-100 hover:bg-blue-200 text-blue-600 px-2 hover:cursor-pointer" type="submit" = "Update"