From 8851b79472faf1d0f53fa2e7c4d07c91ca96a9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 20 May 2020 18:00:25 +0200 Subject: [PATCH] DEV: less code for Category#url --- app/models/category.rb | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 63db814752e..6ecfe9d3afe 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -709,37 +709,29 @@ class Category < ActiveRecord::Base ].include? id end - @@url_cache = DistributedCache.new('category_url') + def full_slug(separator = "-") + start_idx = "#{Discourse.base_uri}/c/".size + url[start_idx..-1].gsub("/", separator) + end + + @@url_cache = DistributedCache.new("category_url") def clear_url_cache @@url_cache.clear end - def full_slug(separator = "-") - start_idx = "#{Discourse.base_uri}/c/".length - url[start_idx..-1].gsub("/", separator) - end - def url - url = @@url_cache[self.id] - unless url - url = "#{Discourse.base_uri}/c/#{slug_path.join('/')}" - - @@url_cache[self.id] = url - end - - url + @@url_cache[self.id] ||= "#{Discourse.base_uri}/c/#{slug_path.join('/')}" end def url_with_id self.parent_category ? "#{url}/#{self.id}" : "#{Discourse.base_uri}/c/#{self.slug}/#{self.id}" end - # If the name changes, try and update the category definition topic too if it's - # an exact match + # If the name changes, try and update the category definition topic too if it's an exact match def rename_category_definition - old_name = saved_changes.transform_values(&:first)["name"] return unless topic.present? + old_name = saved_changes.transform_values(&:first)["name"] if topic.title == I18n.t("category.topic_prefix", category: old_name) topic.update_attribute(:title, I18n.t("category.topic_prefix", category: name)) end