Skip to content

Commit

Permalink
Add deleted category and author fields back to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
zberkom committed Nov 10, 2016
1 parent 262f00a commit 57f6bd8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
25 changes: 24 additions & 1 deletion apps/example/web/controllers/admin/post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ defmodule Example.Admin.PostController do

plug :put_layout, {Example.LayoutView, "admin.html"}
plug :scrub_params, "post" when action in [:create, :update]
plug :assign_authors

plug :assign_categories

@filtrex [
%Config{type: :boolean, keys: ~w(draft)},
%Config{type: :date, keys: ~w(inserted_at), options: %{format: "{YYYY}-{0M}-{0D}"}},
%Config{type: :text, keys: ~w(title body)}
%Config{type: :text, keys: ~w(title body)},
%Config{type: :number, keys: ~w(author_id)},
%Config{type: :number, keys: ~w(category_id)}
]

@pagination [page_size: 10]
Expand Down Expand Up @@ -85,4 +92,20 @@ defmodule Example.Admin.PostController do
|> put_flash(:info, "Post deleted successfully.")
|> redirect(to: admin_post_path(conn, :index))
end

defp assign_authors(conn, _opts) do
authors =
Example.Author
|> Repo.all
|> Enum.map(&({&1.author, &1.id}))
assign(conn, :authors, authors)
end

defp assign_categories(conn, _opts) do
categories =
Example.Category
|> Repo.all
|> Enum.map(&({&1.category, &1.id}))
assign(conn, :categories, categories)
end
end
15 changes: 15 additions & 0 deletions apps/example/web/templates/admin/post/_filters.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<h3>Search by Filters</h3>
<%= form_tag @conn.request_path, method: :get, id: "filters" do %>

<div class="field">
<label>Author</label>
<%= filter_assoc_select(:post, :author_id, @authors, @conn.params) %>
</div>

<div class="field">
<label>Category</label>
<%= filter_assoc_select(:post, :category_id, @categories, @conn.params) %>
</div>

<div class="field">
<label>Title</label>
<%= filter_select(:post, :title, @conn.params) %>
Expand All @@ -14,6 +24,11 @@
<%= filter_text_input(:post, :body, @conn.params) %>
</div>

<div class="field">
<label>Draft</label>
<%= filter_boolean_input(:post, :draft, @conn.params) %>
</div>

<div class="field">
<label>Inserted at</label>
<%= filter_date_input(:post, :inserted_at, @conn.params) %>
Expand Down
18 changes: 18 additions & 0 deletions apps/example/web/templates/admin/post/_form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
<%= error_tag f, :body %>
</div>

<div class="field">
<%= label f, :author %>
<%= select f, :author_id, @authors, prompt: "Choose one" %>
<%= error_tag f, :author_id %>
</div>

<div class="field">
<%= label f, :category %>
<%= select f, :category_id, @categories, prompt: "Choose one" %>
<%= error_tag f, :category_id %>
</div>

<div class="field">
<%= label f, :draft %>
<%= select f, :draft, [{"True", true}, {"False", false}] %>
<%= error_tag f, :draft %>
</div>

<div class="field">
<%= submit "Submit", class: "torch-button" %>
</div>
Expand Down
6 changes: 6 additions & 0 deletions apps/example/web/templates/admin/post/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<tr>
<th><%= table_link(@conn, "Title", :title) %></th>
<th><%= table_link(@conn, "Body", :body) %></th>
<th><%= table_link(@conn, "Author", :author_id) %></th>
<th><%= table_link(@conn, "Category", :category_id) %></th>
<th><%= table_link(@conn, "Draft", :draft) %></th>

<th><span>Actions<span></th>
</tr>
Expand All @@ -22,6 +25,9 @@
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= post.draft %></td>
<td><%= table_assoc_display_name(post, :author_id, @authors) %>
<td><%= table_assoc_display_name(post, :category_id, @categories) %>

<td class="torch-actions">
<%= link "Edit", to: admin_post_path(@conn, :edit, post) %>
Expand Down

0 comments on commit 57f6bd8

Please sign in to comment.