Skip to content

Commit

Permalink
abstract rails/add_application_job snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Feb 5, 2021
1 parent bf0798c commit 6fecdd7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
32 changes: 32 additions & 0 deletions lib/rails/add_application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Synvert::Rewriter.new 'rails', 'add_application_job' do
description <<-EOF
It adds app/models/application_job.rb file and replaces ActiveJob::Base with ApplicationJob in model files.
class PostJob < ActiveJob::Base
end
=>
class PostJob < ApplicationJob
end
EOF

# adds file app/models/application_job.rb
add_file 'app/jobs/application_job.rb', <<~EOS
class ApplicationJob < ActiveJob::Base
end
EOS

within_files 'app/jobs/**/*.rb' do
# class PostJob < ActiveJob::Base
# end
# =>
# class PostJob < ApplicationJob
# end
with_node type: 'class', name: { not: 'ApplicationJob' }, parent_class: 'ActiveJob::Base' do
goto_node :parent_class do
replace_with 'ApplicationJob'
end
end
end
end
18 changes: 1 addition & 17 deletions lib/rails/upgrade_4_2_to_5_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
add_snippet 'rails', 'convert_render_nothing_true_to_head_ok'
add_snippet 'rails', 'convert_rails_test_request_methods_4_2_to_5_0'
add_snippet 'rails', 'add_application_record'
add_snippet 'rails', 'add_application_job'

within_file 'config/application.rb' do
# remove config.raise_in_transactional_callbacks = true
Expand Down Expand Up @@ -127,23 +128,6 @@
end
end

# adds file app/jobs/application_job.rb
new_code = "class ApplicationJob < ActiveJob::Base\n\nend"
add_file 'app/jobs/application_job.rb', new_code

within_files 'app/jobs/**/*.rb' do
# class PostJob < ActiveJob::Base
# end
# =>
# class PostJob < ApplicationJob
# end
with_node type: 'class', name: { not: 'ApplicationJob' }, parent_class: 'ActiveJob::Base' do
goto_node :parent_class do
replace_with 'ApplicationJob'
end
end
end

within_files '**/*.rb' do
# MissingSourceFile
# =>
Expand Down
36 changes: 36 additions & 0 deletions spec/rails/add_application_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

RSpec.describe 'Add ApplicationJob' do
let(:rewriter_name) { 'rails/add_application_job' }

context 'add application_job' do
let(:fake_file_path) { 'app/jobs/application_job.rb' }
let(:test_content) { nil }
let(:test_rewritten_content) {
<<~EOS
class ApplicationJob < ActiveJob::Base
end
EOS
}

include_examples 'convertable'
end

context 'rename ActiveJob::Base' do
let(:fake_file_path) { 'app/jobs/post_job.rb' }
let(:test_content) {
<<~EOS
class PostJob < ActiveJob::Base
end
EOS
}
let(:test_rewritten_content) {
<<~EOS
class PostJob < ApplicationJob
end
EOS
}

include_examples 'convertable'
end
end
12 changes: 4 additions & 8 deletions spec/rails/upgrade_4_2_to_5_0_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ module Namespace
class PostJob < ActiveJob::Base
end
'}
let(:application_job_rewritten_content) { '
class ApplicationJob < ActiveJob::Base
end
'.strip}
let(:post_job_rewritten_content) { '
class PostJob < ApplicationJob
end
Expand Down Expand Up @@ -175,19 +170,20 @@ module Namespace
'.strip}
let(:fake_file_paths) { %w[config/application.rb config/environments/production.rb config/initializers/new_framework_defaults.rb
app/controllers/posts_controller.rb app/controllers/namespace/posts_controller.rb app/models/post.rb
app/models/namespace/post.rb app/jobs/application_job.rb app/jobs/post_job.rb app/jobs/namespace/post_job.rb] }
app/models/namespace/post.rb app/jobs/post_job.rb app/jobs/namespace/post_job.rb] }
let(:test_contents) { [application_content, production_content, nil, posts_controller_content, nested_controller_content,
post_model_content, nested_model_content, nil, post_job_content, nested_job_content] }
post_model_content, nested_model_content, post_job_content, nested_job_content] }
let(:test_rewritten_contents) { [application_rewritten_content, production_rewritten_content, new_framework_defaults_rewritten_content,
posts_controller_rewritten_content, nested_controller_rewritten_content, post_model_rewritten_content, nested_model_rewritten_content,
application_job_rewritten_content, post_job_rewritten_content, nested_job_rewritten_content] }
post_job_rewritten_content, nested_job_rewritten_content] }

before do
load_sub_snippets(%w[
rails/add_active_record_migration_rails_version
rails/convert_render_nothing_true_to_head_ok
rails/convert_rails_test_request_methods_4_2_to_5_0
rails/add_application_record
rails/add_application_job
])
end

Expand Down

0 comments on commit 6fecdd7

Please sign in to comment.