Skip to content

Commit

Permalink
Add option to remove the closing tag and add closing tag by default (#27
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jessedijkstra authored Oct 18, 2017
1 parent 1e8cc65 commit 9a20150
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/ex_cell/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule ExCell.Base do

def data_attribute(name, data, params) when is_list(data) do
data
|> Keyword.merge([cell: name, cell_params: Poison.encode!(params)])
|> Keyword.merge(cell: name, cell_params: Poison.encode!(params))
end

def class_attribute(name, class) do
Expand Down Expand Up @@ -159,6 +159,7 @@ defmodule ExCell.Base do

defp do_container(params, options, content) do
{tag, options} = Keyword.pop(options, :tag, :div)
{closing_tag, options} = Keyword.pop(options, :closing_tag, true)

attributes = attributes(
attribute_name(),
Expand All @@ -167,8 +168,8 @@ defmodule ExCell.Base do
params(params)
)

case content do
nil -> Tag.tag(tag, attributes)
case closing_tag do
false -> Tag.tag(tag, attributes)
_ -> Tag.content_tag(tag, content, attributes)
end
end
Expand Down
16 changes: 11 additions & 5 deletions test/ex_cell/cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ defmodule ExCell.CellTest do
describe "container/3" do
test "defaults to a :div as tag" do
assert safe_to_string(MockCell.container())
== "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\">"
== "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\"></div>"
end

test "overrideables" do
assert safe_to_string(MockCellWithOverridables.container())
== "<div class=\"Bar-MockCellWithOverridables\" data-cell=\"World-MockCellWithOverridables\" data-cell-params=\"{&quot;foo&quot;:&quot;Bar&quot;}\">"
== "<div class=\"Bar-MockCellWithOverridables\" data-cell=\"World-MockCellWithOverridables\" data-cell-params=\"{&quot;foo&quot;:&quot;Bar&quot;}\"></div>"
end

test "custom tag" do
assert safe_to_string(MockCell.container(%{}, tag: :p))
== "<p class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\">"
== "<p class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\"></p>"
end

test "content" do
Expand All @@ -65,18 +65,24 @@ defmodule ExCell.CellTest do
test "cell params" do
assert safe_to_string(
MockCell.container(%{ foo: "bar" })
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{&quot;foo&quot;:&quot;bar&quot;}\">"
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{&quot;foo&quot;:&quot;bar&quot;}\"></div>"
end

test "attributes" do
assert safe_to_string(
MockCell.container(%{}, foo: "bar")
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\" foo=\"bar\">"
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\" foo=\"bar\"></div>"
end

test "data attributes" do
assert safe_to_string(
MockCell.container(%{}, data: [foo: "bar"])
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\" data-foo=\"bar\"></div>"
end

test "no closing tag" do
assert safe_to_string(
MockCell.container(%{}, closing_tag: false, data: [foo: "bar"])
) == "<div class=\"MockCell\" data-cell=\"MockCell\" data-cell-params=\"{}\" data-foo=\"bar\">"
end
end
Expand Down

0 comments on commit 9a20150

Please sign in to comment.