caffeinate 0.8.0 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/caffeinate/campaign.rb +11 -3
- data/app/models/caffeinate/campaign_subscription.rb +32 -0
- data/app/views/layouts/_caffeinate.html.erb +1 -1
- data/lib/caffeinate.rb +11 -1
- data/lib/caffeinate/drip_evaluator.rb +11 -8
- data/lib/caffeinate/dripper/perform.rb +1 -1
- data/lib/caffeinate/dripper/subscriber.rb +14 -2
- data/lib/caffeinate/mail_ext.rb +11 -0
- data/lib/caffeinate/version.rb +1 -1
- data/lib/generators/caffeinate/mailer_generator.rb +24 -0
- data/lib/generators/caffeinate/templates/mailer.rb.tt +9 -0
- data/lib/generators/caffeinate/views_generator.rb +44 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa3c41f5c5af2ed5a8733c8c5f8614a99c2df6cfa6549bce48b6c521aedfc647
|
4
|
+
data.tar.gz: b8585a93022bbf70657f8d81485d9691b6a0c90192fb9b632da2a11fde45579b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cd7ab9dced54ac5cdc6be24498253bf95230d78301d1fecad21c9e74e325ad4bb05eea4cf22e50214448c304a25068f879f12ea2e9af5a6ce59b41e28aab654
|
7
|
+
data.tar.gz: d066b26a02543079a9a9571d71042216e3ccb69d8440b6efccb93fb47bf78e3367b58d681c7312c0c806b96d829edca1114f67951eb2c97f0c7aec9c818a38c3
|
@@ -56,7 +56,15 @@ module Caffeinate
|
|
56
56
|
def unsubscribe(subscriber, **args)
|
57
57
|
reason = args.delete(:reason)
|
58
58
|
subscription = subscriber(subscriber, **args)
|
59
|
-
|
59
|
+
return false if subscription.nil?
|
60
|
+
|
61
|
+
subscription.unsubscribe(reason)
|
62
|
+
end
|
63
|
+
|
64
|
+
def unsubscribe!(subscriber, **args)
|
65
|
+
reason = args.delete(:reason)
|
66
|
+
subscription = subscriber(subscriber, **args)
|
67
|
+
raise ::ActiveRecord::RecordInvalid, subscription if subscription.nil?
|
60
68
|
|
61
69
|
subscription.unsubscribe!(reason)
|
62
70
|
end
|
@@ -64,12 +72,12 @@ module Caffeinate
|
|
64
72
|
# Creates a `CampaignSubscription` object for the present Campaign. Allows passing `**args` to
|
65
73
|
# delegate additional arguments to the record. Uses `find_or_create_by`.
|
66
74
|
def subscribe(subscriber, **args)
|
67
|
-
caffeinate_campaign_subscriptions.
|
75
|
+
caffeinate_campaign_subscriptions.create(subscriber: subscriber, **args)
|
68
76
|
end
|
69
77
|
|
70
78
|
# Subscribes an object to a campaign. Raises `ActiveRecord::RecordInvalid` if the record was invalid.
|
71
79
|
def subscribe!(subscriber, **args)
|
72
|
-
|
80
|
+
caffeinate_campaign_subscriptions.create!(subscriber: subscriber, **args)
|
73
81
|
end
|
74
82
|
end
|
75
83
|
end
|
@@ -83,6 +83,16 @@ module Caffeinate
|
|
83
83
|
true
|
84
84
|
end
|
85
85
|
|
86
|
+
# Updates `ended_at` and runs `on_complete` callbacks
|
87
|
+
def end(reason = nil)
|
88
|
+
return false if unsubscribed?
|
89
|
+
|
90
|
+
result = update(ended_at: ::Caffeinate.config.time_now, ended_reason: reason)
|
91
|
+
|
92
|
+
caffeinate_campaign.to_dripper.run_callbacks(:on_end, self)
|
93
|
+
result
|
94
|
+
end
|
95
|
+
|
86
96
|
# Updates `unsubscribed_at` and runs `on_subscribe` callbacks
|
87
97
|
def unsubscribe!(reason = nil)
|
88
98
|
raise ::Caffeinate::InvalidState, 'CampaignSubscription is already ended.' if ended?
|
@@ -93,6 +103,16 @@ module Caffeinate
|
|
93
103
|
true
|
94
104
|
end
|
95
105
|
|
106
|
+
# Updates `unsubscribed_at` and runs `on_subscribe` callbacks
|
107
|
+
def unsubscribe(reason = nil)
|
108
|
+
return false if ended?
|
109
|
+
|
110
|
+
result = update(unsubscribed_at: ::Caffeinate.config.time_now, unsubscribe_reason: reason)
|
111
|
+
|
112
|
+
caffeinate_campaign.to_dripper.run_callbacks(:on_unsubscribe, self)
|
113
|
+
result
|
114
|
+
end
|
115
|
+
|
96
116
|
# Updates `unsubscribed_at` to nil and runs `on_subscribe` callbacks.
|
97
117
|
# Use `force` to forcefully reset. Does not create the mailings.
|
98
118
|
def resubscribe!(force = false)
|
@@ -105,6 +125,18 @@ module Caffeinate
|
|
105
125
|
true
|
106
126
|
end
|
107
127
|
|
128
|
+
# Updates `unsubscribed_at` to nil and runs `on_subscribe` callbacks.
|
129
|
+
# Use `force` to forcefully reset. Does not create the mailings.
|
130
|
+
def resubscribe!(force = false)
|
131
|
+
return false if ended? && !force
|
132
|
+
return false if unsubscribed? && !force
|
133
|
+
|
134
|
+
result = update(unsubscribed_at: nil, resubscribed_at: ::Caffeinate.config.time_now)
|
135
|
+
|
136
|
+
caffeinate_campaign.to_dripper.run_callbacks(:on_resubscribe, self)
|
137
|
+
result
|
138
|
+
end
|
139
|
+
|
108
140
|
def completed?
|
109
141
|
caffeinate_mailings.unsent.count.zero?
|
110
142
|
end
|
data/lib/caffeinate.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
%w(
|
6
|
+
active_record/railtie
|
7
|
+
action_controller/railtie
|
8
|
+
action_view/railtie
|
9
|
+
action_mailer/railtie
|
10
|
+
).each do |railtie|
|
11
|
+
require railtie
|
12
|
+
end
|
13
|
+
|
4
14
|
require 'caffeinate/mail_ext'
|
5
15
|
require 'caffeinate/engine'
|
6
16
|
require 'caffeinate/drip'
|
@@ -12,25 +12,28 @@ module Caffeinate
|
|
12
12
|
def call(&block)
|
13
13
|
return true unless block
|
14
14
|
|
15
|
-
|
15
|
+
catch(:abort) do
|
16
|
+
return instance_eval(&block)
|
17
|
+
end
|
18
|
+
false
|
16
19
|
end
|
17
20
|
|
18
21
|
# Ends the CampaignSubscription
|
19
|
-
def end!
|
20
|
-
mailing.caffeinate_campaign_subscription.end!
|
21
|
-
|
22
|
+
def end!(*args)
|
23
|
+
mailing.caffeinate_campaign_subscription.end!(*args)
|
24
|
+
throw(:abort)
|
22
25
|
end
|
23
26
|
|
24
27
|
# Unsubscribes the CampaignSubscription
|
25
|
-
def unsubscribe!
|
26
|
-
mailing.caffeinate_campaign_subscription.unsubscribe!
|
27
|
-
|
28
|
+
def unsubscribe!(*args)
|
29
|
+
mailing.caffeinate_campaign_subscription.unsubscribe!(*args)
|
30
|
+
throw(:abort)
|
28
31
|
end
|
29
32
|
|
30
33
|
# Skips the mailing
|
31
34
|
def skip!
|
32
35
|
mailing.skip!
|
33
|
-
|
36
|
+
throw(:abort)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
end
|
@@ -20,7 +20,7 @@ module Caffeinate
|
|
20
20
|
.upcoming
|
21
21
|
.unsent
|
22
22
|
.joins(:caffeinate_campaign_subscription)
|
23
|
-
.merge(Caffeinate::CampaignSubscription.active)
|
23
|
+
.merge(Caffeinate::CampaignSubscription.active.where(caffeinate_campaign: self.campaign))
|
24
24
|
.in_batches(of: self.class.batch_size)
|
25
25
|
.each do |batch|
|
26
26
|
run_callbacks(:on_perform, self, batch)
|
@@ -31,10 +31,10 @@ module Caffeinate
|
|
31
31
|
#
|
32
32
|
# @return [Caffeinate::CampaignSubscriber] the created CampaignSubscriber
|
33
33
|
def subscribe(subscriber, **args)
|
34
|
-
caffeinate_campaign.subscribe(subscriber, **args)
|
34
|
+
caffeinate_campaign.subscribe!(subscriber, **args)
|
35
35
|
end
|
36
36
|
|
37
|
-
# Unsubscribes from the campaign.
|
37
|
+
# Unsubscribes from the campaign. Returns false if something's wrong.
|
38
38
|
#
|
39
39
|
# OrderDripper.unsubscribe(order, user: order.user)
|
40
40
|
#
|
@@ -46,6 +46,18 @@ module Caffeinate
|
|
46
46
|
caffeinate_campaign.unsubscribe(subscriber, **args)
|
47
47
|
end
|
48
48
|
|
49
|
+
# Unsubscribes from the campaign. Raises error if somerthing's wrong.
|
50
|
+
#
|
51
|
+
# OrderDripper.unsubscribe(order, user: order.user)
|
52
|
+
#
|
53
|
+
# @param [ActiveRecord::Base] subscriber The object subscribing
|
54
|
+
# @option [ActiveRecord::Base] :user The associated user (optional)
|
55
|
+
#
|
56
|
+
# @return [Caffeinate::CampaignSubscriber] the CampaignSubscriber
|
57
|
+
def unsubscribe!(subscriber, **args)
|
58
|
+
caffeinate_campaign.unsubscribe!(subscriber, **args)
|
59
|
+
end
|
60
|
+
|
49
61
|
# :nodoc:
|
50
62
|
def subscribes_block
|
51
63
|
raise(NotImplementedError, 'Define subscribes') unless @subscribes_block
|
data/lib/caffeinate/mail_ext.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mail
|
4
|
+
def self.from_source(source)
|
5
|
+
Mail.new Mail::Utilities.binary_unsafe_to_crlf(source.to_s)
|
6
|
+
end
|
7
|
+
|
4
8
|
# Extend Mail::Message to account for a Caffeinate::Mailing
|
5
9
|
class Message
|
6
10
|
attr_accessor :caffeinate_mailing
|
7
11
|
|
12
|
+
def caffeinate_mailing=(mailing)
|
13
|
+
@caffeinate_mailing = mailing
|
14
|
+
if mailing.is_a?(::Caffeinate::Mailing)
|
15
|
+
header['List-Unsubscribe'] = "<#{Caffeinate::UrlHelpers.caffeinate_subscribe_url(mailing.subscription)}>"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
8
19
|
def caffeinate?
|
9
20
|
caffeinate_mailing.present?
|
10
21
|
end
|
data/lib/caffeinate/version.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Caffeinate
|
4
|
+
module Generators
|
5
|
+
# Creates a mailer from a dripper.
|
6
|
+
class MailerGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path('templates', __dir__)
|
8
|
+
include ::Rails::Generators::Migration
|
9
|
+
argument :dripper, banner: "dripper"
|
10
|
+
|
11
|
+
desc 'Creates a Mailer class from a dripper.'
|
12
|
+
|
13
|
+
def create_mailer
|
14
|
+
@dripper_klass = @dripper.safe_constantize
|
15
|
+
if @dripper_klass.nil?
|
16
|
+
raise ArgumentError, "Unknown dripper #{@dripper}"
|
17
|
+
end
|
18
|
+
@mailer_class = @dripper_klass.defaults[:mailer_class] || @dripper_klass.defaults[:mailer]
|
19
|
+
template 'mailer.rb', "app/mailers/#{@mailer_class.underscore}.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= @mailer_class %> < ApplicationMailer
|
2
|
+
<% @dripper_klass.drips.each do |action| -%>
|
3
|
+
def <%= action.action %><% if action.options[:using].nil? %>(mailing)<% end %>
|
4
|
+
<% if action.options[:using].nil? -%>
|
5
|
+
@mailing = mailing
|
6
|
+
<% end -%>
|
7
|
+
end
|
8
|
+
<% end -%>
|
9
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
module Caffeinate
|
6
|
+
module Generators
|
7
|
+
module ViewPathTemplates #:nodoc:
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
public_task :copy_views
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_views
|
15
|
+
view_directory :campaign_subscriptions
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def view_directory(name, _target_path = nil)
|
21
|
+
directory name.to_s, _target_path || "#{target_path}/#{name}" do |content|
|
22
|
+
content
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def target_path
|
27
|
+
@target_path ||= "app/views/caffeinate"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class SharedViewsGenerator < Rails::Generators::Base #:nodoc:
|
32
|
+
include ViewPathTemplates
|
33
|
+
source_root File.expand_path("../../../../app/views/caffeinate", __FILE__)
|
34
|
+
desc "Copies shared Caffeinate views to your application."
|
35
|
+
hide!
|
36
|
+
end
|
37
|
+
|
38
|
+
class ViewsGenerator < Rails::Generators::Base
|
39
|
+
desc "Copies Caffeinate views to your application."
|
40
|
+
|
41
|
+
invoke SharedViewsGenerator
|
42
|
+
end
|
43
|
+
end
|
44
|
+
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
|
+
version: '0.9'
|
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-
|
11
|
+
date: 2020-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -175,8 +175,11 @@ files:
|
|
175
175
|
- lib/caffeinate/url_helpers.rb
|
176
176
|
- lib/caffeinate/version.rb
|
177
177
|
- lib/generators/caffeinate/install_generator.rb
|
178
|
+
- lib/generators/caffeinate/mailer_generator.rb
|
178
179
|
- lib/generators/caffeinate/templates/application_dripper.rb
|
179
180
|
- lib/generators/caffeinate/templates/caffeinate.rb
|
181
|
+
- lib/generators/caffeinate/templates/mailer.rb.tt
|
182
|
+
- lib/generators/caffeinate/views_generator.rb
|
180
183
|
homepage: https://github.com/joshmn/caffeinate
|
181
184
|
licenses:
|
182
185
|
- MIT
|