From 250ef8b5d4f6cb457c4976528f72f6ea7bf5f6cc Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 13 Oct 2023 12:50:33 -0400 Subject: Tag cloud --- app/assets/stylesheets/main/entries.scss | 37 ++++++++++++++++++++++++++++++++ app/controllers/tags_controller.rb | 4 ++++ app/helpers/tags_helper.rb | 11 ++++++++++ app/views/tags/index.html.haml | 4 ++++ 4 files changed, 56 insertions(+) create mode 100644 app/helpers/tags_helper.rb create mode 100644 app/views/tags/index.html.haml (limited to 'app') diff --git a/app/assets/stylesheets/main/entries.scss b/app/assets/stylesheets/main/entries.scss index a4e9463..8d5796f 100644 --- a/app/assets/stylesheets/main/entries.scss +++ b/app/assets/stylesheets/main/entries.scss @@ -355,3 +355,40 @@ margin-left: 1em; } } + +#tag-cloud { + width: 75%; + margin: 0 auto; + text-align: center; + + a { + display: inline-block; + margin-right: 0.5em; + text-decoration: none; + + &:hover { + text-decoration: underline; + } + + &:visited { + color: blue; + } + } + + .xs { + font-size: 0.75em; + } + + .m { + font-size: 2em; + } + + .l { + font-size: 3em; + } + + .xl { + font-size: 3em; + font-weight: bold; + } +} diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index bceb3cf..355706b 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -1,5 +1,9 @@ class TagsController < ApplicationController + def index + @tags = Blog.tag_counts + end + def show @tag_name = params[:name] @blogs = Blog.tagged_with(params[:name]).where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb new file mode 100644 index 0000000..9643834 --- /dev/null +++ b/app/helpers/tags_helper.rb @@ -0,0 +1,11 @@ +module TagsHelper + + def tag_cloud(tags, classes) + max = tags.sort_by(&:taggings_count).last + tags.each do |tag| + index = tag.taggings_count.to_f / max.taggings_count * (classes.size - 1) + yield(tag, classes[index.round]) + end + end + +end diff --git a/app/views/tags/index.html.haml b/app/views/tags/index.html.haml new file mode 100644 index 0000000..c9f1e85 --- /dev/null +++ b/app/views/tags/index.html.haml @@ -0,0 +1,4 @@ +%h2.centered Tag cloud +#tag-cloud + - tag_cloud @tags, %w[xs s m l xl] do |tag, css_class| + = link_to tag.name, tag_url(tag.name), class: css_class, title: tag.taggings_count -- cgit 1.4.1