Class: Decidim::ParticipatoryProcesses::ChangeActiveStepJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
decidim-participatory_processes/app/jobs/decidim/participatory_processes/change_active_step_job.rb

Instance Method Summary collapse

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'decidim-participatory_processes/app/jobs/decidim/participatory_processes/change_active_step_job.rb', line 8

def perform
  participatory_processes = Decidim::ParticipatoryProcess.published.where("start_date <= ? AND end_date >= ?", Time.zone.now.to_date, Time.zone.now.to_date)

  participatory_processes.each do |process|
    steps = Decidim::ParticipatoryProcessStep.unscoped
                                             .where(decidim_participatory_process_id: process.id)
                                             .where("start_date <= ? AND end_date >= ?", Time.zone.now, Time.zone.now).order("end_date ASC", :position)

    active_step = process.steps.find_by(active: true)
    if steps.empty? && active_step
      next_position = active_step.position + 1
      next_step = process.steps.where("start_date <= ?", Time.zone.now.to_date).find_by(position: next_position)
      if next_step.present?
        active_step.update(active: false)
        next_step.update(active: true)
      end
    else
      step_to_activate = steps.first
      if active_step != step_to_activate
        active_step&.update(active: false)
        step_to_activate.update(active: true)
      end
    end
  end
end