caffeinate 0.10.0 → 0.12.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: 3f6b38c582c7ac99605a366b1217a973ebea58dad20cc60e4dd4a0f555da7474
4
- data.tar.gz: a975e28f6386905c9e776cf2cc1dcb835054f0200d91fce9554d5c1f4bcdfd9b
3
+ metadata.gz: 80e462cd99749a8f1352d481fec74d6a053a1660d200888a93844205eafb984c
4
+ data.tar.gz: 1c709353c202c7a97eb2fd8067f31ee40ee2753d93f8771232494867d47ce9c0
5
5
  SHA512:
6
- metadata.gz: dd713236a833425f2a17d8c455fadaec49ba60bf17083c4bd879aa343611d30793e6aaef0e70db5e6cae3e760beec69ea41bdb2e38881ec2ecaabdb00c9c28a7
7
- data.tar.gz: 935e59b886250e6b216535f236241221f313eefe304b9a588f0c42384d0236335e8da0fef7f71421dd80bef4e3fd114729153766c1d52bc95f4c760d681ba2c9
6
+ metadata.gz: 9d4261e176299efc975f8d11791c95a58740bd94eaa6ae2e931fb262240f9e24a27cc1c88230965386ff4658e037dcbeb00ebf2100399d077872be0196e3ba8c
7
+ data.tar.gz: 208bc056825501459d0247e4fa91f64b3d9c4408f8219d7f47aa6dd492e9b183e614adeb97b5746f3e9c686d31aa38035667cc003142c93417f1d6cb8935b906
@@ -28,8 +28,9 @@ module Caffeinate
28
28
 
29
29
  has_many :caffeinate_mailings, class_name: 'Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id, dependent: :destroy
30
30
  has_many :mailings, class_name: 'Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id, dependent: :destroy
31
+ has_many :future_mailings, -> { upcoming.unsent }, class_name: '::Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id
31
32
 
32
- has_one :next_caffeinate_mailing, -> { upcoming.unsent.order(send_at: :asc) }, class_name: '::Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id
33
+ has_one :next_caffeinate_mailing, -> { joins(:caffeinate_campaign_subscription).where(caffeinate_campaign_subscriptions: { ended_at: nil, unsubscribed_at: nil }).upcoming.unsent.order(send_at: :asc) }, class_name: '::Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id
33
34
  has_one :next_mailing, -> { joins(:caffeinate_campaign_subscription).where(caffeinate_campaign_subscriptions: { ended_at: nil, unsubscribed_at: nil }).upcoming.unsent.order(send_at: :asc) }, class_name: '::Caffeinate::Mailing', foreign_key: :caffeinate_campaign_subscription_id
34
35
 
35
36
  belongs_to :caffeinate_campaign, class_name: 'Caffeinate::Campaign', foreign_key: :caffeinate_campaign_id
@@ -49,6 +50,8 @@ module Caffeinate
49
50
  before_validation :set_token!, on: [:create]
50
51
  validates :token, uniqueness: true, on: [:create]
51
52
 
53
+ before_validation :call_dripper_before_subscribe_blocks!, on: :create
54
+
52
55
  after_commit :create_mailings!, on: :create
53
56
 
54
57
  after_commit :on_complete, if: :completed?
@@ -143,6 +146,10 @@ module Caffeinate
143
146
 
144
147
  private
145
148
 
149
+ def call_dripper_before_subscribe_blocks!
150
+ caffeinate_campaign.to_dripper.run_callbacks(:before_subscribe, self)
151
+ end
152
+
146
153
  def on_complete
147
154
  caffeinate_campaign.to_dripper.run_callbacks(:on_complete, self)
148
155
  end
@@ -30,6 +30,8 @@ module Caffeinate
30
30
  scope :skipped, -> { where.not(skipped_at: nil) }
31
31
  scope :unskipped, -> { where(skipped_at: nil) }
32
32
 
33
+ after_touch :end_if_no_mailings!
34
+
33
35
  def initialize_dup(args)
