Module: Dunlop::WorkflowInstanceBatchModel::ClassMethods

Defined in:
app/models/dunlop/workflow_instance_batch_model.rb

Instance Method Summary collapse

Instance Method Details

#activeObject



25
26
27
# File 'app/models/dunlop/workflow_instance_batch_model.rb', line 25

def active
  all
end

#for_selectObject



29
30
31
# File 'app/models/dunlop/workflow_instance_batch_model.rb', line 29

def for_select
  active.map{|mb| [mb.presentation_name, mb.id]}
end

#workflow_step_map(user, options = {}) ⇒ Object

return a mapping for each batch containing the state counts for all the workflow step states. This is a performance implementation heavily reducing the time (queries) for large amount of batches



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/dunlop/workflow_instance_batch_model.rb', line 36

def workflow_step_map(user, options = {})
  #NOTE: do not cache this method!
  batch_ids = pluck(:id)
  workflow_instance_class = self.name.sub(/WorkflowInstanceBatch/, 'WorkflowInstance').constantize
  workflow_instance_class.workflow_step_classes.map.with_object(Hash.new{|h, k| h[k] = {}}) do |klass, hash|
    step_counts_scope = klass.joins(:workflow_instance).where(workflow_instances: {workflow_instance_batch_id: batch_ids})
    if options[:archived].present?
      step_counts_scope = step_counts_scope.where.not(workflow_instances: {archived_at: nil})
    else
      step_counts_scope = step_counts_scope.where(workflow_instances: {archived_at: nil})
    end
    step_counts = step_counts_scope.group("#{klass.table_name}.state", 'workflow_instances.workflow_instance_batch_id').count
    step_counts.each do |(state, batch_id), count|
      hash[batch_id][klass] ||= Hash.new{|h, k| h[k] = {}}
      hash[batch_id][klass][state] = count
    end
  end
end