Class: Decidim::Surveys::Metrics::AnswersMetricManage

Inherits:
MetricManage
  • Object
show all
Defined in:
decidim-surveys/app/queries/decidim/surveys/metrics/answers_metric_manage.rb

Instance Method Summary collapse

Methods inherited from MetricManage

for, #initialize, #valid?

Constructor Details

This class inherits a constructor from Decidim::MetricManage

Instance Method Details

#metric_nameObject



7
8
9
# File 'decidim-surveys/app/queries/decidim/surveys/metrics/answers_metric_manage.rb', line 7

def metric_name
  "survey_answers"
end

#saveObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'decidim-surveys/app/queries/decidim/surveys/metrics/answers_metric_manage.rb', line 11

def save
  query.each do |key, results|
    cumulative_value = results[:cumulative]
    next if cumulative_value.zero?

    quantity_value = results[:quantity] || 0
    space_type, space_id, survey_id = key
    record = Decidim::Metric.find_or_initialize_by(day: @day.to_s, metric_type: @metric_name,
                                                   organization: @organization, decidim_category_id: nil,
                                                   participatory_space_type: space_type, participatory_space_id: space_id,
                                                   related_object_type: Decidim::Surveys::Survey.name, related_object_id: survey_id)
    record.assign_attributes(cumulative: cumulative_value, quantity: quantity_value)
    record.save!
  end
end