From 71bd30e2b88f7b6313e1a6730fc32875a8c62743 Mon Sep 17 00:00:00 2001 From: Emin Kocan Date: Tue, 22 Apr 2025 08:04:41 +0200 Subject: [PATCH] Add profile diaries partial to profile --- .../diary_entries/_profile_diaries.html.erb | 31 +++++++++++++ app/views/users/show.html.erb | 2 + config/locales/en.yml | 5 +++ test/controllers/users_controller_test.rb | 44 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 app/views/diary_entries/_profile_diaries.html.erb diff --git a/app/views/diary_entries/_profile_diaries.html.erb b/app/views/diary_entries/_profile_diaries.html.erb new file mode 100644 index 000000000..a8f97e6c8 --- /dev/null +++ b/app/views/diary_entries/_profile_diaries.html.erb @@ -0,0 +1,31 @@ +<% if diary_entries.present? %> +

<%= t(".latest_diaries") %>

+
+ <% diary_entries.each do |entry| %> +
+
+
+

+ + <%= link_to entry.title, diary_entry_path(@user, entry) %> +

+

<%= truncate(strip_tags(entry.body.to_html), :length => 150) %>

+ +
+ + + <%= link_to diary_entry_path(@user, entry, :anchor => "comments"), :class => "text-body-secondary" do %> + <%= t(".comments", :count => entry.comments.size) %> + <% end %> + + + + <%= l(entry.created_at.to_date, :format => :long) %> + +
+
+
+
+ <% end %> +
+<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 9a04ce50e..56f9aa67b 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -251,6 +251,8 @@ <%= render :partial => "heatmap", :locals => @heatmap_data %> <% end %> +<%= render :partial => "diary_entries/profile_diaries", :locals => { :diary_entries => @user.diary_entries.visible.order(:created_at => :desc).limit(4) } %> + <% if current_user and @user.id == current_user.id %>
<%= link_to t(".edit_profile"), edit_profile_path, :class => "btn btn-outline-primary" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index f6f4aa41c..80417da3f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -650,6 +650,11 @@ en: my_diary: My Diary new: New Diary Entry new_title: Compose a new entry in my user diary + profile_diaries: + latest_diaries: "Latest Diaries" + comments: + one: "%{count} comment" + other: "%{count} comments" diary_comments: new: heading: Add a comment to the following diary entry discussion? diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 1c90cf613..1cb748a9d 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -450,4 +450,48 @@ class UsersControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year" end + + def test_show_profile_diaries + user = create(:user) + create(:language, :code => "en") + create(:diary_entry, :user => user, :title => "First Entry", :body => "First body") + create(:diary_entry, :user => user, :title => "Second Entry", :body => "Second body") + create(:diary_entry, :user => user, :title => "Third Entry", :body => "Third body") + create(:diary_entry, :user => user, :title => "Fourth Entry", :body => "Fourth body") + create(:diary_entry, :user => user, :title => "Fifth Entry", :body => "Fifth body") + + get user_path(user) + assert_response :success + + # Should only show the 4 most recent entries + assert_select ".profile-diary-card", 4 + assert_select ".card-title", "Fifth Entry" + assert_select ".card-title", "Fourth Entry" + assert_select ".card-title", "Third Entry" + assert_select ".card-title", "Second Entry" + assert_select ".card-title", { :text => "First Entry", :count => 0 } + end + + def test_show_profile_diaries_with_comments + user = create(:user) + create(:language, :code => "en") + entry = create(:diary_entry, :user => user, :title => "Entry with Comments") + create(:diary_comment, :diary_entry => entry) + create(:diary_comment, :diary_entry => entry) + + get user_path(user) + assert_response :success + + assert_select ".profile-diary-card" do + assert_select ".card-title", "Entry with Comments" + assert_select "small.text-body-secondary", /2 comments/ + end + end + + def test_show_profile_diaries_empty + user = create(:user) + get user_path(user) + assert_response :success + assert_select ".profile-diary-card", 0 + end end -- 2.39.5