Class: Decidim::ReportingProposals::AssignProposalValuatorsJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



7
8
9
# File 'app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb', line 7

def resource
  @resource
end

Instance Method Details

#categoryObject



46
47
48
# File 'app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb', line 46

def category
  @category ||= resource.category
end

#form(valuator_role) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb', line 31

def form(valuator_role)
  Decidim::Proposals::Admin::ValuationAssignmentForm.from_params(
    valuator_role_ids: valuator_role.id,
    proposal_ids: [resource.id]
  ).with_context(
    current_component: resource.component,
    current_user: resource.organization.users.first # first admin for the traceability
  )
end

#perform(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb', line 9

def perform(data)
  @resource = data[:resource]

  return if valuator_roles.blank?

  unless data[:event_class] == "Decidim::Proposals::Admin::UpdateProposalCategoryEvent"
    return unless data[:extra][:participatory_space]
    return if data[:extra][:type] == "admin"
  end

  valuator_roles.each do |valuator_role|
    Decidim::Proposals::Admin::AssignProposalsToValuator.call(form(valuator_role)) do
      on(:ok) do
        Rails.logger.info("Automatically assigned valuator #{valuator_role.user.name} to proposal ##{resource.id}")
      end
      on(:invalid) do
        Rails.logger.warn("Couldn't automatically assign valuator #{valuator_role.user.name} to proposal ##{resource.id}")
      end
    end
  end
end

#valuator_rolesObject

get valuators from categories



42
43
44
# File 'app/jobs/decidim/reporting_proposals/assign_proposal_valuators_job.rb', line 42

def valuator_roles
  @valuator_roles ||= category.valuator_roles
end