Replace BlockForm with proper BlockView functions

pull/128/head
Luke Imhoff 7 years ago
parent d2bd2cf3c7
commit a34e1a9fff
  1. 6
      apps/explorer_web/lib/explorer_web/controllers/block_controller.ex
  2. 22
      apps/explorer_web/lib/explorer_web/forms/block_form.ex
  3. 4
      apps/explorer_web/lib/explorer_web/templates/block/show.html.eex
  4. 13
      apps/explorer_web/lib/explorer_web/views/block_view.ex
  5. 30
      apps/explorer_web/test/explorer_web/forms/block_form_test.exs

@ -2,7 +2,6 @@ defmodule ExplorerWeb.BlockController do
use ExplorerWeb, :controller
alias Explorer.Chain
alias ExplorerWeb.BlockForm
def index(conn, params) do
blocks =
@ -14,9 +13,8 @@ defmodule ExplorerWeb.BlockController do
def show(conn, %{"id" => number}) do
case Chain.number_to_block(number) do
{:ok, block} ->
block_form = BlockForm.build(block)
render(conn, "show.html", block: block_form)
block_transaction_count = Chain.block_to_transaction_count(block)
render(conn, "show.html", block: block, block_transaction_count: block_transaction_count)
{:error, :not_found} ->
not_found(conn)

@ -1,22 +0,0 @@
defmodule ExplorerWeb.BlockForm do
@moduledoc false
alias Explorer.Chain
def build(block) do
block
|> Map.merge(%{
age: calculate_age(block),
formatted_timestamp: format_timestamp(block),
transactions_count: Chain.block_to_transaction_count(block)
})
end
def calculate_age(block) do
block.timestamp |> Timex.from_now()
end
def format_timestamp(block) do
block.timestamp |> Timex.format!("%b-%d-%Y %H:%M:%S %p %Z", :strftime)
end
end

@ -16,11 +16,11 @@
</div>
<div class="block__item">
<dt class="block__item-key"><%= gettext "Timestamp" %></dt>
<dd class="block__item-value"><%= @block.age %> (<%= @block.formatted_timestamp %>)</dd>
<dd class="block__item-value"><%= age(@block) %> (<%= formatted_timestamp(@block) %>)</dd>
</div>
<div class="block__item">
<dt class="block__item-key"><%= gettext "Transactions" %></dt>
<dd class="block__item-value"><%= gettext "%{count} transactions in this block", count: @block.transactions_count %></dd>
<dd class="block__item-value"><%= gettext "%{count} transactions in this block", count: @block_transaction_count %></dd>
</div>
<div class="block__item">
<dt class="block__item-key"><%= gettext "Hash" %></dt>

@ -1,4 +1,17 @@
defmodule ExplorerWeb.BlockView do
use ExplorerWeb, :view
alias Explorer.Chain.Block
@dialyzer :no_match
# Functions
def age(%Block{timestamp: timestamp}) do
Timex.from_now(timestamp)
end
def formatted_timestamp(%Block{timestamp: timestamp}) do
Timex.format!(timestamp, "%b-%d-%Y %H:%M:%S %p %Z", :strftime)
end
end

@ -1,30 +0,0 @@
defmodule ExplorerWeb.BlockFormTest do
use Explorer.DataCase
alias ExplorerWeb.BlockForm
describe "build/1" do
test "that it has a number" do
block = insert(:block, number: 311)
insert_list(2, :transaction) |> list_with_block(block)
assert BlockForm.build(block).number == 311
end
test "that it returns a count of transactions" do
block = insert(:block, number: 311)
insert_list(2, :transaction) |> list_with_block(block)
assert BlockForm.build(block).transactions_count == 2
end
test "that it returns a block's age" do
block = insert(:block, timestamp: Timex.now() |> Timex.shift(hours: -1))
assert BlockForm.build(block).age == "1 hour ago"
end
test "formats a timestamp" do
date = "Jan-23-2018 10:48:56 AM Etc/UTC"
block = insert(:block, timestamp: Timex.parse!(date, "%b-%d-%Y %H:%M:%S %p %Z", :strftime))
assert BlockForm.build(block).formatted_timestamp == date
end
end
end
Loading…
Cancel
Save