Links, mainly...
Some checks failed
/ Run system tests (push) Waiting to run
/ Build, push and deploy image (push) Blocked by required conditions
/ Run tests (push) Has been cancelled
/ Checkout (push) Successful in 8m9s

This commit is contained in:
david 2024-07-26 00:59:00 +02:00
parent fd42a3f173
commit 21ab02d647
69 changed files with 2258 additions and 155 deletions

View file

@ -13,7 +13,9 @@ class ApplicationController < ActionController::Base
{ label: 'Dashboard', icon: :speedometer2, path: :root },
{ label: Report.model_name.human(count: 2), icon: :'journal-text', path: :reports },
{ label: Checklist.model_name.human(count: 2), icon: :'list-check', path: :checklists },
{ label: Check.model_name.human(count: 2), icon: :check2, path: :checks }
{ label: Check.model_name.human(count: 2), icon: :check2, path: :checks },
{ label: Link.model_name.human(count: 2), icon: :link, path: :links },
{ label: LinkCategory.model_name.human(count: 2), icon: :"folder", path: :link_categories }
]
@search_url = nil # root_url
end

View file

@ -0,0 +1,58 @@
class LinkCategoriesController < ApplicationController
before_action :set_link_category, only: %i[ show edit update destroy ]
# GET /link_categories
def index
@link_categories = LinkCategory.all
end
# GET /link_categories/1
def show
end
# GET /link_categories/new
def new
@link_category = LinkCategory.new
end
# GET /link_categories/1/edit
def edit
end
# POST /link_categories
def create
@link_category = LinkCategory.new(link_category_params)
if @link_category.save
redirect_to @link_category, notice: "Link category was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /link_categories/1
def update
if @link_category.update(link_category_params)
redirect_to @link_category, notice: "Link category was successfully updated.", status: :see_other
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /link_categories/1
def destroy
@link_category.destroy!
redirect_to link_categories_url, notice: "Link category was successfully destroyed.", status: :see_other
end
private
# Use callbacks to share common setup or constraints between actions.
def set_link_category
@link_category = LinkCategory.find(params[:id])
end
# Only allow a list of trusted parameters through.
def link_category_params
params.require(:link_category).permit(:name, :description, :rich_text)
end
end

View file

@ -0,0 +1,59 @@
class LinksController < ApplicationController
before_action :set_link, only: %i[show edit update destroy]
# GET /links
def index
@links = Link.all
end
# GET /links/1
def show
end
# GET /links/new
def new
@link = Link.new
end
# GET /links/1/edit
def edit
end
# POST /links
def create
@link = Link.new(link_params)
if @link.save
redirect_to @link, notice: 'Link was successfully created.'
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /links/1
def update
if @link.update(link_params)
redirect_to @link, notice: 'Link was successfully updated.', status: :see_other
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /links/1
def destroy
@link.destroy!
redirect_to links_url, notice: 'Link was successfully destroyed.', status: :see_other
end
private
# Use callbacks to share common setup or constraints between actions.
def set_link
@link = Link.find(params[:id])
end
# Only allow a list of trusted parameters through.
def link_params
params.require(:link).permit(:url, :text, :description_html, :last_check_at, :fail_count, :link_category_id)
end
end

View file

@ -0,0 +1,2 @@
module LinkCategoriesHelper
end

View file

@ -0,0 +1,2 @@
module LinksHelper
end

View file

@ -0,0 +1,19 @@
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="check-link"
export default class extends Controller {
static targets = ["input", "button"]
connect() {
console.log("connect", this.inputTarget, this.buttonTarget)
this.inputTarget.addEventListener("input", e => this.onUrlInputChange(e))
}
onUrlInputChange(event) {
console.log("connect", this.inputTarget, this.buttonTarget)
console.log(event, this.buttonTarget)
this.buttonTarget.href = this.inputTarget.value;
this.buttonTarget.innerHTML = this.inputTarget.value;
return true;
}
}

View file

