Module: Noticed::Deliverable

Extended by:
ActiveSupport::Concern
Included in:
Ephemeral, 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, enqueue_job: true, **options) ⇒ Object Also known as: deliver_later

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)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/concerns/noticed/deliverable.rb', line 88

def deliver(recipients = nil, enqueue_job: true, **options)
  validate!

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

    self.notifications_count = recipients_attributes.size
    save!

    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) if enqueue_job

  self
end

#deserialize_error?Boolean

If a GlobalID record in params is no longer found, the params will default with a noticed_error key

Returns:

  • (Boolean)


143
144
145
# File 'app/models/concerns/noticed/deliverable.rb', line 143

def deserialize_error?
  !!params[:noticed_error]
end

#recipient_attributes_for(recipient) ⇒ Object



118
119
120
121
122
123
124
# File 'app/models/concerns/noticed/deliverable.rb', line 118

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

#validate!Object



126
127
128
129
# File 'app/models/concerns/noticed/deliverable.rb', line 126

def validate!
  validate_params!
  validate_delivery_methods!
end

#validate_delivery_methods!Object



137
138
139
140
# File 'app/models/concerns/noticed/deliverable.rb', line 137

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

#validate_params!Object



131
132
133
134
135
# File 'app/models/concerns/noticed/deliverable.rb', line 131

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