parent
d2bd2cf3c7
commit
a34e1a9fff
@ -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 |
@ -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…
Reference in new issue