Class: Mailing
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Mailing
- 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
-
#store ⇒ Store
Store the mailing is sent from.
Methods included from Models::Auditable
Has many collapse
-
#activities ⇒ ActiveRecord::Relation<Activity>
CRM activities fulfilled by this mailing.
-
#mail_activities ⇒ ActiveRecord::Relation<MailActivity>
Mail activities (individual pieces) in this batch.
Instance Method Summary collapse
-
#activity_ids ⇒ Array<Integer>
Activity ids of the mailing's mail activities.
-
#activity_ids=(act_ids) ⇒ void
Bulk-creates mail activities for the given activity ids.
-
#addresses ⇒ Array<Address, nil>
Destination addresses of all mail activities.
-
#background_job ⇒ BackgroundJobStatus?
Status record of the mailing's background job.
-
#generate_all_postage_labels_pdf ⇒ Array(Upload, nil, String)
Combines all printable mail-activity label PDFs into one uploaded PDF.
-
#has_active_background_job? ⇒ Boolean?
Whether the mailing has a background job currently running.
-
#ok_to_delete? ⇒ Boolean
Whether the mailing can be deleted (not complete, all activities deletable).
-
#ready_to_complete? ⇒ Boolean
Whether every mail activity in the batch is complete.
-
#to_s ⇒ String
Human-readable label for the mailing.
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
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Method Details
#activities ⇒ ActiveRecord::Relation<Activity>
CRM activities fulfilled by this mailing.
40 |
# File 'app/models/mailing.rb', line 40 has_many :activities, through: :mail_activities |
#activity_ids ⇒ Array<Integer>
Activity ids of the mailing's mail activities.
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.
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 |
#addresses ⇒ Array<Address, nil>
Destination addresses of all mail activities.
98 99 100 |
# File 'app/models/mailing.rb', line 98 def addresses mail_activities.map(&:mailing_address) end |
#background_job ⇒ BackgroundJobStatus?
Status record of the mailing's background job.
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_pdf ⇒ Array(Upload, nil, String)
Combines all printable mail-activity label PDFs into one uploaded PDF.
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&.&.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.
92 93 94 |
# File 'app/models/mailing.rb', line 92 def has_active_background_job? background_job&.active? end |
#mail_activities ⇒ ActiveRecord::Relation<MailActivity>
Mail activities (individual pieces) in this batch.
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).
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.
104 105 106 |
# File 'app/models/mailing.rb', line 104 def ready_to_complete? mail_activities.all?(&:complete?) end |
#store ⇒ Store
Store the mailing is sent from.
Validations:
36 |
# File 'app/models/mailing.rb', line 36 belongs_to :store, optional: true |
#to_s ⇒ String
Human-readable label for the mailing.
63 64 65 |
# File 'app/models/mailing.rb', line 63 def to_s "Mailing #{id}" end |