Skip to content

Commit 749c628

Browse files
Changes total comments query and partials names
Now instead of making a query to get total comments, comments are preloaded on the stories and display with length. Changes plural partials names to singular.
1 parent c265fd1 commit 749c628

File tree

12 files changed

+18
-33
lines changed

12 files changed

+18
-33
lines changed

web/controllers/story_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Fmylife.StoryController do
1111
{stories, kerosene} =
1212
Story
1313
|> order_by(desc: :id)
14-
|> preload(:user)
14+
|> preload([:user, :comments])
1515
|> Repo.paginate(params)
1616

1717
render(conn, :index,

web/models/comment.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,4 @@ defmodule Fmylife.Comment do
1818
|> validate_required([:body])
1919
end
2020

21-
def count(story_id) do
22-
hd Fmylife.Repo.all(
23-
from c in Fmylife.Comment,
24-
select: count(c.id),
25-
where: [story_id: ^story_id]
26-
)
27-
end
2821
end

web/models/story.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Fmylife.Story do
3030
group_by: s.id,
3131
select: s,
3232
limit: 10,
33-
preload: [:user]
33+
preload: [:user, :comments]
3434
)
3535
end
3636

@@ -39,22 +39,22 @@ defmodule Fmylife.Story do
3939
from s in Story,
4040
order_by: [desc: fragment("Random()")],
4141
limit: 10,
42-
preload: [:user]
42+
preload: [:user, :comments]
4343
)
4444
end
4545

4646
def stories_of_category(category_id, params) do
4747
Story
4848
|> order_by(desc: :id)
4949
|> where(category_id: ^category_id)
50-
|> preload(:user)
50+
|> preload([:user, :comments])
5151
|> Repo.paginate(params)
5252
end
5353

5454
def search(search_params, params) do
5555
from(s in Story,
5656
where: ilike(s.body, ^"%#{search_params}%"),
57-
preload: [:user])
57+
preload: [:user, :comments])
5858
|> Repo.paginate(params)
5959
end
6060

web/templates/layout/_header.html.eex

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@
3636
</li>
3737
<% end %>
3838
</ul>
39-
<!-- <form class="navbar-form navbar-left" role="search">
40-
<div class="input-group">
41-
<input type="text" class="form-control" placeholder="Search for...">
42-
<span class="input-group-btn">
43-
<button class="btn btn-default" type="button">Search</button>
44-
</span>
45-
</div>
46-
</form> -->
4739
<%= render Fmylife.StoryView, "_search_form.html", conn: @conn %>
4840
<ul class="nav navbar-nav navbar-right">
4941
<%= if @current_user do %>

web/templates/story/_stories.html.eex renamed to web/templates/story/_story.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<span id="total-dislikes-<%= @story.id %>" class="text-warning">
2121
(<%= Fmylife.Like.total_dislikes(@story.id) %>)
2222
</span>
23-
<%= link "Comments", to: story_path(@conn, :show, @story), class: "btn btn-link btn-xs" %></a><small><em> (<%= Fmylife.Comment.count(@story.id) %>)</em></small>
23+
<%= link "Comments", to: story_path(@conn, :show, @story), class: "btn btn-link btn-xs" %><small><em>(<%= length(@story.comments) %>)</em></small>
2424
<div class="text-muted pull-right"><small>Created by: <%= @story.user.name %>&nbsp;<%= time_ago_in_words(@story.inserted_at) %></small></div>
2525
</div><!-- /.well -->

web/templates/story/categories.html.eex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div class="container">
22
<div class="row">
33
<div class="col-md-8">
4-
<%= render_many @stories, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
4+
<%= render_many @stories, StoryView, "_story.html", conn: @conn, current_user: @current_user %>
55

66
<%= paginate @conn, @kerosene, class: "pull-right" %>
77
</div><!-- /.col-md-8 -->
88
<div class="col-md-4 col-sm-12">
99
<div class="panel panel-default">
1010
<div class="panel-heading text-center">Categories</div>
1111
<ul class="list-group">
12-
<%= render_many @categories, StoryView, "_categories.html", as: :category, conn: @conn %>
12+
<%= render_many @categories, StoryView, "_category.html", as: :category, conn: @conn %>
1313
</ul>
1414
</div><!-- /.panel panel-default -->
1515
</div><!-- /.col-md-4 col-sm-12 -->

web/templates/story/index.html.eex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div class="container">
22
<div class="row">
33
<div class="col-md-8">
4-
<%= render_many @stories, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
4+
<%= render_many @stories, StoryView, "_story.html", conn: @conn, current_user: @current_user %>
55

66
<%= paginate @conn, @kerosene, class: "pull-right" %>
77
</div><!-- /.col-md-8 -->
88
<div class="col-md-4 col-sm-12">
99
<div class="panel panel-default">
1010
<div class="panel-heading text-center">Categories</div>
1111
<ul class="list-group">
12-
<%= render_many @categories, StoryView, "_categories.html", as: :category, conn: @conn %>
12+
<%= render_many @categories, StoryView, "_category.html", as: :category, conn: @conn %>
1313
</ul>
1414
</div><!-- /.panel panel-default -->
1515
</div><!-- /.col-md-4 col-sm-12 -->

web/templates/story/random.html.eex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div class="container">
22
<div class="row">
33
<div class="col-md-8">
4-
<%= render_many @stories, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
4+
<%= render_many @stories, StoryView, "_story.html", conn: @conn, current_user: @current_user %>
55
</div><!-- /.col-md-8 -->
66
<div class="col-md-4 col-sm-12">
77
<div class="panel panel-default">
88
<div class="panel-heading text-center">Categories</div>
99
<ul class="list-group">
10-
<%= render_many @categories, StoryView, "_categories.html", as: :category, conn: @conn %>
10+
<%= render_many @categories, StoryView, "_category.html", as: :category, conn: @conn %>
1111
</ul>
1212
</div><!-- /.panel panel-default -->
1313
</div><!-- /.col-md-4 col-sm-12 -->

web/templates/story/search.html.eex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<div class="row">
33
<div class="col-md-8">
44
<%= if @stories == [] do %>
5-
<%= render_many @top_stories, StoryView, "_stories.html", as: :story, conn: @conn, current_user: @current_user %>
5+
<%= render_many @top_stories, StoryView, "_story.html", as: :story, conn: @conn, current_user: @current_user %>
66
<% else %>
7-
<%= render_many @stories, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
7+
<%= render_many @stories, StoryView, "_story.html", conn: @conn, current_user: @current_user %>
88
<% end %>
99

1010
<%= paginate @conn, @kerosene, class: "pull-right" %>
@@ -13,7 +13,7 @@
1313
<div class="panel panel-default">
1414
<div class="panel-heading text-center">Categories</div>
1515
<ul class="list-group">
16-
<%= render_many @categories, StoryView, "_categories.html", as: :category, conn: @conn %>
16+
<%= render_many @categories, StoryView, "_category.html", as: :category, conn: @conn %>
1717
</ul>
1818
</div><!-- /.panel panel-default -->
1919
</div><!-- /.col-md-4 col-sm-12 -->

0 commit comments

Comments
 (0)