Files
manyfold/app/components/path_template_preview.rb

64 lines
2.0 KiB
Ruby

class Components::PathTemplatePreview < Components::Base
include Phlex::Rails::Helpers::TurboFrameTag
def initialize(library:)
@library = library
end
def before_template
if @library.valid? && @library.parse_metadata_from_path
@paths = @library.sample(3).map { [it, PathParserService.new(@library.path_template, it).call] }
end
end
def view_template
turbo_frame_tag "parse-preview" do
if @paths
p { t("components.path_template_preview.description") }
table class: "table table-striped table-sm" do
tr do
th { t("components.path_template_preview.path") }
th { Creator.model_name.human }
th { Collection.model_name.human }
th { ActsAsTaggableOn::Tag.model_name.human(count: 100) }
th { Model.model_name.human }
end
@paths.map do |path, parsed|
tr do
td(style: "word-break: break-all") { path }
td { find_or_new_from_path_component(Creator, parsed[:creator])&.name || "❌" }
td { parsed[:collections] ? Array(find_or_new_from_path_component(Collection, parsed[:collections])).map(&:name).join(", ") : "❌" }
td {
parsed[:tags]&.map do
Tag tag: ActsAsTaggableOn::Tag.new(name: it), link: false
whitespace
end || "❌"
}
td { to_human_name(parsed[:model_name]) || "❌" }
end
end
end
end
end
end
private
def find_or_new_from_path_component(klass, path_component)
return unless path_component
if path_component.is_a? Array
path_component.map { find_or_new_from_path_component(klass, it) }
else
klass.find_by(slug: path_component) ||
klass.find_by(
name: to_human_name(path_component)
) ||
klass.new(name: to_human_name(path_component))
end
end
def to_human_name(str)
str&.humanize&.tr("+", " ")&.careful_titleize
end
end