@ -7,6 +7,9 @@ import { application } from "./application"
import AutosubmitController from "./autosubmit_controller"
application.register("autosubmit", AutosubmitController)
import CheckLinkController from "./check_link_controller"
application.register("check-link", CheckLinkController)
import CollapseChevronTogglerController from "./collapse_chevron_toggler_controller"
application.register("collapse-chevron-toggler", CollapseChevronTogglerController)

16
app/lib/link_checker.rb Normal file
View file

@ -0,0 +1,16 @@
module LinkChecker
UA = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0"
module_function def http(input)
response = Net::HTTP.get_response(URI.parse(input), {"User-Agent": UA})
# debugger
case response
when Net::HTTPSuccess
response.uri
when Net::HTTPRedirection
http(response[:location])
else
false
end
end
end

View file

@ -7,4 +7,27 @@ class Element < ApplicationRecord
has_many :success_criteria, dependent: :destroy
validates :path, :title, presence: true
# Calculate actual conformity level:
# - if a success_criterion has result :failed -> the confirmity_level
# of that success_criterion is not reached.
# - the resulting level is the highest readched level
# abeying rule above.
def level
return nil
return nil unless success_criteria.all(&:result)
min_failed = success_criteria.select(&:failed?).map(&:level).min
possible_levels = success_criteria.select(&:passed?).map(&:level).uniq
return nil if possible_levels.empty?
puts possible_levels.inspect
puts min_failed
possible_levels[possible_levels.find_index(min_failed) - 1]
end
def max_level
@max_level ||= success_criteria.reject(&:not_applicable?).max(&:level)
end
end

51
app/models/link.rb Normal file
View file

