Skip to content

Commit

Permalink
fix ordering of brainstormings on the landing page (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Feb 14, 2021
1 parent 6a7b1ff commit 9f61454
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
19 changes: 19 additions & 0 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ defmodule Mindwendel.Brainstormings do
alias Mindwendel.Brainstormings.Brainstorming
alias Mindwendel.Brainstormings.Like

@doc """
Returns the 3 most recent brainstormings for a a user.
## Examples
iex> list_brainstormings_for(3)
[%Brainstorming{}, ...]
"""
def list_brainstormings_for(user_id) do
Repo.all(
from brainstorming in Brainstorming,
join: users in assoc(brainstorming, :users),
where: users.id == ^user_id,
order_by: [desc: brainstorming.inserted_at],
limit: 3
)
end

@doc """
Returns the list of ideas.
Expand Down
16 changes: 7 additions & 9 deletions lib/mindwendel_web/templates/static_page/home.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
<div class="row mt-5">
<div class="col-md-12 col-lg-12 mt-2">
<h3>Your latest brainstormings</h3>
<%
sorted_brainstorming =
Enum.sort_by(@current_user.brainstormings, &(&1.inserted_at), {:desc, Time})
|> Enum.slice(0, 3)
%>
<%= for brainstorming <- sorted_brainstorming do %>
<a href="<%= Routes.brainstorming_show_path(@conn, :show, brainstorming) %>">
<h5><%= brainstorming.name %> <span class="badge rounded-pill bg-light text-dark"><%= Timex.format!(brainstorming.inserted_at, "{relative}", :relative) %></span></h5>
</a>
<%= for brainstorming <- list_brainstormings_for(@current_user) do %>
<h5>
<%= link brainstorming.name, to: Routes.brainstorming_show_path(@conn, :show, brainstorming) %>
<span class="badge rounded-pill bg-light text-dark">
<%= Timex.format!(brainstorming.inserted_at, "{relative}", :relative) %>
</span>
</h5>
<% end %>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions lib/mindwendel_web/views/static_page.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
defmodule MindwendelWeb.StaticPageView do
use MindwendelWeb, :view
alias Mindwendel.Brainstormings

def list_brainstormings_for(user) do
Brainstormings.list_brainstormings_for(user.id)
end
end
21 changes: 19 additions & 2 deletions test/mindwendel/brainstormings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@ defmodule Mindwendel.BrainstormingsTest do
alias Mindwendel.Brainstormings

setup do
brainstorming = Factory.insert!(:brainstorming)
user = Factory.insert!(:user)
brainstorming = Factory.insert!(:brainstorming, users: [user])

%{
brainstorming: brainstorming,
idea:
Factory.insert!(:idea, brainstorming: brainstorming, inserted_at: ~N[2021-01-01 15:04:30]),
user: Factory.insert!(:user),
user: user,
like: Factory.insert!(:like, :with_idea_and_user)
}
end

describe "list_brainstormings_for" do
test "returns the 3 most recent brainstormings", %{brainstorming: brainstorming, user: user} do
older_brainstorming =
Factory.insert!(:brainstorming, inserted_at: ~N[2021-01-10 15:04:30], users: [user])

oldest_brainstorming =
Factory.insert!(:brainstorming, inserted_at: ~N[2021-01-05 15:04:30], users: [user])

assert Brainstormings.list_brainstormings_for(user.id) |> Enum.map(fn b -> b.id end) == [
brainstorming.id,
older_brainstorming.id,
oldest_brainstorming.id
]
end
end

describe "change brainstorming" do
test "shortens the brainstorming name if it is too long", %{brainstorming: brainstorming} do
result =
Expand Down
5 changes: 2 additions & 3 deletions test/mindwendel_web/channels/brainstorming_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule MindwendelWeb.BrainstormingChannelTest do

alias Mindwendel.Factory
alias Mindwendel.Brainstormings
alias Mindwendel.Brainstormings.Idea

setup do
%{
Expand All @@ -17,7 +16,7 @@ defmodule MindwendelWeb.BrainstormingChannelTest do

Brainstormings.subscribe(brainstorming.id)
Brainstormings.update_idea(idea, %{body: "lalala"})
assert_received {:idea_updated, idea}
assert_received {:idea_updated, _}
end

test "does not receive messages from other brainstormings", %{brainstorming: brainstorming} do
Expand All @@ -26,7 +25,7 @@ defmodule MindwendelWeb.BrainstormingChannelTest do

Brainstormings.subscribe(other_brainstorming.id)
Brainstormings.update_idea(idea, %{body: "lalala"})
refute_received {:idea_updated, idea}
refute_received {:idea_updated, _}
end
end
end

0 comments on commit 9f61454

Please sign in to comment.