Skip to content

Commit

Permalink
Merge ef1295f into e1a3b31
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Mar 14, 2021
2 parents e1a3b31 + ef1295f commit 45822e6
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 23 deletions.
22 changes: 13 additions & 9 deletions lib/rails/convert_active_record_dirty_5_0_to_5_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def call_after_create
BEFORE_CALLBACK_CHANGES = {
/(\w+)_changed\?$/ => 'will_save_change_to_{{attribute}}?',
/(\w+)_change$/ => '{{attribute}}_change_to_be_saved',
/(\w+)_was$/ => '{{attribute}}_in_database',
/(\w+)_was$/ => '{{attribute}}_in_database',
'changes' => 'changes_to_save',
'changed?' => 'has_changes_to_save?',
'changed' => 'changed_attribute_names_to_save',
Expand All @@ -68,7 +68,7 @@ def call_after_create
AFTER_CALLBACK_CHANGES = {
/(\w+)_changed\?$/ => 'saved_change_to_{{attribute}}?',
/(\w+)_change$/ => 'saved_change_to_{{attribute}}',
/(\w+)_was$/ => '{{attribute}}_before_last_save',
/(\w+)_was$/ => '{{attribute}}_before_last_save',
'changes' => 'saved_changes',
'changed?' => 'saved_changes?',
'changed' => 'saved_changes.keys',
Expand Down Expand Up @@ -102,9 +102,7 @@ def call_after_create
before_callback_names << node.arguments[0].to_value
if node.arguments[1] && node.arguments[1].type == :hash
goto_node node.arguments[1] do
BEFORE_CALLBACK_CHANGES.each do |before_name, after_name|
convert_callback(before_name, after_name)
end
BEFORE_CALLBACK_CHANGES.each do |before_name, after_name| convert_callback(before_name, after_name)end
end
end
end
Expand All @@ -120,14 +118,20 @@ def call_after_create

after_callback_names = []

%i[after_create after_update after_save after_commit after_create_commit after_update_commit after_save_commit].each do |callback_name|
%i[
after_create
after_update
after_save
after_commit
after_create_commit
after_update_commit
after_save_commit
].each do |callback_name|
with_node type: 'send', receiver: nil, message: callback_name do
after_callback_names << node.arguments[0].to_value
if node.arguments[1] && node.arguments[1].type == :hash
goto_node node.arguments[1] do
AFTER_CALLBACK_CHANGES.each do |before_name, after_name|
convert_callback(before_name, after_name)
end
AFTER_CALLBACK_CHANGES.each do |before_name, after_name| convert_callback(before_name, after_name)end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/rails/convert_render_text_to_render_plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
EOS

helper_method :replace_hash_key do |hash_node, old_key, new_key|
hash_node.children.map { |pair_node| pair_node.key.to_value == old_key ? pair_node.to_source.sub(old_key.to_s, new_key.to_s) : pair_node.to_source }.join(', ')
hash_node.children.map { |pair_node|
pair_node.key.to_value == old_key ? pair_node.to_source.sub(old_key.to_s, new_key.to_s) : pair_node.to_source
}.join(', ')
end

within_files 'app/controllers/**/*.rb' do
Expand Down
1 change: 1 addition & 0 deletions spec/rails/add_application_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ApplicationJob < ActiveJob::Base
class PostJob < ActiveJob::Base
end
EOS

let(:test_rewritten_content) { <<~EOS }
class PostJob < ApplicationJob
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails/add_application_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ApplicationRecord < ActiveRecord::Base
class Post < ActiveRecord::Base
end
EOS

let(:test_rewritten_content) { <<~EOS }
class Post < ApplicationRecord
end
Expand Down
8 changes: 2 additions & 6 deletions spec/rails/convert_active_record_dirty_5_0_to_5_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
RSpec.describe 'Convert ActiveRecord::Dirty 5.0 to 5.1' do
let(:rewriter_name) { 'rails/convert_active_record_dirty_5_0_to_5_1' }
let(:fake_file_path) { 'app/models/post.rb' }
let(:test_content) {
<<~EOS
let(:test_content) { <<~EOS }
class Post < ActiveRecord::Base
before_create :call_before_create
before_update :call_before_update, unless: :title_changed?
Expand All @@ -28,9 +27,7 @@ def call_after_create
end
end
EOS
}
let(:test_rewritten_content) {
<<~EOS
let(:test_rewritten_content) { <<~EOS }
class Post < ActiveRecord::Base
before_create :call_before_create
before_update :call_before_update, unless: :will_save_change_to_title?
Expand All @@ -52,7 +49,6 @@ def call_after_create
end
end
EOS
}

include_examples 'convertable'
end
8 changes: 2 additions & 6 deletions spec/rails/convert_render_text_to_render_plain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
RSpec.describe 'Convert render :text to render :plain' do
let(:rewriter_name) { 'rails/convert_render_text_to_render_plain' }
let(:fake_file_path) { 'app/controllers/posts_controller.rb' }
let(:test_content) {
<<~EOS
let(:test_content) { <<~EOS }
class PostsController < ApplicationController
def foo
render text: 'OK'
Expand All @@ -17,9 +16,7 @@ def bar
end
end
EOS
}
let(:test_rewritten_content) {
<<~EOS
let(:test_rewritten_content) { <<~EOS }
class PostsController < ApplicationController
def foo
render plain: 'OK'
Expand All @@ -30,7 +27,6 @@ def bar
end
end
EOS
}

include_examples 'convertable'
end
1 change: 1 addition & 0 deletions spec/rails/upgrade_3_0_to_3_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
self.include_root_in_json = false
end
EOS

let(:session_store_content) {
"
Synvert::Application.config.session_store :cookie_store, key: 'somethingold'
Expand Down
1 change: 0 additions & 1 deletion spec/rails/upgrade_5_0_to_5_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ def configs
load_sub_snippets(%w[rails/convert_active_record_dirty_5_0_to_5_1])
end


include_examples 'convertable with multiple files'
end
1 change: 1 addition & 0 deletions spec/ruby/new_2_2_hash_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{ :"foo-#{key}" => 'foo_value', :"bar-key" => 42, :"a\tb" => false, :"c'd" => nil }
{ "foo-#{key}": 'foo_value', 'bar-key': 42, "a\tb": false, "c'd": nil }
EOS

let(:test_rewritten_content) { <<~'EOS' }
{ foo: 'bar', 'foo' => 'bar' }
{ key1: 'value1', key2: 'value2' }
Expand Down

0 comments on commit 45822e6

Please sign in to comment.