From 91cb7a1ff3bbec236c0a9caddadeacb72d9dd744 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 14:05:17 +0300 Subject: [PATCH 1/6] started adding tests for the tags_set model --- test/fixtures/tag_sets.yml | 4 ++++ test/models/tag_set_test.rb | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/fixtures/tag_sets.yml b/test/fixtures/tag_sets.yml index df6517d05..57b91b8a2 100644 --- a/test/fixtures/tag_sets.yml +++ b/test/fixtures/tag_sets.yml @@ -5,3 +5,7 @@ main: meta: name: Meta community: sample + +empty: + name: 'Empty' + community: sample diff --git a/test/models/tag_set_test.rb b/test/models/tag_set_test.rb index c5ea5ed11..9583ccc00 100644 --- a/test/models/tag_set_test.rb +++ b/test/models/tag_set_test.rb @@ -1,7 +1,9 @@ require 'test_helper' class TagSetTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + include CommunityRelatedHelper + + test 'is community related' do + assert_community_related(TagSet) + end end From 13f109985f68b6f45539b5f212c7aa4ba05b94f6 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 14:11:43 +0300 Subject: [PATCH 2/6] added with_paths method to TagSet model to properly handle no_excerpt params --- app/models/tag_set.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/tag_set.rb b/app/models/tag_set.rb index e71e33c89..150dab46a 100644 --- a/app/models/tag_set.rb +++ b/app/models/tag_set.rb @@ -13,4 +13,12 @@ def self.meta def self.main where(name: 'Main').first end + + def with_paths(no_excerpt = false) + if no_excerpt + tags_with_paths.where(excerpt: ['', nil]) + else + tags_with_paths + end + end end From b3ce4da71e24961914ddfd7276bc8c227820c0b4 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 14:12:05 +0300 Subject: [PATCH 3/6] fixed server error on categories with empty tag sets --- app/controllers/tags_controller.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 134c5dbff..98f133329 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -27,20 +27,27 @@ def category @tags = if params[:q].present? @tag_set.tags.search(params[:q]) elsif params[:hierarchical].present? - @tag_set.tags_with_paths.order(:path) + @tag_set.with_paths(params[:no_excerpt]) elsif params[:no_excerpt].present? - @tag_set.tags.where(excerpt: '').or(@tag_set.tags.where(excerpt: nil)) - .order(Arel.sql('COUNT(posts.id) DESC')) + @tag_set.tags.where(excerpt: ['', nil]) else - @tag_set&.tags&.order(Arel.sql('COUNT(posts.id) DESC')) + @tag_set&.tags end - @count = @tags&.size || 0 + table = params[:hierarchical].present? ? 'tags_paths' : 'tags' - if @count.positive? - @tags = @tags.left_joins(:posts).group(Arel.sql("#{table}.id")) - .select(Arel.sql("#{table}.*, COUNT(DISTINCT IF(posts.deleted = 0, posts.id, NULL)) AS post_count")) - .paginate(per_page: 96, page: params[:page]) - end + + @tags = @tags&.left_joins(:posts) + &.group(Arel.sql("#{table}.id")) + &.select(Arel.sql("#{table}.*, COUNT(DISTINCT IF(posts.deleted = 0, posts.id, NULL)) AS post_count")) + &.paginate(per_page: 96, page: params[:page]) + + @tags = if params[:hierarchical].present? + @tags&.order(:path) + else + @tags&.order(Arel.sql('COUNT(posts.id) DESC')) + end + + @count = @tags&.length || 0 end def show From 4574d9a41b30423dfdb54fd15cefec5a3132fe1a Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 15:08:19 +0300 Subject: [PATCH 4/6] ensured create_tags_path_view doesn't fail if view already exists --- db/scripts/create_tags_path_view.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/scripts/create_tags_path_view.sql b/db/scripts/create_tags_path_view.sql index 92708f38b..6752f6734 100644 --- a/db/scripts/create_tags_path_view.sql +++ b/db/scripts/create_tags_path_view.sql @@ -1,4 +1,4 @@ -create view tags_paths as +CREATE OR REPLACE VIEW tags_paths AS WITH RECURSIVE tag_path (id, created_at, updated_at, community_id, tag_set_id, wiki_markdown, wiki, excerpt, parent_id, name, path) AS ( From 1e4ea6d5d96848b4041d8d058b0200cea45bc835 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 15:08:52 +0300 Subject: [PATCH 5/6] ensured create_tags_path_view is executed when running tests --- test/test_helper.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_helper.rb b/test/test_helper.rb index 8e885708b..638cf4085 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,6 +25,7 @@ class ActiveSupport::TestCase def load_fixtures(config) # Loading a fixture deletes all data in the same tables, so it has to happen before we load our normal seeds. fixture_data = super(config) + load_tags_paths load_seeds # We do need to return the same thing as the original method to not break fixtures @@ -42,6 +43,10 @@ def load_seeds Rails.application.load_seed end + def load_tags_paths + ActiveRecord::Base.connection.execute File.read(Rails.root.join('db/scripts/create_tags_path_view.sql')) + end + def clear_cache Rails.cache.clear end From 8608aad278f9ca107cbae452d4dfd9d1c8f85e25 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 15 Dec 2024 15:09:44 +0300 Subject: [PATCH 6/6] added test for TagSet's model with_paths method --- test/fixtures/tags.yml | 1 + test/models/tag_set_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml index 0c5a1af59..422794b4e 100644 --- a/test/fixtures/tags.yml +++ b/test/fixtures/tags.yml @@ -10,6 +10,7 @@ support: bug: name: bug + excerpt: use for bug reports community: sample tag_set: main diff --git a/test/models/tag_set_test.rb b/test/models/tag_set_test.rb index 9583ccc00..39e8b086b 100644 --- a/test/models/tag_set_test.rb +++ b/test/models/tag_set_test.rb @@ -6,4 +6,13 @@ class TagSetTest < ActiveSupport::TestCase test 'is community related' do assert_community_related(TagSet) end + + test 'with_paths method should respect no_excerpt' do + main = TagSet.main + + all = main.with_paths.size + excerptless = main.with_paths(true).size + + assert_not_equal(all, excerptless) + end end