diff --git a/spec/apps/blogging/handlers/home_handler_spec.cr b/spec/apps/blogging/handlers/home_handler_spec.cr index 53a4a8a..eddb25a 100644 --- a/spec/apps/blogging/handlers/home_handler_spec.cr +++ b/spec/apps/blogging/handlers/home_handler_spec.cr @@ -479,6 +479,130 @@ describe Blogging::HomeHandler do article.author.should eq other_user.profile! end end + + it "inserts the first page of tag-specific articles when a tag is targeted" do + user = create_user(username: "test1", email: "test1@example.com", password: "insecure") + other_user = create_user(username: "test2", email: "test2@example.com", password: "insecure") + + tag = Blogging::Tag.create!(label: "test") + other_tag = Blogging::Tag.create!(label: "other") + + 15.times do |i| + article = Blogging::Article.create!( + title: "Article #{i}", + slug: "article-#{i}", + description: "My article description", + body: "# Hello World", + author: other_user.profile!, + ) + + article.tags.add(tag) + end + + 15.times do |i| + article = Blogging::Article.create!( + title: "Other article #{i}", + slug: "article-#{i}", + description: "My article description", + body: "# Hello World", + author: other_user.profile!, + ) + + article.tags.add(other_tag) + end + + request = Marten::HTTP::Request.new(method: "GET", resource: "/test/xyz?tag=test") + request.session = Marten::HTTP::Session::Store::Cookie.new("sessionkey") + MartenAuth.sign_in(request, user) + + handler = Blogging::HomeHandler.new(request, Marten::Routing::MatchParameters.new) + + handler.render_to_response + + handler.context["current_tab"].should eq "tag" + + page = handler.context["articles"].raw.as(Marten::DB::Query::Page(Blogging::Article)) + page.size.should eq 10 + page.each do |article| + article.title!.starts_with?("Article").should be_true + end + end + + it "inserts the right page of tag-specific articles when a tag is targeted and a page is set" do + user = create_user(username: "test1", email: "test1@example.com", password: "insecure") + other_user = create_user(username: "test2", email: "test2@example.com", password: "insecure") + + tag = Blogging::Tag.create!(label: "test") + other_tag = Blogging::Tag.create!(label: "other") + + 15.times do |i| + article = Blogging::Article.create!( + title: "Article #{i}", + slug: "article-#{i}", + description: "My article description", + body: "# Hello World", + author: other_user.profile!, + ) + + article.tags.add(tag) + end + + 15.times do |i| + article = Blogging::Article.create!( + title: "Other article #{i}", + slug: "article-#{i}", + description: "My article description", + body: "# Hello World", + author: other_user.profile!, + ) + + article.tags.add(other_tag) + end + + request = Marten::HTTP::Request.new(method: "GET", resource: "/test/xyz?tag=test&page=2") + request.session = Marten::HTTP::Session::Store::Cookie.new("sessionkey") + MartenAuth.sign_in(request, user) + + handler = Blogging::HomeHandler.new(request, Marten::Routing::MatchParameters.new) + + handler.render_to_response + + handler.context["current_tab"].should eq "tag" + + page = handler.context["articles"].raw.as(Marten::DB::Query::Page(Blogging::Article)) + page.size.should eq 5 + page.each do |article| + article.title!.starts_with?("Article").should be_true + end + end + + it "inserts the expected popular tags" do + user = create_user(username: "test1", email: "test1@example.com", password: "insecure") + other_user = create_user(username: "test2", email: "test2@example.com", password: "insecure") + + 25.times do |i| + article = Blogging::Article.create!( + title: "Article #{i}", + slug: "article-#{i}", + description: "My article description", + body: "# Hello World", + author: other_user.profile!, + ) + + tag = Blogging::Tag.create!(label: "tag-#{i}") + article.tags.add(tag) + end + + request = Marten::HTTP::Request.new(method: "GET", resource: "/test/xyz?articles=global") + request.session = Marten::HTTP::Session::Store::Cookie.new("sessionkey") + MartenAuth.sign_in(request, user) + + handler = Blogging::HomeHandler.new(request, Marten::Routing::MatchParameters.new) + + handler.render_to_response + + handler.context["tags"].to_a.should eq Blogging::Tag.all[..20].to_a + end end end end diff --git a/src/apps/blogging/handlers/home_handler.cr b/src/apps/blogging/handlers/home_handler.cr index ec6edde..27dea34 100644 --- a/src/apps/blogging/handlers/home_handler.cr +++ b/src/apps/blogging/handlers/home_handler.cr @@ -9,12 +9,18 @@ module Blogging before_render :add_user_data_to_context before_render :add_articles_to_context + before_render :add_popular_tags_to_context + before_render :add_targeted_tag_to_context private PAGE_PARAM = "page" private PAGE_SIZE = 10 + private TAGS_COUNT = 20 private def add_articles_to_context - if request.query_params.fetch(:articles, "user") == "user" && following_users? + if !targeted_tag.nil? + context[:current_tab] = "tag" + context[:articles] = paginated_tag_feed_articles + elsif request.query_params.fetch(:articles, "user") == "user" && following_users? context[:current_tab] = "user" context[:articles] = paginated_user_feed_articles else @@ -23,6 +29,14 @@ module Blogging end end + private def add_popular_tags_to_context + context[:tags] = Tag.all[..TAGS_COUNT] + end + + private def add_targeted_tag_to_context + context[:targeted_tag] = targeted_tag + end + private def add_user_data_to_context context[:following_users] = following_users? end @@ -42,10 +56,19 @@ module Blogging paginator.page(page_number) end + private def paginated_tag_feed_articles + paginator = Article.filter(tags__label: targeted_tag).order("-created_at").paginator(PAGE_SIZE) + paginator.page(page_number) + end + private def paginated_user_feed_articles followed_user_pks = request.user!.profile!.followed_users.pluck(:pk).flatten paginator = Article.filter(author_id__in: followed_user_pks).order("-created_at").paginator(PAGE_SIZE) paginator.page(page_number) end + + private def targeted_tag + request.query_params[:tag]? + end end end diff --git a/src/apps/blogging/templates/blogging/article_create_update.html b/src/apps/blogging/templates/blogging/article_create_update.html index 2b6a973..54235aa 100644 --- a/src/apps/blogging/templates/blogging/article_create_update.html +++ b/src/apps/blogging/templates/blogging/article_create_update.html @@ -58,7 +58,7 @@ name="{{ schema.tags.id }}" value="{{ schema.tags.value }}" /> - +
{% for error in schema.tags.errors %}
{{ error.message }}
{% endfor %} diff --git a/src/apps/blogging/templates/blogging/home.html b/src/apps/blogging/templates/blogging/home.html index ce431e0..978222b 100644 --- a/src/apps/blogging/templates/blogging/home.html +++ b/src/apps/blogging/templates/blogging/home.html @@ -24,6 +24,11 @@

conduit

+ {% if targeted_tag %} + + {% endif %} @@ -36,22 +41,18 @@

conduit

{% include "partials/article_feed.html" with additional_page_params = articles_query_param %} + {% if not tags.empty? %}
+ {% endif %}