34
36
  super
35
37
  self.send_at = nil
@@ -119,5 +121,9 @@ module Caffeinate
119
121
  raise NoMethodError, "Neither perform_later or perform_async are defined on #{klass}."
120
122
  end
121
123
  end
124
+
125
+ def end_if_no_mailings!
126
+ end! if future_mailings.empty?
127
+ end
122
128
  end
123
129
  end
@@ -6,6 +6,26 @@ module Caffeinate
6
6
  #
7
7
  # Handles the block and provides convenience methods for the drip
8
8
  class Drip
9
+ class OptionEvaluator
10
+ def initialize(thing, drip, mailing)
11
+ @thing = thing
12
+ @drip = drip
13
+ @mailing = mailing
14
+ end
15
+
16
+ def call
17
+ if @thing.is_a?(Symbol)
18
+ @drip.dripper.new.send(@thing, @drip, @mailing)
19
+ elsif @thing.is_a?(Proc)
20
+ @mailing.instance_exec(&@thing)
21
+ elsif @thing.is_a?(String)
22
+ Time.parse(@thing)
23
+ else
24
+ @thing
25
+ end
26
+ end
27
+ end
28
+
9
29
  attr_reader :dripper, :action, :options, :block
10
30
 
11
31
  def initialize(dripper, action, options, &block)
@@ -26,13 +46,16 @@ module Caffeinate
26
46
  start += options[:every] if mailing.caffeinate_campaign_subscription.caffeinate_mailings.count.positive?
27
47
  date = start.from_now
28
48
  elsif options[:on]
29
- date = mailing.instance_exec(&options[:on])
49
+ date = OptionEvaluator.new(options[:on], self, mailing).call
30
50
  else
31
- date = options[:delay].from_now
51
+ date = OptionEvaluator.new(options[:delay], self, mailing).call
52
+ if date.respond_to?(:from_now)
53
+ date = date.from_now
54
+ end
32
55
  end
33
56
 
34
57
  if options[:at]
35
- time = Time.parse(options[:at])
58
+ time = OptionEvaluator.new(options[:at], self, mailing).call
36
59
  return date.change(hour: time.hour, min: time.min, sec: time.sec)
37
60
  end
38
61
 
@@ -21,6 +21,14 @@ module Caffeinate
21
21
  end
22
22
  end
23
23
 
24
+ def before_subscribe(&block)
25
+ before_subscribe_blocks << block
26
+ end
27
+
28
+ def before_subscribe_blocks
29
+ @before_subscribe_blocks ||= []
30
+ end
31
+
24
32
  # Callback after a Caffeinate::CampaignSubscription is created, and after the Caffeinate::Mailings have
25
33
  # been created.
26
34
  #
@@ -16,11 +16,7 @@ module Caffeinate
16
16
  # @return nil
17
17
  def perform!
18
18
  run_callbacks(:before_perform, self)
19
- Caffeinate::Mailing
20
- .upcoming
21
- .unsent
22
- .joins(:caffeinate_campaign_subscription)
23
- .merge(Caffeinate::CampaignSubscription.active.where(caffeinate_campaign: self.campaign))
19
+ self.class.upcoming_mailings
24
20
  .in_batches(of: self.class.batch_size)
25
21
  .each do |batch|
26
22
  run_callbacks(:on_perform, self, batch)
@@ -35,6 +31,14 @@ module Caffeinate
35
31
  def perform!
36
32
  new.perform!
37
33
  end
34
+
35
+ def upcoming_mailings
36
+ Caffeinate::Mailing
37
+ .upcoming
38
+ .unsent
39
+ .joins(:caffeinate_campaign_subscription)
40
+ .merge(Caffeinate::CampaignSubscription.active.where(caffeinate_campaign: campaign))
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Caffeinate
4
- VERSION = '0.10.0'
4
+ VERSION = '0.12.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.10.0
4
+ version: 0.12.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-26 00:00:00.000000000 Z
11
+ date: 2021-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails