Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for server error on the tags page when the category doesn't have tags #1486

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/models/tag_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion db/scripts/create_tags_path_view.sql
Original file line number Diff line number Diff line change
@@ -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
(
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/tag_sets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ main:
meta:
name: Meta
community: sample

empty:
name: 'Empty'
community: sample
1 change: 1 addition & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ support:

bug:
name: bug
excerpt: use for bug reports
community: sample
tag_set: main

Expand Down
17 changes: 14 additions & 3 deletions test/models/tag_set_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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

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
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading