File tree Expand file tree Collapse file tree 5 files changed +19
-14
lines changed Expand file tree Collapse file tree 5 files changed +19
-14
lines changed Original file line number Diff line number Diff line change @@ -33,10 +33,12 @@ def deliver
33
33
end
34
34
messages . flatten!
35
35
36
- begin
37
- Exponent ::Push ::Client . new ( gzip : true ) . publish messages
38
- rescue Exception
39
- Rails . logger . error "error sending push notifications:\n #{ $!} \n " + $!. backtrace . join ( "\n " )
36
+ if messages . any?
37
+ begin
38
+ Exponent ::Push ::Client . new ( gzip : true ) . publish messages
39
+ rescue Exception
40
+ Rails . logger . error "error sending push notifications:\n #{ $!} \n " + $!. backtrace . join ( "\n " )
41
+ end
40
42
end
41
43
end
42
44
end
Original file line number Diff line number Diff line change @@ -21,7 +21,8 @@ def send_push_notifications?
21
21
end
22
22
23
23
def push_skip_emails?
24
- pref . push_notifications == "enabled_no_email"
24
+ # only skip emails when at least one device is registered
25
+ pref . push_notifications == "enabled_no_email" and push_device_registered?
25
26
end
26
27
27
28
def remove_references_before_destroy
Original file line number Diff line number Diff line change @@ -13,11 +13,7 @@ def self.apply
13
13
end
14
14
15
15
def push_notifications
16
- if user . push_device_registered?
17
- self [ :push_notifications ] || "enabled_no_email"
18
- else
19
- "disabled"
20
- end
16
+ self [ :push_notifications ] || "enabled_no_email"
21
17
end
22
18
23
19
def push_notifications = ( new_value )
Original file line number Diff line number Diff line change @@ -17,16 +17,22 @@ class NotificationTest < ActiveSupport::TestCase
17
17
assert_match ( /released/ , n . body )
18
18
end
19
19
20
- test "should send notification instead of email" do
20
+ test "should send notification instead of email when token present " do
21
21
Exponent ::Push ::Client . any_instance . expects ( :publish )
22
22
23
- # no token, email is sent
23
+ assert_equal 'enabled_no_email' , @user . pref . push_notifications
24
+ assert @user . send_push_notifications?
25
+ refute @user . push_skip_emails?
26
+
27
+ # no token, email is still sent
24
28
mail = Mailer . news_added @user , @news
25
29
mail . deliver
26
30
assert_equal 1 , @emails . size
27
31
28
32
@emails . clear
29
33
@user . expo_push_tokens . create! token : "foo"
34
+ assert @user . send_push_notifications?
35
+ assert @user . push_skip_emails?
30
36
31
37
# token is set, push notification but no email is sent
32
38
mail = Mailer . news_added @user , @news
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ class UserTest < ActiveSupport::TestCase
7
7
@user = User . find_by_login "jsmith"
8
8
end
9
9
10
- test "should default to no push notifs " do
11
- refute @user . send_push_notifications?
10
+ test "should not skip emails when no tokens present " do
11
+ assert @user . send_push_notifications?
12
12
refute @user . push_skip_emails?
13
13
end
14
14
You can’t perform that action at this time.
0 commit comments