caffeinate 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9862310b9a457373cfff3e69645b2c07a01f87ba6cafb4a1818a129a757841e
4
- data.tar.gz: 3b73b977f565f19dd681a56b585397fa4de24020e1ef320956eb993eec15bc52
3
+ metadata.gz: cac59a69d742f7948455f3f3b5787e33841664267eab72a2c6bc296735da5d1e
4
+ data.tar.gz: 826ca15444ba2242f99d8376d0f5a4aae9612846ee61ef53d1a4a53a879d0b54
5
5
  SHA512:
6
- metadata.gz: c2aaad4b624a503bc5a0e290373ebe50167ec82446deba5ae693937f59dcd8d9ef88b0796fc5b1263351098c67af32fd9e6eca7067f803b284c9d4f10a17608b
7
- data.tar.gz: 1b4f720e186533495905725aaf334087ea83eb5dd4d641ea670951b48cfa8b629188ec9872245958333484b88c5f7e1aed7fb8f37831d2b00822bcd83b4dffcb
6
+ metadata.gz: 2d1cc9c295d50c77c5db6c5f22d585372b513396d54a9a37b05f40af405f8f4e32b1e0d42045a9e5b0e0c40fe592466a4d835a67dd71ad62bab55d24b7586d15
7
+ data.tar.gz: 0ca6801b63b8e50f73654dbc6a7f5e9c5063fd825699bd6698e4af4f6846ef23358ed0955cc2d0ed8d19e705124d0f25094c0980331dd90dd31121ab06c07f3e
@@ -17,9 +17,14 @@
17
17
  # updated_at :datetime not null
18
18
  #
19
19
  module Caffeinate
20
+ # If a record tries to be `unsubscribed!` or `ended!` or `resubscribe!` and it's in a state that is not
21
+ # correct, raise this
22
+ class InvalidState < ::ActiveRecord::RecordInvalid; end
23
+
20
24
  # CampaignSubscription associates an object and its optional user to a Campaign
21
25
  # and its relevant Mailings.
22
26
  class CampaignSubscription < ApplicationRecord
27
+
23
28
  self.table_name = 'caffeinate_campaign_subscriptions'
24
29
 
25
30
  has_many :caffeinate_mailings, class_name: 'Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id, dependent: :destroy
@@ -66,23 +71,34 @@ module Caffeinate
66
71
 
67
72
  # Updates `ended_at` and runs `on_complete` callbacks
68
73
  def end!(reason = nil)
74
+ raise ::Caffeinate::InvalidState, "CampaignSubscription is already unsubscribed." if unsubscribed?
75
+
69
76
  update!(ended_at: ::Caffeinate.config.time_now, ended_reason: reason)
70
77
 
71
- caffeinate_campaign.to_dripper.run_callbacks(:on_complete, self)
78
+ caffeinate_campaign.to_dripper.run_callbacks(:on_end, self)
79
+ true
72
80
  end
73
81
 
74
82
  # Updates `unsubscribed_at` and runs `on_subscribe` callbacks
75
83
  def unsubscribe!(reason = nil)
84
+ raise ::Caffeinate::InvalidState, "CampaignSubscription is already ended." if ended?
85
+
76
86
  update!(unsubscribed_at: ::Caffeinate.config.time_now, unsubscribe_reason: reason)
77
87
 
78
88
  caffeinate_campaign.to_dripper.run_callbacks(:on_unsubscribe, self)
89
+ true
79
90
  end
80
91
 
81
- # Updates `unsubscribed_at` to nil and runs `on_subscribe` callbacks
82
- def resubscribe!
92
+ # Updates `unsubscribed_at` to nil and runs `on_subscribe` callbacks.
93
+ # Use `force` to forcefully reset. Does not create the mailings.
94
+ def resubscribe!(force = false)
95
+ raise ::Caffeinate::InvalidState, "CampaignSubscription is already ended." if ended? && !force
96
+ raise ::Caffeinate::InvalidState, "CampaignSubscription is already unsubscribed." if unsubscribed? && !force
97
+
83
98
  update!(unsubscribed_at: nil, resubscribed_at: ::Caffeinate.config.time_now)
84
99
 
85
100
  caffeinate_campaign.to_dripper.run_callbacks(:on_resubscribe, self)
101
+ true
86
102
  end
87
103
 
88
104
  private
@@ -94,6 +110,7 @@ module Caffeinate
94
110
  mailing.save!
95
111
  end
96
112
  caffeinate_campaign.to_dripper.run_callbacks(:on_subscribe, self)
113
+ true
97
114
  end
98
115
 
99
116
  def set_token!
@@ -11,6 +11,7 @@ module Caffeinate
11
11
 
12
12
  mailing.update!(sent_at: Caffeinate.config.time_now, skipped_at: nil) if message.perform_deliveries