@ -0,0 +1,51 @@
class Link < ApplicationRecord
belongs_to :link_category
has_rich_text :description_html
validates :url, :text, presence: true
validate :check_url
validate :valid_url
before_validation :ensure_absolute_url
def ok?
fail_count == 0
end
private
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
def check_url(input = url, add_error = true)
response = begin
Net::HTTP.get_response(URI.parse(input), "User-Agent": USER_AGENT)
rescue Socket::ResolutionError
errors.add(:url, :invalid) if add_error
return false
end
case response
when Net::HTTPSuccess
self.last_check_at = Time.zone.now
self.fail_count = 0
true
when Net::HTTPRedirection
check_url(response[:location])
else
self.last_check_at = Time.zone.now
self.fail_count += 1
errors.add(:url, :invalid) if add_error
false
end
end
def ensure_absolute_url
self.url = "https://#{self.url}" unless self.url.match?(%r{https{0,1}://.*})
end
def valid_url
errors.add(:url, :invalid) unless url.match /\A#{URI::regexp(['http', 'https'])}\z/
end
end

View file

@ -0,0 +1,5 @@
class LinkCategory < ApplicationRecord
has_many :links
has_rich_text :description_html
end

View file

@ -6,4 +6,10 @@ class SuccessCriterion < ApplicationRecord
has_rich_text :description_html
belongs_to :element, touch: true
def level_value
return nil unless level
self.class.levels.values.index_of(level)
end
end

View file

@ -12,7 +12,6 @@
</div>
<% end %>
<%== pagy_info(@pagy) %>
<table class="table table-striped">
<thead>
<tr>
@ -32,9 +31,12 @@
<% end %>
</tbody>
</table>
<div class="my-3">
<%== pagy_info(@pagy) %>
</div>
<%== pagy_bootstrap_nav(@pagy) %>
<div class="action-row">
<%= link_to t("scaffold.link_new", model: Check.model_name.human), new_check_path %>
</div>
</div>

View file

@ -2,6 +2,7 @@
<div class="container">
<a class="navbar-brand" href="<%= root_path %>">
<%= tag.i(class: "bi bi-universal-access") %>
a11ydive
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -61,4 +62,4 @@
</ul>
</div>
</div>
</nav>
</nav>

View file

@ -1,7 +1,7 @@
<!doctype html>
<html <%== cookies[:"modeTheme"] && "data-bs-theme=\"#{cookies[:"modeTheme"]}\"" %> data-controller="set-theme">
<head>
<title>A11yDive</title>
<title>a11ydive</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>

View file

@ -0,0 +1,5 @@
<%= bootstrap_form_with(model: link_category) do |form| %>
<%= form.text_field :name %>
<%= form.rich_text_area :description_html %>
<%= form.submit %>
<% end %>

View file

@ -0,0 +1,12 @@
<div id="<%= dom_id link_category %>">
<p>
<strong>Name:</strong>
<%= link_category.name %>
</p>
<p>
<strong>Description:</strong>
<%= link_category.description_html %>
</p>
</div>

View file

@ -0,0 +1,2 @@
json.extract! link_category, :id, :name, :description, :rich_text, :created_at, :updated_at
json.url link_category_url(link_category, format: :json)

View file

@ -0,0 +1,8 @@
<h1><%= t("scaffold.pagetitle_edit", model: LinkCategory.model_name.human) %></h1>
<%= render "form", link_category: @link_category %>
<div class="action-row">
<%= link_to t("scaffold.link_show", model: LinkCategory.model_name.human), @link_category %>
<%= link_to t("scaffold.link_index", model: LinkCategory.model_name.human(count: 2)), link_categories_path %>
</div>

View file

@ -0,0 +1,29 @@
<h1><%= t("scaffold.pagetitle_index", model: LinkCategory.model_name.human(count: 2)) %></h1>
<table class="table table-striped">
<thead>
<tr>
<th><%= LinkCategory.human_attribute_name(:id) %></th>
<th><%= LinkCategory.human_attribute_name(:name) %></th>
<th><%= LinkCategory.human_attribute_name(:description) %></th>
</thead>
<tbody>
<% @link_categories.each do |link_category| %>
<tr>
<td><%= link_to(link_category.id, url_for(link_category)) %></td>
<td><%= link_to(link_category.name, url_for(link_category)) %></td>
<td><%= link_to(link_category.description, url_for(link_category)) %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="action-row">
<%= link_to t("scaffold.link_new", model: LinkCategory.model_name.human), new_link_category_path %>
</div>

View file

@ -0,0 +1 @@
json.array! @link_categories, partial: "link_categories/link_category", as: :link_category

View file

@ -0,0 +1,7 @@
<h1><%= t("scaffold.pagetitle_new", model: LinkCategory.model_name.human) %></h1>
<%= render "form", link_category: @link_category %>
<div class="action-row">
<%= link_to t("scaffold.link_index", model: LinkCategory.model_name.human(count: 2)), link_categories_path %>
</div>

View file

@ -0,0 +1,9 @@
<h1><%= t("scaffold.pagetitle_show", model: @link_category.class.model_name.human) %></h1>
<%= render @link_category %>
<div class="action-row">
<%= link_to t("scaffold.link_edit", model: @link_category.model_name.human), edit_link_category_path(@link_category) %>
<%= link_to t("scaffold.link_index", model: @link_category.model_name.human(count: 2)), link_categories_path %>
<%= button_to t("scaffold.link_destroy", model: @link_category.model_name.human), @link_category, method: :delete, class: "btn btn-outline-danger" %>
</div>

View file

@ -0,0 +1 @@
json.partial! "link_categories/link_category", link_category: @link_category

View file

@ -0,0 +1,12 @@
<%= bootstrap_form_with(model: link) do |form| %>
<div data-controller="check-link">
<%= form.text_field :url, placeholder: "https://wikipedia.org", data: { "check-link-target": "input" } %>
<%= link_to(link.url, link.url, target: "_blank", class: "mb-2", data: { "check-link-target": "button" }) %>
</div>
<%= form.collection_select :link_category_id, LinkCategory.all.order(:name), :id, :name, include_blank: !link.persisted? %>
<%= form.text_field :text %>
<%= form.rich_text_area :description %>
<%= form.submit %>
<% end %>

View file

@ -0,0 +1,32 @@
<div id="<%= dom_id link %>">
<p>
<strong>Url:</strong>
<%= link.url %>
</p>
<p>
<strong>Text:</strong>
<%= link.text %>
</p>
<p>
<strong>Description:</strong>
<%= link.description_html %>
</p>
<p>
<strong>Last check at:</strong>
<%= link.last_check_at %>
</p>
<p>
<strong>Fail count:</strong>
<%= link.fail_count %>
</p>
<p>
<strong>Link category:</strong>
<%= link.link_category_id %>
</p>
</div>

View file

@ -0,0 +1,3 @@
json.extract! link, :id, :url, :text, :description, :last_check_at, :fail_count, :link_category_id, :created_at, :updated_at
json.url link_url(link, format: :json)
json.description link.description.to_s

View file

@ -0,0 +1,8 @@
<h1><%= t("scaffold.pagetitle_edit", model: Link.model_name.human) %></h1>
<%= render "form", link: @link %>
<div class="action-row">
<%= link_to t("scaffold.link_show", model: Link.model_name.human), @link %>
<%= link_to t("scaffold.link_index", model: Link.model_name.human(count: 2)), links_path %>
</div>

View file

@ -0,0 +1,30 @@
<h1><%= t("scaffold.pagetitle_index", model: Link.model_name.human(count: 2)) %></h1>
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th><%= LinkCategory.model_name.human %></th>
<th><%= Link.human_attribute_name(:url) %></th>
<th><%= Link.human_attribute_name(:text) %></th>
<th><%= Link.human_attribute_name(:description) %></th>
<th><%= Link.human_attribute_name(:last_check_at) %></th>
</tr>
</thead>
<tbody>
<% @links.each do |link| %>
<tr>
<td><%= link_to(tag.i(class: link.ok? ? "bi bi-check" : "bi bi-x-lg"), url_for(link)) %></td>
<td><%= link_to(link.link_category.name, url_for(link)) %></td>
<td><%= link_to(truncate(link.url), link.url, target: "_blank", rel: "nofollow") %></td>
<td><%= link_to(link.text, url_for(link)) %></td>
<td><%= link_to(truncate(link.description_html.to_plain_text), url_for(link)) %></td>
<td><%= link.last_check_at && l(link.last_check_at, format: :short) %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="action-row">
<%= link_to t("scaffold.link_new", model: Link.model_name.human), new_link_path %>
</div>

View file

@ -0,0 +1 @@
json.array! @links, partial: "links/link", as: :link

View file

@ -0,0 +1,7 @@
<h1><%= t("scaffold.pagetitle_new", model: Link.model_name.human) %></h1>
<%= render "form", link: @link %>
<div class="action-row">
<%= link_to t("scaffold.link_index", model: Link.model_name.human(count: 2)), links_path %>
</div>

View file

@ -0,0 +1,8 @@
<h1><%= t("scaffold.pagetitle_show", model: @link.class.model_name.human) %></h1>
<%= render @link %>
<div class="action-row">
<%= link_to t("scaffold.link_edit", model: @link.model_name.human), edit_link_path(@link) %>
<%= link_to t("scaffold.link_index", model: @link.model_name.human(count: 2)), links_path %>
<%= button_to t("scaffold.link_destroy", model: @link.model_name.human), @link, method: :delete, class: "btn btn-outline-danger" %>
</div>

View file

@ -0,0 +1 @@
json.partial! "links/link", link: @link

View file

@ -18,7 +18,7 @@
<td><%= link_to(report.name, url_for(report)) %></td>
<td><%= link_to(truncate(strip_tags(report.comment)), url_for(report)) if report.comment %></td>
<td><%= link_to(truncate(report.comment_html.to_plain_text), url_for(report)) if report.comment_html %></td>
<td><%= l(report.created_at, format: :short) %></td>
<td><%= l(report.updated_at, format: :short) %></td>
</tr>
@ -28,4 +28,4 @@
<div class="action-row">
<%= link_to t("scaffold.link_new", model: Report.model_name.human), new_report_path %>
</div>
</div>