Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple flash messages on same key #304

Merged
merged 5 commits into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 45 additions & 2 deletions lib/phoenix/controller/flash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ defmodule Phoenix.Controller.Flash do

"""
def put(conn, key, message) do
persist(conn, put_in(get(conn), [key], message))
messages = [message] ++ get_all(conn, key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still be using [head | tail] style composition here :)

persist(conn, put_in(get(conn), [key], messages))
end

@doc """
Expand All @@ -69,7 +70,28 @@ defmodule Phoenix.Controller.Flash do
"""
def get(conn), do: get_session(conn, :phoenix_messages) || %{}
def get(conn, key) do
get_in get(conn), [key]
case get_in get(conn), [key] do
nil -> nil
[message | _messages] -> message
end
end

@doc """
Returns a list of messages from the Flash

## Examples

iex> conn
iex> |> Flash.put(:notice, "Hello")
iex> |> Flash.put(:notice, "Hi!")
iex> |> Flash.get
%{notice: ["hello", "world"]

"""
def get_all(conn), do: get_session(conn, :phoenix_messages) || %{}
def get_all(conn, key) do
messages = get_in(get(conn), [key]) || []
Enum.reverse messages
end

@doc """
Expand All @@ -91,6 +113,27 @@ defmodule Phoenix.Controller.Flash do
{message, conn}
end

@doc """
Removes all messages from the Flash, returning a {messages, conn} pair

## Examples

iex> %Conn{}
iex> |> Flash.put(:notices, "oh noes!")
iex> |> Flash.put(:notice, "false alarm!")
iex> |> Flash.pop_all(:notices) |> elem(0)
["oh noes!", "false alarm!"]

"""
def pop_all(conn, key) do
messages = get_all(conn, key)
if messages do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_all always returns a list, so this expression will always be truthy

conn = persist(conn, Dict.drop(get(conn), [key]))
end

{messages, conn}
end

@doc """
Clears all flash messages
"""
Expand Down
34 changes: 32 additions & 2 deletions test/phoenix/controller/flash_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,39 @@ defmodule Phoenix.Controller.FlashTest do

test "get/1 returns the map of messages" do
conn = conn_with_session |> Flash.put(:notice, "hi")
assert Flash.get(conn) == %{notice: "hi"}
assert Flash.get(conn) == %{notice: ["hi"]}
end

test "get/2 returns the message by key" do
conn = conn_with_session |> Flash.put(:notice, "hi")
assert Flash.get(conn, :notice) == "hi"
end

test "get/2 returns the only the last message put" do
conn = conn_with_session
|> Flash.put(:notice, "hi")
|> Flash.put(:notice, "bye")
assert Flash.get(conn, :notice) == "bye"
end

test "get/2 returns nil for missing key" do
conn = conn_with_session
assert Flash.get(conn, :notice) == nil
end

test "get_all/2 returns a list of messages by key" do
conn = conn_with_session
|> Flash.put(:notices, "hello")
|> Flash.put(:notices, "world")

assert Flash.get_all(conn, :notices) == ["hello", "world"]
end

test "get_all/2 returns [] for missing key" do
conn = conn_with_session
assert Flash.get_all(conn, :notices) == []
end

test "put/3 adds the key/message pair to the flash" do
conn = conn_with_session
|> Flash.put(:error, "oh noes!")
Expand All @@ -96,11 +116,21 @@ defmodule Phoenix.Controller.FlashTest do

{message, conn} = Flash.pop(conn, :error)
assert message == "oh noes!"
assert Flash.get(conn) == %{notice: "false alarm!"}
assert Flash.get(conn) == %{notice: ["false alarm!"]}

{message, conn} = Flash.pop(conn, :notice)
assert message == "false alarm!"
assert Flash.get(conn) == %{}
end

test "pop_all/3 pops all messages from the flash" do
conn = conn_with_session
|> Flash.put(:notices, "oh noes!")
|> Flash.put(:notices, "false alarm!")

{messages, conn} = Flash.pop_all(conn, :notices)
assert messages == ["oh noes!", "false alarm!"]
assert Flash.get(conn) == %{}
end
end