diff --git a/app/components/application_component.rb b/app/components/application_component.rb index 96f355c03..c162fbedc 100644 --- a/app/components/application_component.rb +++ b/app/components/application_component.rb @@ -7,12 +7,4 @@ class ApplicationComponent < ViewComponent::Base def policy(subject) Pundit.policy!(current_user, subject) end - - # XXX Silence warnings about `with_variant` being deprecated until we can fix it. - # DEPRECATION WARNING: `with_variant` is deprecated and will be removed in ViewComponent v3.0.0 - def with_variant(...) - ActiveSupport::Deprecation.silence do - super(...) - end - end end diff --git a/app/components/categorized_tag_list_component.rb b/app/components/categorized_tag_list_component.rb new file mode 100644 index 000000000..abb2104e2 --- /dev/null +++ b/app/components/categorized_tag_list_component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# A vertical tag list, with tags split into categories. Used in app/views/posts/show.html.erb. +class CategorizedTagListComponent < TagListComponent +end diff --git a/app/components/tag_list_component/tag_list_component.html+categorized.erb b/app/components/categorized_tag_list_component/categorized_tag_list_component.html.erb similarity index 100% rename from app/components/tag_list_component/tag_list_component.html+categorized.erb rename to app/components/categorized_tag_list_component/categorized_tag_list_component.html.erb diff --git a/app/components/inline_tag_list_component.rb b/app/components/inline_tag_list_component.rb new file mode 100644 index 000000000..c3a2cfb2a --- /dev/null +++ b/app/components/inline_tag_list_component.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# A horizontal tag list, with tags grouped by category. Used in post tooltips, +# on the comments index, and in the modqueue. +class InlineTagListComponent < TagListComponent +end diff --git a/app/components/tag_list_component/tag_list_component.html+inline.erb b/app/components/inline_tag_list_component/inline_tag_list_component.html.erb similarity index 100% rename from app/components/tag_list_component/tag_list_component.html+inline.erb rename to app/components/inline_tag_list_component/inline_tag_list_component.html.erb diff --git a/app/components/meta_links_component.rb b/app/components/meta_links_component.rb new file mode 100644 index 000000000..e6cc12ae9 --- /dev/null +++ b/app/components/meta_links_component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# The / links in the element of the . +class MetaLinksComponent < PaginatorComponent +end diff --git a/app/components/paginator_component/paginator_component.html+meta_links.erb b/app/components/meta_links_component/meta_links_component.html.erb similarity index 100% rename from app/components/paginator_component/paginator_component.html+meta_links.erb rename to app/components/meta_links_component/meta_links_component.html.erb diff --git a/app/components/numbered_paginator_component.rb b/app/components/numbered_paginator_component.rb new file mode 100644 index 000000000..d1a10c659 --- /dev/null +++ b/app/components/numbered_paginator_component.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class NumberedPaginatorComponent < PaginatorComponent +end diff --git a/app/components/paginator_component/paginator_component.html+numbered.erb b/app/components/numbered_paginator_component/numbered_paginator_component.html.erb similarity index 100% rename from app/components/paginator_component/paginator_component.html+numbered.erb rename to app/components/numbered_paginator_component/numbered_paginator_component.html.erb diff --git a/app/components/paginator_component.rb b/app/components/paginator_component.rb index 697666391..dc6c4dacd 100644 --- a/app/components/paginator_component.rb +++ b/app/components/paginator_component.rb @@ -13,10 +13,6 @@ class PaginatorComponent < ApplicationComponent @params = params end - def use_sequential_paginator? - paginator_mode != :numbered - end - def pages last_page = total_pages.clamp(1..) left = (current_page - window).clamp(2..) diff --git a/app/components/related_tag_list_component.rb b/app/components/related_tag_list_component.rb new file mode 100644 index 000000000..ed47bfe2c --- /dev/null +++ b/app/components/related_tag_list_component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# A simple vertical tag list with no post counts. Used in related tags. +class RelatedTagListComponent < TagListComponent +end diff --git a/app/components/tag_list_component/tag_list_component.html+simple.erb b/app/components/related_tag_list_component/related_tag_list_component.html.erb similarity index 100% rename from app/components/tag_list_component/tag_list_component.html+simple.erb rename to app/components/related_tag_list_component/related_tag_list_component.html.erb diff --git a/app/components/search_tag_list_component.rb b/app/components/search_tag_list_component.rb new file mode 100644 index 000000000..f5b5b3bbe --- /dev/null +++ b/app/components/search_tag_list_component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# A vertical tag list, used in the post index sidebar. +class SearchTagListComponent < TagListComponent +end diff --git a/app/components/tag_list_component/tag_list_component.html+search.erb b/app/components/search_tag_list_component/search_tag_list_component.html.erb similarity index 100% rename from app/components/tag_list_component/tag_list_component.html+search.erb rename to app/components/search_tag_list_component/search_tag_list_component.html.erb diff --git a/app/components/sequential_paginator_component.rb b/app/components/sequential_paginator_component.rb new file mode 100644 index 000000000..da1d244e8 --- /dev/null +++ b/app/components/sequential_paginator_component.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class SequentialPaginatorComponent < PaginatorComponent +end diff --git a/app/components/paginator_component/paginator_component.html+sequential.erb b/app/components/sequential_paginator_component/sequential_paginator_component.html.erb similarity index 100% rename from app/components/paginator_component/paginator_component.html+sequential.erb rename to app/components/sequential_paginator_component/sequential_paginator_component.html.erb diff --git a/app/helpers/components_helper.rb b/app/helpers/components_helper.rb index fdb4843c6..15aaa4124 100644 --- a/app/helpers/components_helper.rb +++ b/app/helpers/components_helper.rb @@ -56,47 +56,45 @@ module ComponentsHelper end # A simple vertical tag list with no post counts. Used in related tags. - def render_simple_tag_list(tag_names, **options) - tags = TagListComponent.tags_from_names(tag_names) - render TagListComponent.new(tags: tags, **options).with_variant(:simple) + def render_related_tag_list(tag_names, **options) + tags = RelatedTagListComponent.tags_from_names(tag_names) + render RelatedTagListComponent.new(tags: tags, **options) end # A horizontal tag list, with tags grouped by category. Used in post # tooltips, on the comments index, and in the modqueue. def render_inline_tag_list(post, **options) - render TagListComponent.new(tags: post.tags, **options).with_variant(:inline) + render InlineTagListComponent.new(tags: post.tags, **options) end def render_inline_tag_list_from_names(tag_names, **options) - tags = TagListComponent.tags_from_names(tag_names) - render TagListComponent.new(tags: tags, **options).with_variant(:inline) + tags = InlineTagListComponent.tags_from_names(tag_names) + render InlineTagListComponent.new(tags: tags, **options) end # A vertical tag list, with tags split into categories. Used on post show pages. def render_categorized_tag_list(post, **options) - render TagListComponent.new(tags: post.tags, **options).with_variant(:categorized) + render CategorizedTagListComponent.new(tags: post.tags, **options) end # A vertical tag list, used in the post index sidebar. def render_search_tag_list(tag_names, **options) - tags = TagListComponent.tags_from_names(tag_names) - render TagListComponent.new(tags: tags, **options).with_variant(:search) + tags = SearchTagListComponent.tags_from_names(tag_names) + render SearchTagListComponent.new(tags: tags, **options) end # The / links in the element of the . def render_meta_links(records) - render PaginatorComponent.new(records: records, params: params).with_variant(:meta_links) + render MetaLinksComponent.new(records: records, params: params) rescue ActiveRecord::StatementInvalid # Swallow any exceptions when loading records so that the page load doesn't fail. end def numbered_paginator(records) - paginator = PaginatorComponent.new(records: records, params: params) - - if paginator.use_sequential_paginator? - render paginator.with_variant(:sequential) + if records.paginator_mode == :numbered + render NumberedPaginatorComponent.new(records: records, params: params) else - render paginator.with_variant(:numbered) + render SequentialPaginatorComponent.new(records: records, params: params) end end end diff --git a/app/views/related_tags/_tag_column.html.erb b/app/views/related_tags/_tag_column.html.erb index c48d305e0..08e12406f 100644 --- a/app/views/related_tags/_tag_column.html.erb +++ b/app/views/related_tags/_tag_column.html.erb @@ -6,6 +6,6 @@ <%= title %> - <%= render_simple_tag_list(tags) %> + <%= render_related_tag_list(tags) %> <% end %> <% end %> diff --git a/test/components/paginator_component_test.rb b/test/components/paginator_component_test.rb index 33b9a291c..9659fbaa4 100644 --- a/test/components/paginator_component_test.rb +++ b/test/components/paginator_component_test.rb @@ -1,12 +1,16 @@ require "test_helper" class PaginatorComponentTest < ViewComponent::TestCase - def render_paginator(variant, records, page: 1, limit: 3, page_limit: 100) - with_variant(variant) do - records = records.paginate(page, limit: limit, page_limit: page_limit) - params = ActionController::Parameters.new(controller: records.model_name.plural, action: :index) - return render_inline(PaginatorComponent.new(records: records, params: params)) - end + def render_sequential_paginator(records, page: 1, limit: 3, page_limit: 100) + records = records.paginate(page, limit: limit, page_limit: page_limit) + params = ActionController::Parameters.new(controller: records.model_name.plural, action: :index) + return render_inline(SequentialPaginatorComponent.new(records: records, params: params)) + end + + def render_numbered_paginator(records, page: 1, limit: 3, page_limit: 100) + records = records.paginate(page, limit: limit, page_limit: page_limit) + params = ActionController::Parameters.new(controller: records.model_name.plural, action: :index) + return render_inline(NumberedPaginatorComponent.new(records: records, params: params)) end def assert_page(expected_page, link) @@ -24,14 +28,14 @@ class PaginatorComponentTest < ViewComponent::TestCase end should "work with an aN page" do - html = render_paginator(:sequential, Tag.all, page: "a#{@tags[5].id}", limit: 3) + html = render_sequential_paginator(Tag.all, page: "a#{@tags[5].id}", limit: 3) assert_page("a#{@tags[5+3].id}", html.css("a[rel=prev]")) assert_page("b#{@tags[5+1].id}", html.css("a[rel=next]")) end should "work with a bN page" do - html = render_paginator(:sequential, Tag.all, page: "b#{@tags[5].id}", limit: 3) + html = render_sequential_paginator(Tag.all, page: "b#{@tags[5].id}", limit: 3) assert_page("a#{@tags[5-1].id}", html.css("a[rel=prev]")) assert_page("b#{@tags[5-3].id}", html.css("a[rel=next]")) @@ -45,21 +49,21 @@ class PaginatorComponentTest < ViewComponent::TestCase end should "work for page 1" do - html = render_paginator(:numbered, Tag.all, page: 1, limit: 3) + html = render_numbered_paginator(Tag.all, page: 1, limit: 3) assert_css("span.paginator-prev") assert_page("2", html.css("a.paginator-next")) end should "work for page 2" do - html = render_paginator(:numbered, Tag.all, page: 2, limit: 3) + html = render_numbered_paginator(Tag.all, page: 2, limit: 3) assert_page("1", html.css("a.paginator-prev")) assert_page("3", html.css("a.paginator-next")) end should "work for page 4" do - html = render_paginator(:numbered, Tag.all, page: 4, limit: 3) + html = render_numbered_paginator(Tag.all, page: 4, limit: 3) assert_page("3", html.css("a.paginator-prev")) assert_css("span.paginator-next") @@ -68,7 +72,7 @@ class PaginatorComponentTest < ViewComponent::TestCase context "for a search with zero results" do should "work for page 1" do - html = render_paginator(:numbered, Tag.none, page: 1, limit: 3) + html = render_numbered_paginator(Tag.none, page: 1, limit: 3) assert_css("span.paginator-current", text: "1") assert_css("span.paginator-prev") @@ -81,7 +85,7 @@ class PaginatorComponentTest < ViewComponent::TestCase should "show the unlimited paginator" do @tags = Tag.all @tags.stubs(:total_count).returns(Float::INFINITY) - html = render_paginator(:numbered, @tags, page: 1, limit: 200) + html = render_numbered_paginator(@tags, page: 1, limit: 200) assert_css("span.paginator-current", text: "1") assert_css("span.paginator-prev") diff --git a/test/components/tag_list_component_test.rb b/test/components/tag_list_component_test.rb index 5f0a25ca0..31e4c65fb 100644 --- a/test/components/tag_list_component_test.rb +++ b/test/components/tag_list_component_test.rb @@ -1,12 +1,6 @@ require "test_helper" class TagListComponentTest < ViewComponent::TestCase - def render_tag_list(tags, variant, **options) - with_variant(variant) do - render_inline(TagListComponent.new(tags: tags, **options)) - end - end - context "The TagListComponent" do setup do @arttag = create(:artist_tag) @@ -17,9 +11,9 @@ class TagListComponentTest < ViewComponent::TestCase @tags = Tag.all end - context "for a simple tag list" do + context "for a related tag list" do should "render" do - render_tag_list(@tags, :simple) + render_inline(RelatedTagListComponent.new(tags: @tags)) assert_css(".simple-tag-list a.search-tag", count: 5) end @@ -27,7 +21,7 @@ class TagListComponentTest < ViewComponent::TestCase context "for an inline tag list" do should "render" do - render_tag_list(@tags, :inline) + render_inline(InlineTagListComponent.new(tags: @tags)) assert_css(".inline-tag-list a.search-tag", count: 5) end @@ -36,7 +30,7 @@ class TagListComponentTest < ViewComponent::TestCase context "for a search tag list" do context "with +/- links" do should "render" do - render_tag_list(@tags, :search, current_query: "touhou", show_extra_links: true) + render_inline(SearchTagListComponent.new(tags: @tags, current_query: "touhou", show_extra_links: true)) assert_css(".search-tag-list li a.search-tag", count: 5) end @@ -44,7 +38,7 @@ class TagListComponentTest < ViewComponent::TestCase context "without +/- links" do should "render" do - render_tag_list(@tags, :search) + render_inline(SearchTagListComponent.new(tags: @tags)) assert_css(".search-tag-list li a.search-tag", count: 5) end @@ -53,7 +47,7 @@ class TagListComponentTest < ViewComponent::TestCase context "for a categorized tag list" do should "render" do - render_tag_list(@tags, :categorized) + render_inline(CategorizedTagListComponent.new(tags: @tags)) assert_css(".categorized-tag-list li a.search-tag", count: 5) assert_css(".categorized-tag-list h3", count: 5)