diff --git a/lib/kaffy/resource_form.ex b/lib/kaffy/resource_form.ex index 99f623bd..f7c5885c 100644 --- a/lib/kaffy/resource_form.ex +++ b/lib/kaffy/resource_form.ex @@ -61,6 +61,12 @@ defmodule Kaffy.ResourceForm do !is_nil(choices) -> select(form, field, choices, class: "custom-select") + type == :image -> + build_image_preview(changeset.data, options) + + type == :html -> + build_custom_html(changeset.data, options) + true -> build_html_input(changeset.data, form, field, type, opts, permission == :readonly) end @@ -71,6 +77,20 @@ defmodule Kaffy.ResourceForm do build_html_input(changeset.data, form, field, type, opts) end + defp build_image_preview(data, opts) do + url = extract_value(data, opts) + content_tag :img, "", src: url, style: Map.get(opts, :inline_css) + end + + defp build_custom_html(data, opts) do + value = extract_value(data, opts) + {:safe, value} + end + + defp extract_value(data, %{value_fn: fun}) when is_function(fun), do: fun.(data) + defp extract_value(_data, %{value: value}), do: value + defp extract_value(_data, _), do: raise (":value or :value_fn is missing") + defp build_html_input(schema, form, field, type, opts, readonly \\ false) do data = schema {conn, opts} = Keyword.pop(opts, :conn) diff --git a/lib/kaffy/utils.ex b/lib/kaffy/utils.ex index 5542273e..0490edc9 100644 --- a/lib/kaffy/utils.ex +++ b/lib/kaffy/utils.ex @@ -352,7 +352,7 @@ defmodule Kaffy.Utils do end defp convert_to_atom(string) do - if is_binary(string), do: String.to_existing_atom(string), else: string + if is_binary(string), do: String.to_atom(string), else: string end defp setup_resources do diff --git a/mix.exs b/mix.exs index 47cb3fff..5389c052 100644 --- a/mix.exs +++ b/mix.exs @@ -31,7 +31,7 @@ defmodule Kaffy.MixProject do defp deps do [ {:phoenix, "~> 1.4"}, - {:phoenix_html, "~> 2.11"}, + {:phoenix_html, ">= 0.0.0"}, {:mock, "~> 0.3.0", only: :test}, {:ecto, "~> 3.0"}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}