Skip to content

Commit

Permalink
Merge pull request #55 from infinitered/sort_order
Browse files Browse the repository at this point in the history
Add ability to set default sort order
  • Loading branch information
danielberkompas authored May 30, 2017
2 parents fcc057a + 2aac737 commit 06b527a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ npm: {
Run `mix torch.gen (eex|slim)` to generate admin controllers and views for a given Ecto schema module. Torch expects you to have already defined the schema in your project.
Also, Torch expects you to have `phoenix_slime` installed and configured if you generate `slim` templates.

The full format is as follows:

`mix torch.gen (eex|slim) [Admin | term for admin] [Singular
model term] [plural model term] (sort field) (sort_direction)
(attribute:attribute type)`

For example, if we wanted to generate an admin area for a `Post` model we already have using `eex` templates, we could run this command:

```bash
$ mix torch.gen eex Admin Post posts title:string body:text inserted_at:date
$ mix torch.gen eex Admin Post posts inserted_at desc title:string body:text inserted_at:date
```

And the output would be:
Expand Down
17 changes: 16 additions & 1 deletion lib/mix/tasks/torch.gen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,29 @@ defmodule Mix.Tasks.Torch.Gen do
@doc false
def run(args) do
Mix.Task.run("app.start", [])
{_opts, [format, namespace, singular, plural | attrs], _} = OptionParser.parse(args, switches: [])
{_opts,
[format,
namespace,
singular,
plural,
sort_field,
sort_direction
|
attrs
],
_
} = OptionParser.parse(args, switches: [])

namespace_underscore = Macro.underscore(namespace)
binding = Mix.Torch.inflect(namespace, singular)
sort_field = sort_field || "id"
sort_direction = sort_direction || "desc"
path = namespace_underscore <> "/" <> binding[:path]
readonly_attrs = Mix.Torch.readonly_attrs(attrs)
attrs = Mix.Torch.attrs(attrs)
binding = binding ++ [plural: plural,
sort_field: sort_field,
sort_direction: sort_direction,
attrs: attrs,
params: params(attrs),
configs: configs(attrs),
Expand Down
2 changes: 1 addition & 1 deletion lib/torch/templates/pagination/_pagination.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= if @total_pages > 1 do %>
<%= for num <- start_page(@page_number, distance)..end_page(@page_number, @total_pages, distance) do %>
<li>
<a href="?<%= querystring(@conn, page: num) %>" class="<%= if @page_number == num, do: "active", else: "" %>">
<a href="?<%= querystring( @conn, page: num, sort_opts: sort_opts(@conn.assigns) ) %>" class="<%= if @page_number == num, do: "active", else: "" %>">
<%= num %>
</a>
</li>
Expand Down
13 changes: 9 additions & 4 deletions lib/torch/views/pagination_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ defmodule Torch.PaginationView do
prev_link(1, 1)
# => returns nil
"""
def prev_link(conn, current_page, _num_pages) do
def prev_link(conn, current_page, _num_pages, sort_opts \\ nil) do
if current_page != 1 do
link "< Prev", to: "?" <> querystring(conn, page: current_page - 1)
link "< Prev", to: "?" <> querystring(conn, page: current_page - 1, sort_opts: sort_opts)
end
end

Expand All @@ -37,9 +37,9 @@ defmodule Torch.PaginationView do
next_link(2, 2)
# => returns nil
"""
def next_link(conn, current_page, num_pages) do
def next_link(conn, current_page, num_pages, sort_opts \\ nil) do
if current_page != num_pages do
link "Next >", to: "?" <> querystring(conn, page: current_page + 1)
link "Next >", to: "?" <> querystring(conn, page: current_page + 1, sort_opts: sort_opts)
end
end

Expand All @@ -63,4 +63,9 @@ defmodule Torch.PaginationView do
defp end_page(current_page, _total, distance) do
current_page + distance - 1
end

defp sort_opts(%{sort_field: sort_field, sort_direction: sort_direction}) do
%{sort_field: sort_field,
sort_direction: sort_direction}
end
end
6 changes: 4 additions & 2 deletions lib/torch/views/table_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ defmodule Torch.TableView do
original = URI.decode_query(conn.query_string)
opts = %{
"page" => opts[:page],
"sort_field" => opts[:sort_field] || conn.params["sort_field"] || "id",
"sort_direction" => opts[:sort_direction] || conn.params["sort_direction"] || "asc"
"sort_field" => opts[:sort_field] || conn.params["sort_field"] || nil,
"sort_direction" => opts[:sort_direction] || conn.params["sort_direction"] || nil
}

original
|> Map.merge(opts)
|> Enum.filter(fn{_, v} -> v != nil end)
|> Enum.into(%{})
|> URI.encode_query
end

Expand Down
11 changes: 10 additions & 1 deletion priv/templates/elixir/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ defmodule <%= module %>Controller do
@pagination_distance 5

def index(conn, params) do
params =
params
|> Map.put_new("sort_direction", "<%= sort_direction %>")
|> Map.put_new("sort_field", "<%= sort_field %>")

{:ok, sort_direction} = Map.fetch(params, "sort_direction")
{:ok, sort_field} = Map.fetch(params, "sort_field")
{:ok, filter} = Filtrex.parse_params(@filtrex, params["<%= singular %>"] || %{})

page =
Expand All @@ -35,7 +42,9 @@ defmodule <%= module %>Controller do
page_size: page.page_size,
total_pages: page.total_pages,
total_entries: page.total_entries,
distance: @pagination_distance
distance: @pagination_distance,
sort_field: sort_field,
sort_direction: sort_direction
end

def new(conn, _params) do
Expand Down

0 comments on commit 06b527a

Please sign in to comment.