Clean up auto tagging

This commit is contained in:
2023-05-06 20:27:11 +10:00
parent 9f8359d782
commit 0c74055e27
7 changed files with 52 additions and 11 deletions

View File

@@ -5,22 +5,32 @@ module Admin
include Deps["commands.auto_tagging.create",
"views.auto_tagging.index",
auto_tag: "commands.auto_tagging.tag"]
"validation.contracts.auto_tagging_contract",
auto_tag: "commands.auto_tagging.tag",
new_view: "views.auto_tagging.new"]
def handle(req, res)
title_contains = req.params[:title_contains]
body_contains = req.params[:body_contains]
tag_id = req.params[:tag_id]
result = create.(title_contains: title_contains,
body_contains: body_contains,
tag_id: tag_id)
validation = auto_tagging_contract.call(title_contains: title_contains,
body_contains: body_contains,
tag_id: tag_id)
if result.success?
auto_tag.(auto_tag_id: result.value!)
if validation.success?
result = create.(title_contains: title_contains,
body_contains: body_contains,
tag_id: tag_id)
if result.success?
auto_tag.(auto_tag_id: result.value!)
end
res.render index
end
res.render index
res.render new_view, errors: validation.errors
end
end
end

View File

@@ -6,7 +6,7 @@ module Admin
include Deps[new_view: "views.auto_tagging.new"]
def handle(req, res)
res.render new_view
res.render new_view, errors: {}
end
end
end

View File

@@ -1,10 +1,11 @@
div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200"
h1 Admin // Auto tagging
div class="prose dark:prose-invert max-w-prose mx-auto"
div class="prose dark:prose-invert max-w-prose mx-auto mb-12"
- auto_taggings.each do |auto_tagging|
div id="auto-tag-#{auto_tagging.id}"
div id="auto-tag-#{auto_tagging.id}" class="mb-2"
== "Tag post with <strong>#{auto_tagging.tag.label}</strong> when <strong>#{auto_tagging.title_only? ? "title" : "content"}</strong> contains <strong>#{auto_tagging.term}</strong>"
= " — "
button hx-delete="/admin/tags/auto_taggings/#{auto_tagging.id}" hx-target="#auto-tag-#{auto_tagging.id}" delete
a class="bg-blue-100 text-blue-600 p-1 rounded no-underline" href="/admin/tags/auto_tagging/new" New
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"

View File

@@ -2,6 +2,12 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:
h1 Admin // Auto tagging
div class="max-w-prose mx-auto"
- if errors
div class="text-gray-100 dark:text-gray-100 bg-pink-100 dark:bg-pink-600 rounded px-4 py-2 mb-12"
p Errors:
- errors.each do |error|
li = "#{error[1].join(",")}"
form method="POST" action="/admin/tags/auto_tagging"
div class="mb-4"
label class="text-gray-800 dark:text-gray-200" for="title_contains" Title contains:&nbsp;

View File

@@ -1,10 +1,12 @@
div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200"
h1 Admin
div class="max-w-prose mx-auto"
div class="max-w-prose mx-auto prose dark:prose-invert"
ul
li
a href="/admin/tags" Tags
li
a href="/admin/tags/auto_tagging" Auto tagging
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"

View File

@@ -0,0 +1,18 @@
module Admin
module Validation
module Contracts
class AutoTaggingContract < Dry::Validation::Contract
params do
required(:title_contains).maybe(:string)
required(:body_contains).maybe(:string)
required(:tag_id).filled(:integer)
end
rule(:title_contains, :body_contains) do
key.failure("must provide a title or body term") if values[:title_contains].nil? && values[:body_contains].nil?
key.failure("must provide a title or body term") if !values[:title_contains].nil? && !values[:body_contains].nil?
end
end
end
end
end

View File

@@ -8,6 +8,10 @@ module Admin
expose :tags do
tag_repo.list.to_a
end
expose :errors do |errors:|
errors.empty? ? nil : errors.to_h
end
end
end
end