Skip to content

Commit

Permalink
change the web layer according to previous refactoring add template t…
Browse files Browse the repository at this point in the history
…o render the chunk
  • Loading branch information
RudolfMan committed Jun 10, 2020
1 parent ca61d2b commit 4d378f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
33 changes: 20 additions & 13 deletions lib/diff_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ defmodule DiffWeb.PageController do
end
end

def expand_context(conn, %{"file_name" => file_name} = params) do
%{
"version" => version,
"package" => package,
"from_line" => from_line,
"direction" => direction
} = params

case parse_version(version) do
def expand_context(conn, %{"file_name" => _file_name} = params) do
case parse_version(params["version"]) do
{:ok, version} ->
version = to_string(version)
do_expand_context(conn, package, version, file_name, from_line, direction)
params = Map.update!(params, "version", fn _ -> version end)

do_expand_context(conn, params)

:error ->
conn
Expand Down Expand Up @@ -96,10 +91,22 @@ defmodule DiffWeb.PageController do
end
end

defp do_expand_context(conn, package, version, file_name, from_line, direction) do
case Diff.Hex.get_chunk(package, version, file_name, from_line, direction) do
defp do_expand_context(conn, params) do
chunk_extractor_params =
for {key, val} <- params, into: %{} do
{String.to_existing_atom(key), val}
end

case Diff.Hex.get_chunk(chunk_extractor_params) do
{:ok, chunk} ->
json(conn, %{chunk: chunk})
rendered_chunk =
Phoenix.View.render_to_string(DiffWeb.RenderView, "render_context_chunk.html",
chunk: chunk
)

conn
|> put_status(200)
|> json(%{chunk: rendered_chunk, lines: length(chunk)})

{:error, %{errors: errors}} ->
conn
Expand Down
6 changes: 5 additions & 1 deletion lib/diff_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ defmodule DiffWeb.Router do
pipe_through :browser

live "/", SearchLiveView
get "/diff/:package/:version/expand/:from_line/:direction", PageController, :expand_context

get "/diff/:package/:version/expand/:from_line/:to_line/:right_line/",
PageController,
:expand_context

get "/diff/:package/:versions", PageController, :diff
get "/diffs", PageController, :diffs
end
Expand Down
15 changes: 15 additions & 0 deletions lib/diff_web/templates/render/render_context_chunk.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= for line <- @chunk do %>
<tr class="ghd-line ghd-line-type-context expanded">
<td class="ghd-line-number">
<div class="ghd-line-number-from">
<%= line_number(line.from_line_number) %>
</div>
<div class="ghd-line-number-to">
<%= line_number(line.to_line_number) %>
</div>
</td>
<td class="ghd-text">
<div class="ghd-text-internal"><%= line_text(line.text) %></div>
</td>
</tr>
<% end %>
24 changes: 6 additions & 18 deletions test/diff_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,23 @@ defmodule DiffWeb.PageControllerTest do
end
end

describe "/GET /diff/:package/:versions/expand/:from_line/:direction" do
describe "/GET /diff/:package/:versions/expand/:from/:to/:right_line" do
setup :verify_on_exit!

test "requires file_name query param", %{conn: conn} do
conn = get(conn, "/diff/phoenix/1.4.5/expand/1/down")
conn = get(conn, "/diff/phoenix/1.4.5/expand/1/1/1")
assert %{"error" => "missing query parameter: file_name"} = json_response(conn, 400)
end

test "doesn't accept direction other that up or down", %{conn: conn} do
conn = get(conn, "/diff/phoenix/1.4.5/expand/1/left?file_name=mix.exs")
assert %{"errors" => %{"direction" => error}} = json_response(conn, 400)
assert error =~ ~r/direction must be either \"up\" or \"down\"/
end

test "doesn't accept negative from_line", %{conn: conn} do
conn = get(conn, "/diff/phoenix/1.4.5/expand/-2/up?file_name=mix.exs")
test "doesn't accept when to_line is less than from_line", %{conn: conn} do
conn = get(conn, "/diff/phoenix/1.4.5/expand/2/1/1?file_name=mix.exs")
assert json_response(conn, 400)
end

test "returns chunk of the file", %{conn: conn} do
conn = get(conn, "/diff/phoenix/1.4.5/expand/2/up?file_name=mix.exs")
conn = get(conn, "/diff/phoenix/1.4.5/expand/1/4/1?file_name=mix.exs")
assert %{"chunk" => chunk} = json_response(conn, 200)

assert [
%{"line_text" => "defmodule Phoenix.MixProject do"},
%{"line_text" => " use Mix.Project"}
] = chunk

assert 2 = length(chunk)
assert chunk =~ "defmodule Phoenix.MixProject do"
end
end
end

0 comments on commit 4d378f6

Please sign in to comment.