Class: Workflower::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/workflower/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calling_model, source) ⇒ Manager

Returns a new instance of Manager.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/workflower/manager.rb', line 7

def initialize(calling_model, source)
  @transitions = source.get_workflows[calling_model.workflow_id.to_s.to_sym]
  @current_state = calling_model.send(calling_model.workflower_state_column_name)
  @current_sequence = calling_model.send(:sequence)
  @calling_model    = calling_model
  @source           = source

  # Flows
  @flows_container = buildup_flows
  @events = @flows_container.map(&:event)
  @allowed_events = allowed_transitions.map(&:event)
  @validation_errors = []
end

Instance Attribute Details

#allowed_eventsObject (readonly)

Returns the value of attribute allowed_events.



5
6
7
# File 'lib/workflower/manager.rb', line 5

def allowed_events
  @allowed_events
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/workflower/manager.rb', line 5

def events
  @events
end

#flows_containerObject (readonly)

Returns the value of attribute flows_container.



5
6
7
# File 'lib/workflower/manager.rb', line 5

def flows_container
  @flows_container
end

Instance Method Details

#allowed_transitionsObject



49
50
51
# File 'lib/workflower/manager.rb', line 49

def allowed_transitions
  buildup_flows.select { |flow| transition_possible?(flow) }
end

#buildup_flowsObject



35
36
37
# File 'lib/workflower/manager.rb', line 35

def buildup_flows
  possible_transitions.map { |transition| Workflower::Flow.new(transition) }
end

#possible_transitionsObject



39
40
41
42
43
44
45
46
47
# File 'lib/workflower/manager.rb', line 39

def possible_transitions
  # @transitions.where(state: @current_state).where("sequence = :seq OR sequence = :seq_plus", seq: @current_sequence, seq_plus: @current_sequence + 1).order("sequence ASC") || []
  @transitions.select do |item|
    item[:state] == @current_state && (item[:sequence] == @current_sequence || item[:sequence] == @current_sequence + 1)
  end
              .sort_by do |item|
    item[:sequence]
  end
end

#process_transition!(flow) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/workflower/manager.rb', line 57

def process_transition!(flow)
  if flow.condition_is_met?(@calling_model)
    begin
      flow.call_before_transition(@calling_model)
      @calling_model.assign_attributes flow.updateable_attributes(@calling_model)
      flow.call_after_transition(@calling_model)
      true
    rescue Exception
      @calling_model.errors.add(@calling_model.workflower_state_column_name, :transition_faild)
      false
    end
  else
    @calling_model.errors.add(@calling_model.workflower_state_column_name,
                              :precondition_not_met_to_process_transition)
  end
end

#set_initial_stateObject



53
54
55
# File 'lib/workflower/manager.rb', line 53

def set_initial_state
  "saved"
end

#transition_possible?(flow) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/workflower/manager.rb', line 74

def transition_possible?(flow)
  @calling_model.send(@calling_model.workflower_state_column_name) != flow.transition_into && flow.condition_is_met?(@calling_model)
end

#uninitializeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/workflower/manager.rb', line 21

def uninitialize
  @transitions = []
  @current_state = []
  @current_sequence = []
  @calling_model    = []
  @source           = []

  # Flows
  @flows_container = []
  @events = []
  @allowed_events = []
  @validation_errors = []
end