Module: Noticed::Deliverable

Extended by:
ActiveSupport::Concern
Included in:
Event
Defined in:
app/models/concerns/noticed/deliverable.rb,
app/models/noticed/deliverable/deliver_by.rb

Defined Under Namespace

Classes: DeliverBy

Instance Method Summary collapse

Instance Method Details

#deliver(recipients = nil, options = {}) ⇒ Object

CommentNotifier.deliver(User.all) CommentNotifier.deliver(User.all, priority: 10) CommentNotifier.deliver(User.all, queue: :low_priority) CommentNotifier.deliver(User.all, wait: 5.minutes) CommentNotifier.deliver(User.all, wait_until: 1.hour.from_now)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/noticed/deliverable.rb', line 63

def deliver(recipients = nil, options = {})
  validate!

  transaction do
    save!

    recipients_attributes = Array.wrap(recipients).map do |recipient|
      recipient_attributes_for(recipient)
    end

    if Rails.gem_version >= Gem::Version.new("7.0.0.alpha1")
      notifications.insert_all!(recipients_attributes, record_timestamps: true) if recipients_attributes.any?
    else
      time = Time.current
      recipients_attributes.each do |attributes|
        attributes[:created_at] = time
        attributes[:updated_at] = time
      end
      notifications.insert_all!(recipients_attributes) if recipients_attributes.any?
    end
  end

  # Enqueue delivery job
  EventJob.set(options).perform_later(self)

  self
end

#recipient_attributes_for(recipient) ⇒ Object



91
92
93
94
95
96
97
# File 'app/models/concerns/noticed/deliverable.rb', line 91

def recipient_attributes_for(recipient)
  {
    type: "#{self.class.name}::Notification",
    recipient_type: recipient.class.name,
    recipient_id: recipient.id
  }
end

#validate!Object



99
100
101
102
# File 'app/models/concerns/noticed/deliverable.rb', line 99

def validate!
  validate_params!
  validate_delivery_methods!
end

#validate_delivery_methods!Object



110
111
112
113
# File 'app/models/concerns/noticed/deliverable.rb', line 110

def validate_delivery_methods!
  bulk_delivery_methods.values.each(&:validate!)
  delivery_methods.values.each(&:validate!)
end

#validate_params!Object



104
105
106
107
108
# File 'app/models/concerns/noticed/deliverable.rb', line 104

def validate_params!
  required_param_names.each do |param_name|
    raise ValidationError, "Param `#{param_name}` is required for #{self.class.name}." unless params[param_name].present?
  end
end