Tag posts from the admin area

This commit is contained in:
2024-02-11 10:11:04 +11:00
parent 1172ba20ec
commit 9ab228fde0
4 changed files with 47 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"