Skip to content

add posibility to add image preview in show/edit page #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/kaffy/resource_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/kaffy/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down