13
13
  mailing.caffeinate_campaign.to_dripper.run_callbacks(:after_send, mailing, message)
14
+ true
14
15
  end
15
16
  end
16
17
  end
@@ -40,18 +40,18 @@ module Caffeinate
40
40
 
41
41
  # Callback before the mailings get processed.
42
42
  #
43
- # before_process do |dripper|
43
+ # before_perform do |dripper|
44
44
  # Slack.notify(:caffeinate, "Dripper is getting ready for mailing! #{dripper.caffeinate_campaign.name}!")
45
45
  # end
46
46
  #
47
47
  # @yield Caffeinate::Dripper
48
- def before_process(&block)
49
- before_process_blocks << block
48
+ def before_perform(&block)
49
+ before_perform_blocks << block
50
50
  end
51
51
 
52
52
  # :nodoc:
53
- def before_process_blocks
54
- @before_process_blocks ||= []
53
+ def before_perform_blocks
54
+ @before_perform_blocks ||= []
55
55
  end
56
56
 
57
57
  # Callback before the mailings get processed in a batch.
@@ -62,13 +62,13 @@ module Caffeinate
62
62
  #
63
63
  # @yield Caffeinate::Dripper
64
64
  # @yield Caffeinate::Mailing [Array]
65
- def on_process(&block)
66
- on_process_blocks << block
65
+ def on_perform(&block)
66
+ on_perform_blocks << block
67
67
  end
68
68
 
69
69
  # :nodoc:
70
- def on_process_blocks
71
- @on_process_blocks ||= []
70
+ def on_perform_blocks
71
+ @on_perform_blocks ||= []
72
72
  end
73
73
 
74
74
  # Callback after the all the mailings have been sent.
@@ -79,13 +79,13 @@ module Caffeinate
79
79
  #
80
80
  # @yield Caffeinate::Dripper
81
81
  # @yield Caffeinate::Mailing [Array]
82
- def after_process(&block)
83
- after_process_blocks << block
82
+ def after_perform(&block)
83
+ after_perform_blocks << block
84
84
  end
85
85
 
86
86
  # :nodoc:
87
- def after_process_blocks
88
- @after_process_blocks ||= []
87
+ def after_perform_blocks
88
+ @after_perform_blocks ||= []
89
89
  end
90
90
 
91
91
  # Callback before a Drip has called the mailer.
@@ -171,6 +171,23 @@ module Caffeinate
171
171
  @on_unsubscribe_blocks ||= []
172
172
  end
173
173
 
174
+
175
+ # Callback after a CampaignSubscriber has ended.
176
+ #
177
+ # on_end do |campaign_sub|
178
+ # Slack.notify(:caffeinate, "#{campaign_sub.id} has ended... sad day.")
179
+ # end
180
+ #
181
+ # @yield Caffeinate::CampaignSubscription
182
+ def on_end(&block)
183
+ on_end_blocks << block
184
+ end
185
+
186
+ # :nodoc:
187
+ def on_end_blocks
188
+ @on_end_blocks ||= []
189
+ end
190
+
174
191
  # Callback after a `Caffeinate::Mailing` is skipped.
175
192
  #
176
193
  # on_skip do |campaign_subscription, mailing, message|
@@ -15,28 +15,20 @@ module Caffeinate
15
15
  #
16
16
  # @return nil
17
17
  def perform!
18
- includes = [:next_caffeinate_mailing]
19
- preloads = []
20
- if ::Caffeinate.config.async_delivery?
21
- # nothing
22
- else
23
- preloads += %i[subscriber user]
24
- end
25
-
26
- run_callbacks(:before_process, self)
27
- campaign.caffeinate_campaign_subscriptions
28
- .active
29
- .joins(:next_caffeinate_mailing)
30
- .preload(*preloads)
31
- .includes(*includes)
32
- .in_batches(of: self.class.batch_size)
33
- .each do |batch|
34
- run_callbacks(:on_process, self, batch)
35
- batch.each do |subscriber|
36
- subscriber.next_caffeinate_mailing.process!
18
+ run_callbacks(:before_perform, self)
19
+ Caffeinate::Mailing
20
+ .upcoming
21
+ .unsent
22
+ .joins(:caffeinate_campaign_subscription)
23
+ .merge(Caffeinate::CampaignSubscription.active)
24
+ .in_batches(of: self.class.batch_size)
25
+ .each do |batch|
26
+ run_callbacks(:on_perform, self, batch)
27
+ batch.each do |mailing|
28
+ mailing.process!
37
29
  end
38
30
  end
39
- run_callbacks(:after_process, self)
31
+ run_callbacks(:after_perform, self)
40
32
  nil
41
33
  end
42
34
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Caffeinate
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caffeinate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Brody
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-05 00:00:00.000000000 Z
11
+ date: 2020-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails