Skip to content
Merged
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
22 changes: 11 additions & 11 deletions lib/live_admin/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ defmodule LiveAdmin.Router do
resource_opts = resource_mod.__live_admin_config__()
levels = [resource_opts, scope_opts, app_opts]

Enum.each(@disabled_with_custom_component, fn {with_key, component_key} ->
disabled? = Enum.any?(levels, &(Keyword.get(&1, with_key) == false))

component_set? =
Enum.any?(levels, &Keyword.has_key?(Keyword.get(&1, :components, []), component_key))

if disabled? and component_set? do
raise ArgumentError,
"invalid config for resource #{inspect(resource_mod)}: " <>
"#{with_key}: false cannot be combined with a custom :#{component_key} component"
end
Enum.each(levels, fn level ->
Enum.each(@disabled_with_custom_component, fn {with_key, component_key} ->
disabled? = Keyword.get(level, with_key) == false
component_set? = Keyword.has_key?(Keyword.get(level, :components, []), component_key)

if disabled? and component_set? do
raise ArgumentError,
"invalid config for resource #{inspect(resource_mod)}: " <>
"#{with_key}: false cannot be combined with a custom :#{component_key} component at the same level"
end
end)
end)
end

Expand Down
37 changes: 31 additions & 6 deletions test/live_admin/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,59 @@ defmodule LiveAdmin.RouterTest do

alias LiveAdmin.Router

describe "create_with: false at resource level with custom :create component at resource level" do
describe "create_with: false and custom :create component at the resource level" do
test "raises ArgumentError" do
assert_raise ArgumentError, ~r/create_with: false.*:create component/, fn ->
Router.__validate_config__!(LiveAdminTest.UserWithCustomCreate, [], [])
end
end
end

describe "create_with: false at scope level with custom :create component at resource level" do
describe "create_with: false and custom :create component at the scope level" do
test "raises ArgumentError" do
assert_raise ArgumentError, ~r/create_with: false.*:create component/, fn ->
Router.__validate_config__!(
LiveAdminTest.UserWithCustomCreateOnly,
[create_with: false],
LiveAdminTest.User,
[create_with: false, components: [create: LiveAdminTest.CustomFormComponent]],
[]
)
end
end
end

describe "update_with: false at app level with custom :edit component at resource level" do
describe "update_with: false and custom :edit component at the app level" do
test "raises ArgumentError" do
assert_raise ArgumentError, ~r/update_with: false.*:edit component/, fn ->
Router.__validate_config__!(LiveAdminTest.UserWithCustomEditOnly, [], update_with: false)
Router.__validate_config__!(
LiveAdminTest.User,
[],
update_with: false,
components: [edit: LiveAdminTest.CustomFormComponent]
)
end
end
end

describe "create_with: false at scope level with custom :create component at resource level" do
test "returns :ok" do
assert :ok ==
Router.__validate_config__!(
LiveAdminTest.UserWithCustomCreateOnly,
[create_with: false],
[]
)
end
end

describe "update_with: false at app level with custom :edit component at resource level" do
test "returns :ok" do
assert :ok ==
Router.__validate_config__!(LiveAdminTest.UserWithCustomEditOnly, [],
update_with: false
)
end
end

describe "resource without conflicting config" do
test "returns :ok" do
assert :ok == Router.__validate_config__!(LiveAdminTest.User, [], [])
Expand Down
Loading