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)



86
87
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 86

def deliver(recipients = nil, enqueue_job: true, **options)
  recipients ||= evaluate_recipients

  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)


153
154
155
# File 'app/models/concerns/noticed/deliverable.rb', line 153

def deserialize_error?
  !!params[:noticed_error]
end

#evaluate_recipientsObject



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

def evaluate_recipients
  return unless _recipients

  if _recipients.respond_to?(:call)
    instance_exec(&_recipients)
  elsif _recipients.is_a?(Symbol) && respond_to?(_recipients)
    send(_recipients)
  end
end

#recipient_attributes_for(recipient) ⇒ Object



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

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

#validate!Object



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

def validate!
  validate_params!
  validate_delivery_methods!
end

#validate_delivery_methods!Object



147
148
149
150
# File 'app/models/concerns/noticed/deliverable.rb', line 147

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

#validate_params!Object



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

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