Class: Mailing

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/mailing.rb

Overview

== Schema Information

Table name: mailings
Database name: primary

id :integer not null, primary key
cover_file_size :integer
cover_letter_file_name :string(255)
cover_letter_type :string(255)
date_completed :datetime
description :text
label_content_type :string(255)
label_file_name :string(255)
label_file_size :integer
state :string(255)
created_at :datetime
updated_at :datetime
creator_id :integer
job_id :string(255)
store_id :integer
updater_id :integer

Indexes

index_mailings_on_store_id (store_id)

Foreign Keys

fk_rails_... (store_id => stores.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Method Details

#activitiesActiveRecord::Relation<Activity>

CRM activities fulfilled by this mailing.

Returns:

See Also:



40
# File 'app/models/mailing.rb', line 40

has_many :activities, through: :mail_activities

#activity_idsArray<Integer>

Activity ids of the mailing's mail activities.

Returns:

  • (Array<Integer>)


78
79
80
# File 'app/models/mailing.rb', line 78

def activity_ids
  mail_activities.pluck(:activity_id)
end

#activity_ids=(act_ids) ⇒ void

This method returns an undefined value.

Bulk-creates mail activities for the given activity ids.

Parameters:

  • act_ids (Array<Integer>)

    activity ids to add to the mailing



70
71
72
73
74
# File 'app/models/mailing.rb', line 70

def activity_ids=(act_ids)
  act_ids.each do |act_id|
    mail_activities << MailActivity.create(mailing_id: id, activity_id: act_id)
  end
end

#addressesArray<Address, nil>

Destination addresses of all mail activities.

Returns:



98
99
100
# File 'app/models/mailing.rb', line 98

def addresses
  mail_activities.map(&:mailing_address)
end

#background_jobBackgroundJobStatus?

Status record of the mailing's background job.

Returns:



84
85
86
87
88
# File 'app/models/mailing.rb', line 84

def background_job
  return unless job_id

  BackgroundJobStatus.find(job_id)
end

#generate_all_postage_labels_pdfArray(Upload, nil, String)

Combines all printable mail-activity label PDFs into one uploaded PDF.

Returns:

  • (Array(Upload, nil, String))

    [upload, summary message] pair



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/mailing.rb', line 116

def generate_all_postage_labels_pdf
  upload = nil
  files_to_combine = []
  nil
  msg_arr = []
  mail_activities_to_print = mail_activities.complete.select(&:ready_to_print_labels?)
  t = mail_activities_to_print.length
  p = 0
  mail_activities_to_print.each do |ma|
    pdf = ma.all_labels_pdf&.attachment&.path
    if pdf
      files_to_combine << pdf
      p += 1
    else
      msg_arr << ma.name.to_s
    end
  rescue Dragonfly::Job::Fetch::NotFound
    msg_arr << ma.name.to_s
  end
  unless files_to_combine.empty?
    file_name = "all_postage_labels_mailing_#{id}_#{Time.current.strftime('%m_%d_%Y_%I_%M%p')}.pdf".downcase
    output_file_path = Upload.temp_location(file_name)
    all_postage_labels_path = PdfTools.combine(files_to_combine, output_file_path: output_file_path)
    upload = Upload.uploadify(all_postage_labels_path, "all_postage_labels_path_pdf")
  end
  msg = "Processed #{p} of #{t} mail activities. "
  msg += "Postage Labels could not generate for #{msg_arr.join(', ')}." unless msg_arr.empty?
  [upload, msg]
end

#has_active_background_job?Boolean?

Whether the mailing has a background job currently running.

Returns:

  • (Boolean, nil)


92
93
94
# File 'app/models/mailing.rb', line 92

def has_active_background_job?
  background_job&.active?
end

#mail_activitiesActiveRecord::Relation<MailActivity>

Mail activities (individual pieces) in this batch.

Returns:

See Also:



38
# File 'app/models/mailing.rb', line 38

has_many :mail_activities, dependent: :destroy

#ok_to_delete?Boolean

Whether the mailing can be deleted (not complete, all activities deletable).

Returns:

  • (Boolean)


110
111
112
# File 'app/models/mailing.rb', line 110

def ok_to_delete?
  !complete? && mail_activities.all?(&:ok_to_delete?)
end

#ready_to_complete?Boolean

Whether every mail activity in the batch is complete.

Returns:

  • (Boolean)


104
105
106
# File 'app/models/mailing.rb', line 104

def ready_to_complete?
  mail_activities.all?(&:complete?)
end

#storeStore

Store the mailing is sent from.

Returns:

See Also:

Validations:



36
# File 'app/models/mailing.rb', line 36

belongs_to :store, optional: true

#to_sString

Human-readable label for the mailing.

Returns:

  • (String)


63
64
65
# File 'app/models/mailing.rb', line 63

def to_s
  "Mailing #{id}"
end