Skip to content

Commit fe4ee78

Browse files
adds community trim action and makes community routes public
1 parent ce0aba8 commit fe4ee78

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/reddit/category/community.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ defmodule Reddit.Category.Community do
1515
community
1616
|> cast(attrs, [:name, :summary, :rules])
1717
|> validate_required([:name, :summary, :rules])
18+
|> trim_name()
1819
|> unique_constraint(:name)
1920
end
21+
22+
defp trim_name(changeset) do
23+
case changeset do
24+
%Ecto.Changeset{valid?: true, changes: %{name: name}} ->
25+
put_change(changeset, :name, clean_name(name))
26+
_ ->
27+
changeset
28+
end
29+
end
30+
31+
defp clean_name(name) do
32+
name |> String.split |> Enum.join
33+
end
2034
end

lib/reddit_web/router.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ defmodule RedditWeb.Router do
3232

3333
get "/", PageController, :index
3434
get "/u/:username", UserController, :profile
35+
resources "/comunities", CommunityController, except: [:show]
36+
get "/communities/r/:name", CommunityController, :show
3537
end
3638

3739
scope "/" do
@@ -49,8 +51,6 @@ defmodule RedditWeb.Router do
4951
scope "/", RedditWeb do
5052
pipe_through [:browser, :protected]
5153
# Add your protected routes here
52-
resources "/comunities", CommunityController, except: [:show]
53-
get "/communities/r/:name", CommunityController, :show
5454
end
5555

5656
# Other scopes may use custom stacks.

0 commit comments

Comments
 (0)