Class: Bizflow::Lib::ProcessBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bizflow/lib/process_builder.rb

Constant Summary collapse

PID_LENGTH =
9

Instance Method Summary collapse

Instance Method Details

#build(blueprint_id, creator_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bizflow/lib/process_builder.rb', line 11

def build(blueprint_id, creator_id)

  pbp = Bizflow::DataModel::ProcessBlueprint[blueprint_id]
  raise "no process with id '#{blueprint_id}'" unless pbp

  actions_map = {}

  bp = Bizflow::DataModel::ProcessBlueprint.where(id: blueprint_id).eager(:action_blueprints => [:next_action_blueprints]).all.first
  process = bp.add_process(
    name: bp.name,
    creator_id: creator_id,
    description: bp.description,
    pid: (SecureRandom.random_number * 10**PID_LENGTH).to_i)

  bp.action_blueprints.each do |a|

    action = Bizflow::DataModel::Action.create(
      name: a.name,
      type: a.type,
      process: process,
      description: a.description,
      question: a.question,
      action_blueprint_id: a.id)
    
    actions_map[a.name] = action.id

    process.update(start_action_id: action.id) if bp.start == a.name

  end

  bp.action_blueprints.each do |abp|

    abp.next_action_blueprints.each do |nbp|
      acc = Bizflow::DataModel::NextAction.create(
        action_id: actions_map[nbp.action_blueprint.name],
        next_id: nbp.next_blueprint ? actions_map[nbp.next_blueprint.name] : nil,
        ending: nbp.ending
      )

    end

  end

  process.add_head({})

  Bizflow::BusinessModel::Process.wrap(process)
end