feste 0.2.1 → 0.3.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: 780ca4e3f5539ccc209639ad6c74c73b13c273a5a7caf22b1775c36339606445
4
- data.tar.gz: a4b6d5c3ffbc86aaf53d08c5ee767121a66b26d4959b1036b35d3c90ce8338f6
3
+ metadata.gz: 2f1e50a574b4f37ee451f00c0865e6448dc38550d5a703ee66fe1428856b73a5
4
+ data.tar.gz: 840fe1442af14cc7a173320310c7d6ebcd33818a9c938bd0e0abdf0a493f3fe4
5
5
  SHA512:
6
- metadata.gz: 8bb1dcf62ce31f4594b065cfd9ac5c8e4fc0df1fa7c83a345a2bf363432652469f84963f92b90fffe371ab429c760af824747889cf08601d45fade0b3b82f76f
7
- data.tar.gz: 264e8a84f385e74f8f5cc0333546b6f2fa90b6bd30dab3ab72b0354ef8606124642bba0b314a73ccbdd7604d11adf0e999dd6190e4f8adaf6463fae811be0aa6
6
+ metadata.gz: c959555736853d7309b6cd8d96ef90f807aabf2e5b5f7a4319ba9e18261a7e4bbcee1b62a9e27d1e2b6a4a761faa28fa43a8b5e979cc095cff34ae81cdfefd62
7
+ data.tar.gz: bee0cb29c3aa769c3bdd6dd3fa3ad830f69c4ace1ed1ff6c2a6453530d79f178140af1a972a3fe63aad1e264b3bb393bc42482a7ae0f7b1b66c008ed1eceae94
data/README.md CHANGED
@@ -48,6 +48,8 @@ Feste.configure do |config|
48
48
  config.email_source = :email
49
49
  # set the host for subscription_url
50
50
  config.host = ActionMailer::Base.default_url_options[:host]
51
+ # set callbacks
52
+ config.callback_handler = FesteCallbackHandler.new
51
53
  end
52
54
  ```
53
55
  ## Usage
@@ -137,6 +139,30 @@ en:
137
139
 
138
140
  It is recommended you DO NOT include any important emails, such as password reset emails, into a subscribable category. It is also recommended you do not include the subscription link in any email that is sent to multiple recipients. Though Feste comes with some security measures, it is assumed that each email is intended for only one recipient, and the `subscription_url` helper leads to a subsciption page meant only for that recipient. Exposing this page to other users may allow them to change subscription preferences for someone else's account.
139
141
 
142
+ ## Callbacks
143
+
144
+ If you would like to create callbacks for when a user unsubscribes or resubscribes to a mailing list, you can do so by creating a callback handler. Callback handlers should be objects with two instance methods: `unsubscribe` and `resubscribe`. You can register an instance of this object as your callback handler with the `callback_handler` configuration option.
145
+
146
+ ```ruby
147
+ # config/initializers/feste.rb
148
+
149
+ class CallbackHandler
150
+ def unsubscribe(event)
151
+ # This method is called whenever a user unsubscribes from an mailing list they were previously subscribed to.
152
+ event[:controller] # the instance of the controller
153
+ event[:subscriber] # the user that unsubscribed
154
+ end
155
+
156
+ def resubscribe(event)
157
+ # This method is called whenever a user subscribes to a mailing list they were previously unsubscribed to.
158
+ end
159
+ end
160
+
161
+ Feste.configure do |config|
162
+ config.callback_handler = CallbackHandler.new
163
+ end
164
+ ```
165
+
140
166
  ## Development
141
167
 
142
168
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -152,4 +178,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jerein
152
178
 
153
179
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
154
180
 
155
- l create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
181
+ l create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
@@ -11,7 +11,9 @@ module Feste
11
11
  end
12
12
 
13
13
  def update
14
+ events = set_callback_events
14
15
  if update_subscriptions
16
+ publish_callback_events(events)
15
17
  flash[:success] = "You have successfully updated your subscriptions!"
16
18
  else
17
19
  flash[:notice] = "Something went wrong! Please try again later."
@@ -39,5 +41,54 @@ module Feste
39
41
  subscribed.update_all(canceled: false) &&
40
42
  unsubscribed.update_all(canceled: true)
41
43
  end
44
+
45
+ def set_callback_events
46
+ events = []
47
+ if Feste.options[:callback_handler].present?
48
+ events << unsubscribe_event if user_unsubscribing_from_emails?
49
+ events << resubscribe_event if user_resubscribing_to_emails?
50
+ end
51
+ events
52
+ end
53
+
54
+ def user_unsubscribing_from_emails?
55
+ subscription_ids = user_params[:subscriptions]&.map(&:to_i) || []
56
+ subscriber.subscriptions.where(canceled: false).pluck(:id).any? do |id|
57
+ !subscription_ids.include?(id)
58
+ end
59
+ end
60
+
61
+ def user_resubscribing_to_emails?
62
+ subscription_ids = user_params[:subscriptions]&.map(&:to_i) || []
63
+ subscriber.subscriptions.where(canceled: true).pluck(:id).any? do |id|
64
+ subscription_ids.include?(id)
65
+ end
66
+ end
67
+
68
+ def unsubscribe_event
69
+ {
70
+ name: :unsubscribe,
71
+ subscriber: subscriber,
72
+ controller: self
73
+ }
74
+ end
75
+
76
+ def resubscribe_event
77
+ {
78
+ name: :resubscribe,
79
+ subscriber: subscriber,
80
+ controller: self
81
+ }
82
+ end
83
+
84
+ def publish_callback_events(events)
85
+ if Feste.options[:callback_handler].present?
86
+ events.each do |event|
87
+ if Feste.options[:callback_handler].respond_to?(event[:name])
88
+ Feste.options[:callback_handler].public_send(event[:name], event)
89
+ end
90
+ end
91
+ end
92
+ end
42
93
  end
43
- end
94
+ end
data/lib/feste.rb CHANGED
@@ -21,7 +21,8 @@ module Feste
21
21
  categories: [],
22
22
  host: nil,
23
23
  email_source: :email,
24
- authenticate_with: nil
24
+ authenticate_with: nil,
25
+ callback_handler: nil
25
26
  }
26
27
 
27
28
  def self.configure
data/lib/feste/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Feste
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feste
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Reinhardt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails