Skip to content

Commit

Permalink
handle callback block in rails/convert_active_record_dirty_5_0_to_5_1
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Mar 16, 2021
1 parent 96b7fe6 commit 33f4b3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/rails/convert_active_record_dirty_5_0_to_5_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,19 @@ def call_after_create
#
# after_save :invalidate_cache, if: :status_changed?
with_node type: 'send', receiver: nil, message: callback_name do
custom_callback_names << node.arguments[0].to_value if node.arguments[0].type == :sym
custom_callback_names << node.arguments[0].to_value if !node.arguments.empty? && node.arguments[0].type == :sym
callback_changes.each do |before_name, after_name|
convert_dirty_api_change(before_name, after_name, attributes)
end
end

# find callback like
#
# before_save do
# if status_chagned?
# end
# end
with_node type: 'block', caller: { type: 'send', receiver: nil, message: callback_name } do
callback_changes.each do |before_name, after_name|
convert_dirty_api_change(before_name, after_name, attributes)
end
Expand Down
10 changes: 10 additions & 0 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 @@ -27,6 +27,11 @@ class Post < ActiveRecord::Base
after_update :call_after_update, unless: :title_changed?
after_save :call_after_save, if: -> { status_changed? || summary_changed? }
before_save do
if title_changed?
end
end
def call_before_create
if title_changed?
changes
Expand All @@ -51,6 +56,11 @@ class Post < ActiveRecord::Base
after_update :call_after_update, unless: :saved_change_to_title?
after_save :call_after_save, if: -> { saved_change_to_status? || saved_change_to_summary? }
before_save do
if will_save_change_to_title?
end
end
def call_before_create
if will_save_change_to_title?
changes_to_save
Expand Down

0 comments on commit 33f4b3e

Please sign in to comment.