Module: Motor::Alerts::Persistance

Defined in:
lib/motor/alerts/persistance.rb

Constant Summary collapse

ALERT_ATTRIBUTES =
%i[
  query_id
  name
  description
  preferences
  is_enabled
  to_emails
].freeze
NameAlreadyExists =
Class.new(StandardError)
InvalidInterval =
Class.new(StandardError)
NORMALIZE_INTERVAL_REGEXP =
/\A(?:every\s+)?/i.freeze

Class Method Summary collapse

Class Method Details

.archive_with_existing_name(alert) ⇒ Object


79
80
81
82
# File 'lib/motor/alerts/persistance.rb', line 79

def archive_with_existing_name(alert)
  Motor::Alert.where(['name = ? AND id != ?', alert.name, alert.id])
              .update_all(deleted_at: Time.current)
end

.assign_attributes(alert, params) ⇒ Object


71
72
73
74
75
76
77
# File 'lib/motor/alerts/persistance.rb', line 71

def assign_attributes(alert, params)
  alert.assign_attributes(params.slice(*ALERT_ATTRIBUTES))
  alert.preferences[:interval] = normalize_interval(alert.preferences[:interval])
  alert.updated_at = [params[:updated_at], Time.current].min if params[:updated_at].present?

  Motor::Tags.assign_tags(alert, params[:tags])
end

.build_from_params(params, current_user = nil) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/motor/alerts/persistance.rb', line 22

def build_from_params(params, current_user = nil)
  alert = assign_attributes(Alert.new, params)

  alert.author = current_user

  alert
end

.create_from_params!(params, current_user = nil) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/motor/alerts/persistance.rb', line 30

def create_from_params!(params, current_user = nil)
  raise NameAlreadyExists if Alert.exists?(name: params[:name])

  alert = build_from_params(params, current_user)

  raise InvalidInterval unless alert.cron

  ApplicationRecord.transaction do
    alert.save!
  end

  alert
rescue ActiveRecord::RecordNotUnique
  retry
end

.name_already_exists?(alert) ⇒ Boolean

Returns:

  • (Boolean)

88
89
90
91
92
93
94
# File 'lib/motor/alerts/persistance.rb', line 88

def name_already_exists?(alert)
  if alert.new_record?
    Alert.exists?(name: alert.name)
  else
    Alert.exists?(['name = ? AND id != ?', alert.name, alert.id])
  end
end

.normalize_interval(interval) ⇒ Object


84
85
86
# File 'lib/motor/alerts/persistance.rb', line 84

def normalize_interval(interval)
  interval.to_s.gsub(NORMALIZE_INTERVAL_REGEXP, 'every ')
end

.tags_changed?(previous_ids, alert) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/motor/alerts/persistance.rb', line 67

def tags_changed?(previous_ids, alert)
  previous_ids.sort != alert.tags.reload.ids.sort
end

.update_from_params!(alert, params, force_replace: false) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/motor/alerts/persistance.rb', line 46

def update_from_params!(alert, params, force_replace: false)
  tag_ids = alert.tags.ids

  alert = assign_attributes(alert, params)

  raise NameAlreadyExists if !force_replace && name_already_exists?(alert)
  raise InvalidInterval unless alert.cron

  ApplicationRecord.transaction do
    archive_with_existing_name(alert) if force_replace

    alert.save!
  end

  alert.touch if tags_changed?(tag_ids, alert) && params[:updated_at].blank?

  alert
rescue ActiveRecord::RecordNotUnique
  retry
end