Skip to content

Commit

Permalink
Switch to alt command for creating a new entity if restrictions are set
Browse files Browse the repository at this point in the history
  • Loading branch information
fuelen committed Jan 11, 2024
1 parent d7f571f commit e3a4277
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/seed_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,33 @@ defmodule SeedFactory do
end
else
if trait_names == [] do
command_name = fetch_command_name_by_entity_name!(context, entity_name)
{command_name, traits} =
case restrictions[entity_name] || [] do
[] ->
{fetch_command_name_by_entity_name!(context, entity_name), []}

{add_command_to_requirements(requirements, command_name, required_by, []),
MapSet.put(command_names, command_name)}
restricted_to_trait_names ->
%{by_name: trait_by_name} = fetch_traits!(context, entity_name)

restricted_to_trait_names
|> select_traits_with_dependencies_by_names(trait_by_name, entity_name)
|> Enum.filter(fn trait ->
context
|> fetch_command_by_name!(trait.exec_step.command_name)
|> command_produces?(entity_name)
end)
|> Enum.group_by(& &1.exec_step.command_name)
|> Enum.to_list()
|> case do
[] -> {fetch_command_name_by_entity_name!(context, entity_name), []}
[{command_name, traits}] -> {command_name, traits}
end
end

{
add_command_to_requirements(requirements, command_name, required_by, traits),
MapSet.put(command_names, command_name)
}
else
%{by_name: trait_by_name} = fetch_traits!(context, entity_name)

Expand Down Expand Up @@ -522,6 +545,12 @@ defmodule SeedFactory do
end)
end

defp command_produces?(command, entity_name) do
Enum.any?(command.producing_instructions, fn instruction ->
instruction.entity == entity_name
end)
end

defp fetch_command_by_name!(context, command_name) do
case Map.fetch(context.__seed_factory_meta__.commands, command_name) do
{:ok, command} -> command
Expand Down

0 comments on commit e3a4277

Please sign in to comment.