|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Rails perform job in after_commit callback' do |
| 6 | + before { load_helpers(%w[helpers/parse_ruby]) } |
| 7 | + |
| 8 | + let(:rewriter_name) { 'rails/perform_job_in_after_commit_callback' } |
| 9 | + |
| 10 | + context 'ApplicationJob' do |
| 11 | + let(:fake_file_paths) { ['app/jobs/notification_job.rb', 'app/models/user.rb'] } |
| 12 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 13 | + class NotificationJob < ApplicationJob |
| 14 | + end |
| 15 | + EOF |
| 16 | + class User < ApplicationRecord |
| 17 | + after_create :send_notification |
| 18 | + after_save :update_cache |
| 19 | +
|
| 20 | + def send_notification |
| 21 | + NotificationJob.perform_later(self) |
| 22 | + end |
| 23 | +
|
| 24 | + def update_cache |
| 25 | + Cache.update(self) |
| 26 | + end |
| 27 | + end |
| 28 | + EOF |
| 29 | + let(:test_rewritten_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 30 | + class NotificationJob < ApplicationJob |
| 31 | + end |
| 32 | + EOF |
| 33 | + class User < ApplicationRecord |
| 34 | + after_create_commit :send_notification |
| 35 | + after_save :update_cache |
| 36 | +
|
| 37 | + def send_notification |
| 38 | + NotificationJob.perform_later(self) |
| 39 | + end |
| 40 | +
|
| 41 | + def update_cache |
| 42 | + Cache.update(self) |
| 43 | + end |
| 44 | + end |
| 45 | + EOF |
| 46 | + |
| 47 | + include_examples 'convertable with multiple files' |
| 48 | + end |
| 49 | + |
| 50 | + context 'Sidekiq' do |
| 51 | + let(:fake_file_paths) { ['app/jobs/notification_job.rb', 'app/models/user.rb'] } |
| 52 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 53 | + class NotificationJob |
| 54 | + include Sidekiq::Job |
| 55 | +
|
| 56 | + def perform(user) |
| 57 | + end |
| 58 | + end |
| 59 | + EOF |
| 60 | + class User < ApplicationRecord |
| 61 | + after_create :send_notification |
| 62 | + after_save :update_cache |
| 63 | +
|
| 64 | + def send_notification |
| 65 | + NotificationJob.perform_async(self) |
| 66 | + end |
| 67 | +
|
| 68 | + def update_cache |
| 69 | + Cache.update(self) |
| 70 | + end |
| 71 | + end |
| 72 | + EOF |
| 73 | + let(:test_rewritten_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 74 | + class NotificationJob |
| 75 | + include Sidekiq::Job |
| 76 | +
|
| 77 | + def perform(user) |
| 78 | + end |
| 79 | + end |
| 80 | + EOF |
| 81 | + class User < ApplicationRecord |
| 82 | + after_create_commit :send_notification |
| 83 | + after_save :update_cache |
| 84 | +
|
| 85 | + def send_notification |
| 86 | + NotificationJob.perform_async(self) |
| 87 | + end |
| 88 | +
|
| 89 | + def update_cache |
| 90 | + Cache.update(self) |
| 91 | + end |
| 92 | + end |
| 93 | + EOF |
| 94 | + |
| 95 | + include_examples 'convertable with multiple files' |
| 96 | + end |
| 97 | + |
| 98 | + context 'Mailer deliver_later' do |
| 99 | + let(:fake_file_paths) { ['app/mailers/user_mailer.rb', 'app/models/user.rb'] } |
| 100 | + let(:test_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 101 | + class UserMailer < ApplicationMailer |
| 102 | + end |
| 103 | + EOF |
| 104 | + class User < ApplicationRecord |
| 105 | + after_create :send_notification |
| 106 | + after_save :update_cache |
| 107 | +
|
| 108 | + def send_notification |
| 109 | + UserMailer.welcome(self).deliver_later |
| 110 | + end |
| 111 | +
|
| 112 | + def update_cache |
| 113 | + Cache.update(self) |
| 114 | + end |
| 115 | + end |
| 116 | + EOF |
| 117 | + let(:test_rewritten_contents) { [<<~EOF.strip, <<~EOF.strip] } |
| 118 | + class UserMailer < ApplicationMailer |
| 119 | + end |
| 120 | + EOF |
| 121 | + class User < ApplicationRecord |
| 122 | + after_create_commit :send_notification |
| 123 | + after_save :update_cache |
| 124 | +
|
| 125 | + def send_notification |
| 126 | + UserMailer.welcome(self).deliver_later |
| 127 | + end |
| 128 | +
|
| 129 | + def update_cache |
| 130 | + Cache.update(self) |
| 131 | + end |
| 132 | + end |
| 133 | + EOF |
| 134 | + |
| 135 | + include_examples 'convertable with multiple files' |
| 136 | + end |
| 137 | +end |
0 commit comments