Class: Decidim::MetricManage

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/app/queries/decidim/metric_manage.rb

Overview

This class search for objects related to Metrics, and creates a new registry within his own parameters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(day_string, organization) ⇒ MetricManage

Returns a new instance of MetricManage.

Raises:

  • (ArgumentError)

11
12
13
14
15
16
17
18
# File 'decidim-core/app/queries/decidim/metric_manage.rb', line 11

def initialize(day_string, organization)
  @day = day_string.present? ? Date.parse(day_string) : Time.zone.yesterday
  raise ArgumentError, "[ERROR] Malformed `day` argument. Format must be `YYYY-MM-DD` and in the past" if @day > Time.zone.today

  @day ||= Time.zone.yesterday
  @organization = organization
  @metric_name = metric_name
end

Class Method Details

.for(day_string, organization) ⇒ Object


7
8
9
# File 'decidim-core/app/queries/decidim/metric_manage.rb', line 7

def self.for(day_string, organization)
  new(day_string, organization)
end

Instance Method Details

#metric_nameObject


20
21
22
# File 'decidim-core/app/queries/decidim/metric_manage.rb', line 20

def metric_name
  ""
end

#saveObject


28
29
30
31
32
33
34
35
36
37
# File 'decidim-core/app/queries/decidim/metric_manage.rb', line 28

def save
  return @registry if @registry

  return if cumulative.zero?

  @registry = Decidim::Metric.find_or_initialize_by(day: @day.to_s, metric_type: @metric_name, organization: @organization)
  @registry.assign_attributes(cumulative: cumulative, quantity: quantity)
  @registry.save!
  @registry
end

#valid?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'decidim-core/app/queries/decidim/metric_manage.rb', line 24

def valid?
  @day.present?
end