Skip to content
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
10 changes: 10 additions & 0 deletions lib/kaffy/resource_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ defmodule Kaffy.ResourceAdmin do
Utils.get_assigned_value_or_default(resource, :list_actions, nil, [conn], false)
end

def crud_actions(resource, conn) do
Utils.get_assigned_value_or_default(
resource,
:crud_actions,
[:new, :show, :update, :delete],
[conn],
false
)
end

def widgets(resource, conn) do
Utils.get_assigned_value_or_default(
resource,
Expand Down
10 changes: 6 additions & 4 deletions lib/kaffy/resource_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Kaffy.ResourceForm do
end

permission =
case is_nil(changeset.data.id) do
case is_nil(Map.get(changeset.data, :id)) do
true -> Map.get(options, :create, :editable)
false -> Map.get(options, :update, :editable)
end
Expand Down Expand Up @@ -93,12 +93,14 @@ defmodule Kaffy.ResourceForm do
inputs_for(form, field, fn fp ->
[
{:safe, ~s(<div class="card ml-3" style="padding:15px;">)},
Enum.reduce(embed_fields, [], fn f, all ->
Enum.reduce(embed_fields, [], fn {embed_f, embed_f_options}, all ->
content_tag :div, class: "form-group" do
[
[
form_label(fp, f),
form_field(embed_changeset, fp, {f, options}, class: "form-control")
form_label(fp, embed_f),
form_field(embed_changeset, fp, {embed_f, embed_f_options},
class: "form-control"
)
]
| all
]
Expand Down
3 changes: 2 additions & 1 deletion lib/kaffy/resource_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Kaffy.ResourceSchema do
end

defp reorder_fields(fields_list, schema) do
[_id, first_field | _fields] = schema.__schema__(:fields)
[first_field | _fields] = schema.__schema__(:fields) -- [:id]

# this is a "nice" feature to re-order the default fields to put the specified fields at the top/bottom of the form
fields_list
Expand Down Expand Up @@ -299,6 +299,7 @@ defmodule Kaffy.ResourceSchema do

def field_type(_schema, {_, type}), do: type
def field_type(schema, field), do: schema.__changeset__() |> Map.get(field, :string)

# def field_type(schema, field), do: schema.__schema__(:type, field)

def get_map_fields(schema) do
Expand Down
2 changes: 1 addition & 1 deletion lib/kaffy_web/templates/resource/_table.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>
</td>
<%= for {field, index} <- Enum.with_index(@fields) do %>
<%= if index == 0 do %>
<%= if index == 0 and :update in Kaffy.ResourceAdmin.crud_actions(@my_resource, @conn) do %>
<td><%= link Kaffy.ResourceSchema.kaffy_field_value(@conn, entry, field), to: Kaffy.Utils.router().kaffy_resource_path(@conn, :show, @context, @resource, entry.id) %></td>
<% else %>
<td><%= Kaffy.ResourceSchema.kaffy_field_value(@conn, entry, field) %></td>
Expand Down
2 changes: 2 additions & 0 deletions lib/kaffy_web/templates/resource/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
<div id="checkbox-selected-count" class="checkbox-selected-count float-right"></div>
</h3>
</div>
<%= if :new in Kaffy.ResourceAdmin.crud_actions(@my_resource, @conn) do %>
<div class="col-auto">
<%= link to: Kaffy.Utils.router().kaffy_resource_path(@conn, :new, @context, @resource), class: "btn btn-outline-primary" do %>
<i class="fas fa-plus"></i>
New <%= Kaffy.ResourceAdmin.singular_name(@my_resource) %>
<% end %>
</div>
<% end %>
</div>
</div>

Expand Down