lib/jets_upgrade/rewrite/controller.rb in jets-upgrade-0.1.0 vs lib/jets_upgrade/rewrite/controller.rb in jets-upgrade-0.1.1
- old
+ new
@@ -11,18 +11,42 @@
def marker
"def destroy"
end
def content
+ within_destroy = false
modified_lines = lines.map do |line|
if line =~ /\s{2,}def delete$/
line = line.sub('delete', 'destroy')
+ within_destroy = true
end
+ if within_destroy
+ if line =~ /\s{2,}redirect_to (\w+)$/
+ location_method = $1 # IE: posts_path
+ line = respond_to_code(location_method)
+ end
+ end
+ if within_destroy && line =~ /\s{2,}end$/
+ within_destroy = false
+ end
+
if line =~ /before_action :/ && line =~ /delete/
line = line.sub('delete', 'destroy')
end
line
end
modified_lines.join('')
+ end
+
+ def respond_to_code(location_method)
+ code = <<~EOL
+ respond_to do |format|
+ format.html { redirect_to #{location_method} }
+ format.js { render json: { location: #{location_method} } }
+ end
+ EOL
+ lines = code.split("\n")
+ # add 4 spaces to each line
+ lines.map! { |line| " #{line}" }.join("\n") + "\n"
end
end
end