Skip to content

Commit 5cb0f98

Browse files
committed
keep_defaults?
1 parent 5555a69 commit 5cb0f98

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

lib/transformer.ex

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ defmodule AshRandomParams.Transformer do
1515
init_params = Builder.build_action_argument(:init_params, :map, default: %{})
1616
fill = Builder.build_action_argument(:fill, {:array, :atom}, default: [])
1717
unfill = Builder.build_action_argument(:unfill, {:array, :atom}, default: [])
18+
keep_defaults? = Builder.build_action_argument(:keep_defaults?, :boolean, default: true)
1819

1920
dsl_state
2021
|> Builder.add_action(:action, :random_params,
2122
returns: :map,
22-
arguments: [action, init_params, unfill, fill],
23+
arguments: [action, init_params, unfill, fill, keep_defaults?],
2324
run: &__MODULE__.do_random_params/2
2425
)
2526
|> Builder.add_interface(:random_params, args: [:action, {:optional, :init_params}])
@@ -44,15 +45,13 @@ defmodule AshRandomParams.Transformer do
4445
action: action,
4546
init_params: init_params,
4647
unfill: unfill,
47-
fill: fill
48+
fill: fill,
49+
keep_defaults?: keep_defaults?
4850
}
4951
} =
5052
input,
5153
ctx
5254
) do
53-
{random_mod, random_opts} =
54-
random = AshRandomParams.Info.random_params_random!(input.resource)
55-
5655
action = Ash.Resource.Info.action(resource, action)
5756
init_keys = init_params |> Map.keys()
5857

@@ -75,21 +74,41 @@ defmodule AshRandomParams.Transformer do
7574

7675
candidates = accepted_attrs ++ action.arguments
7776

78-
placeholder =
79-
candidates
80-
|> then(fn attr_or_args ->
81-
if action.type == :create do
82-
attr_or_args
83-
else
84-
attr_or_args
85-
|> Enum.reject(&(&1.name in rel_names))
86-
|> Enum.reject(&(&1.name in belongs_to_attrs))
87-
end
88-
end)
89-
|> Map.new(&{&1.name, nil})
77+
defaults =
78+
if keep_defaults? do
79+
candidates
80+
|> then(fn attr_or_args ->
81+
if action.type == :create do
82+
attr_or_args
83+
else
84+
attr_or_args
85+
|> Enum.reject(&(&1.name in rel_names))
86+
|> Enum.reject(&(&1.name in belongs_to_attrs))
87+
end
88+
end)
89+
|> Enum.reject(&(&1.name in unfill))
90+
|> Enum.reject(&(&1.name in init_keys))
91+
|> Map.new(fn %{name: name, default: default} ->
92+
value =
93+
case default do
94+
default when is_function(default, 0) ->
95+
default.()
96+
97+
default ->
98+
default
99+
end
100+
101+
{name, value}
102+
end)
103+
else
104+
%{}
105+
end
90106

91107
generated_params =
92108
(
109+
{random_mod, random_opts} =
110+
random = AshRandomParams.Info.random_params_random!(input.resource)
111+
93112
fields_by_unfill =
94113
candidates
95114
|> Enum.reject(&(&1.name in rel_names))
@@ -118,7 +137,7 @@ defmodule AshRandomParams.Transformer do
118137
end)
119138
)
120139

121-
result_params = placeholder |> Map.merge(init_params) |> Map.merge(generated_params)
140+
result_params = defaults |> Map.merge(init_params) |> Map.merge(generated_params)
122141

123142
{:ok, result_params}
124143
end

test/ash_random_params_test.exs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ defmodule AshRandomParamsTest do
141141
test "with unfill" do
142142
assert %{
143143
author_id: nil,
144-
req_str: nil,
145144
opt_str: nil,
146145
req_int: 777,
147146
opt_int: nil
148147
} =
148+
params =
149149
Post
150150
|> Ash.ActionInput.for_action(
151151
:random_params,
@@ -155,6 +155,29 @@ defmodule AshRandomParamsTest do
155155
}
156156
)
157157
|> Ash.run_action!()
158+
159+
refute params |> Map.has_key?(:req_str)
160+
end
161+
162+
test "with keep_defaults? false" do
163+
assert %{
164+
req_str: "req_str-" <> _,
165+
req_int: 777
166+
} =
167+
params =
168+
Post
169+
|> Ash.ActionInput.for_action(
170+
:random_params,
171+
%{
172+
action: :create,
173+
keep_defaults?: false
174+
}
175+
)
176+
|> Ash.run_action!()
177+
178+
refute params |> Map.has_key?(:opt_str)
179+
refute params |> Map.has_key?(:opt_int)
180+
refute params |> Map.has_key?(:author_id)
158181
end
159182
end
160183

0 commit comments

Comments
 (0)