Skip to content

Commit

Permalink
Moved emailable logic to user model. Adjust AREL clause to only load …
Browse files Browse the repository at this point in the history
…necessary users.
  • Loading branch information
steveyken committed Sep 21, 2024
1 parent 52fdc53 commit 25b42a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions app/models/polymorphic/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ def subscribe_user_to_entity(u = user)

# Notify subscribed users when a comment is added, unless user created this comment
def notify_subscribers
commentable.subscribed_users.reject { |user_id| user_id == user.id }.each do |subscriber_id|
subscriber = User.find_by_id(subscriber_id)
if subscriber&.confirmed? && !subscriber.awaits_approval? && !subscriber.suspended? && subscriber.email.present?
SubscriptionMailer.comment_notification(subscriber, self).deliver_later
end
users_to_notify = User.where(id: commentable.subscribed_users.reject { |user_id| user_id == user.id })
users_to_notify.select(&:emailable?).each do |subscriber|
SubscriptionMailer.comment_notification(subscriber, self).deliver_later
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/users/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def inactive_message
end
end

# Send emails to active users only
#----------------------------------------------------------------------------
def emailable?
confirmed? && !awaits_approval? && !suspended? && email.present?
end

#----------------------------------------------------------------------------
def preference
@preference ||= preferences.build
Expand Down
20 changes: 20 additions & 0 deletions spec/models/users/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,24 @@
expect(search.first).to eql(user)
end
end

describe "emailable?" do
let(:user) { create(:user) }
it "should return true for standard user" do
expect(user.emailable?).to eql(true)
end
it "should return false for unconfirmed user" do
user.update(confirmed_at: nil)
expect(user.emailable?).to eql(false)
end
it "should return false for user awaiting approval" do
user.update(sign_in_count: 0, suspended_at: Time.now)
allow(Setting).to receive(:user_signup).and_return(:not_allowed)
expect(user.emailable?).to eql(false)
end
it "should return false for suspended user" do
user.update(suspended_at: Time.now)
expect(user.emailable?).to eql(false)
end
end
end

0 comments on commit 25b42a4

Please sign in to comment.