Skip to content
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
4 changes: 2 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PostsController < ApplicationController
def index
@user = User.find(params[:user_id])

posts_scope = @user.posts.eager_load(:comments)
posts_scope = @user.posts.preload(:comments)
posts_scope = posts_scope.published unless Current.user == @user

# Filter by tag if specified
Expand All @@ -13,7 +13,7 @@ def index
end

@page = Page.new(params: params, relation: posts_scope)
@posts = posts_scope.order(created_at: :desc).limit(@page.per_page).offset(@page.offset)
@posts = posts_scope.order(created_at: :desc).limit(@page.per_page).offset(@page.offset).preload(:tags, comments: :user)
@all_user_tags = Tag.joins(:posts).where(posts: { user: @user }).distinct
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index

if @tag
@page = Page.new(params: params, relation: @tag.posts.published)
@posts = @tag.posts.published.order(view_count: :desc).limit(5).offset(@page.offset)
@posts = @tag.posts.published.order(view_count: :desc).limit(5).offset(@page.offset).preload(comments: :user)
else
@page = Page.new(params: params, relation: Post.none)
@posts = Post.none
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class WelcomeController < ApplicationController

def index
@page = Page.new(params: params, relation: Post)
@posts = Post.published.order(view_count: :desc).limit(5).offset(@page.offset)
@posts = Post.published.order(view_count: :desc).limit(5).offset(@page.offset).preload(:tags, comments: :user)
@popular_tags = Tag.joins(:posts)
.where(posts: { status: :published })
.group("tags.id")
Expand Down
4 changes: 2 additions & 2 deletions app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<% else%>
<p><%= simple_format(post.body) %></p>
<% end%>
<% if post.tags.any? %>

<% if post.tags.present? %>
<div style="margin-top: 10px;">
<% post.tags.each do |tag| %>
<span style="background-color: #007bff; color: white; padding: 3px 8px; border-radius: 12px; font-size: 0.8em; margin-right: 5px; display: inline-block;"><%= tag.name %></span>
Expand Down
8 changes: 3 additions & 5 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<section>
<label for="tag_filter">Filter by tag:</label>
<%= form_with url: user_posts_path(@user), method: :get, local: true do |form| %>
<%= form.select :tag_id,
options_from_collection_for_select(@all_user_tags, :id, :name, params[:tag_id]),
<%= form.select :tag_id,
options_from_collection_for_select(@all_user_tags, :id, :name, params[:tag_id]),
{ prompt: "All posts" },
{ onchange: "this.form.submit();" } %>
<% end %>
Expand All @@ -30,7 +30,7 @@
<%= render partial: post, locals: { short: true } %>
<footer>
<details>
<summary>Comments (<%= post.comments.count %>)</summary>
<summary>Comments (<%= post.comments.size %>)</summary>
<%= render post.comments %>
</details>
</footer>
Expand All @@ -39,5 +39,3 @@

<%= render 'posts/paging', page: @page %>
</div>


4 changes: 2 additions & 2 deletions app/views/searches/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<%= render partial: post, locals: { short: true } %>
<footer>
<details>
<summary>Comments (<%= post.comments.count %>)</summary>
<summary>Comments (<%= post.comments.size %>)</summary>
<%= render post.comments %>
</details>
</footer>
</article>
<% end %>

<%= render 'posts/paging', page: @page, additional_params: { tag: params[:tag] } %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<%= render partial: post, locals: { short: true } %>
<footer>
<details>
<summary>Comments (<%= post.comments.count %>)</summary>
<summary>Comments (<%= post.comments.size %>)</summary>
<%= render post.comments %>
</details>
</footer>
Expand Down