Skip to content

Commit

Permalink
Remove Duplicate mailer definition in specs and update find_categorie…
Browse files Browse the repository at this point in the history
…s_for specs to allow for Rails 5 typecasting of enum fields (#329)

Also duplicate mailer method uses .send syntax which is no longer valid in Ruby 2.6+
  • Loading branch information
yuenmichelle1 authored Jun 25, 2024
1 parent 037aaa0 commit 2070649
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def notify(user, digest_frequency)
find_notifications_for categories
organize

mail to: @user.email, subject: subject(digest_frequency)
mail to: @user.email, subject: subject(digest_frequency), template_name: 'notify'
end

def find_categories_for(digest_frequency)
@user.subscription_preferences.enabled.where(email_digest: digest_frequency).pluck :category
@user.subscription_preferences.enabled.where(email_digest: digest_frequency).pluck(:category)
end

def find_notifications_for(categories)
Expand Down
20 changes: 16 additions & 4 deletions spec/mailers/notification_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

describe '#notify' do
RSpec.shared_examples_for 'NotificationMailer#notify' do
let(:mailer){ NotificationMailer.send :new, :notify, user1, frequency }
let(:frequency_int){ SubscriptionPreference.email_digests[frequency] }
let(:mail){ mailer.notify user1, frequency }

Expand Down Expand Up @@ -100,22 +99,35 @@
before(:each){ mailer.instance_variable_set :@user, user1 }
let(:categories){ mailer.find_categories_for SubscriptionPreference.email_digests[frequency] }

# TODO: Once on Rails 5, Can Remove Version Check.
# Rails 5 introduces a change to ActiveRecord:Enum where it typecasts enum attributes to its string value.
# See: https://github.com/rails/rails/commit/c51f9b61ce1e167f5f58f07441adcfa117694301
# Luckily we can still send int values when doing a where clause on categories AND by Rails 5, we can also do a string array search of categories. So we do not need to update `find_notifications_for`
# Regardless, we will test thoroughly in Staging Canary When Up.
# Version Checks are on L112, L120, L128

context 'with immediate' do
let(:frequency){ :immediate }
subject{ categories }
it{ is_expected.to match_array Subscription.categories.values_at(:mentions, :group_mentions, :system, :moderation_reports) }
# TODO: When on Rails 5, Remove Version Checks (See L102 For Details)
expected_categories = Rails.version.starts_with?('4.2') ? Subscription.categories.values_at(:mentions, :group_mentions, :system, :moderation_reports) : ['mentions', 'group_mentions', 'system', 'moderation_reports']
it{ is_expected.to match_array expected_categories }
end

context 'with daily' do
let(:frequency){ :daily }
subject{ categories }
it{ is_expected.to match_array Subscription.categories.values_at(:participating_discussions, :followed_discussions) }
# TODO: When on Rails 5, Remove Version Checks(See L102 For Details)
expected_categories = Rails.version.starts_with?('4.2') ? Subscription.categories.values_at(:participating_discussions, :followed_discussions) : ['participating_discussions', 'followed_discussions']
it{ is_expected.to match_array expected_categories }
end

context 'with weekly' do
let(:frequency){ :weekly }
subject{ categories }
it{ is_expected.to match_array Subscription.categories.values_at(:messages, :started_discussions) }
# TODO: When on Rails 5, Remove Version Checks(See L102 For Details)
expected_categories = Rails.version.starts_with?('4.2') ? Subscription.categories.values_at(:messages, :started_discussions) : ['messages', 'started_discussions']
it{ is_expected.to match_array expected_categories }
end

context 'with disabled preferences' do
Expand Down

0 comments on commit 2070649

Please sign in to comment.