Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,48 @@ def network
# Preload steps + options (and their workflow_option) to avoid extra queries when iterating
steps = workflow.workflow_steps.order(:position).includes(workflow_step_options: :workflow_option)

# Build a hash of category_id => category to avoid N+1 queries
category_ids = steps.filter_map(&:category_id).uniq
categories_by_id = Category.where(id: category_ids).index_by(&:id)

# Build a hash of step_id => step for quick lookups in links
steps_by_id = steps.index_by(&:id)

# Lanes: unique categories in order of step position
lanes = steps.map do |step|
category = Category.find(step.category_id)
category = categories_by_id[step.category_id]
next unless category

{
name: category.name,
link: "/c/#{step.category_id}"
}
end.uniq { |lane| lane[:name] }
end.compact.uniq { |lane| lane[:name] }

# Build lane name to index hash for efficient lookups
lane_index_by_name = lanes.each_with_index.to_h { |lane, idx| [lane[:name], idx] }

# Nodes: one per step
nodes = steps.map do |step|
category_name = Category.find(step.category_id).name
category = categories_by_id[step.category_id]
next unless category

category_name = category.name

{
id: step.name,
lane: lanes.find_index { |lane| lane[:name] == category_name },
lane: lane_index_by_name[category_name],
active: step.id == workflow_state.workflow_step_id
}
end
end.compact

# Links: from each step via its options
links = []

steps.each do |step|
step.workflow_step_options.each do |option|
target_step = DiscourseWorkflow::WorkflowStep.find(option.target_step_id)
target_step = steps_by_id[option.target_step_id]
next unless target_step

links << {
source: step.name,
Expand Down
Loading