Skip to content

Commit

Permalink
add snippet to convert rails 2.3 views to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Jul 28, 2014
1 parent 5fefad7 commit fc1f01b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
37 changes: 37 additions & 0 deletions lib/rails/convert_views_2_3_to_3_0.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Synvert::Rewriter.new 'convert_views_2_3_to_3_0' do
description <<-EOF
1. remove h helper, rails 3 uses it by default.
<%= h user.login %> => <%= user.login %>
2. use erb expression instead of erb statement for view helpers.
<% form_for post do |f| %>
<% end %>
=>
<%= form_for post do |f| %>
<% end %>
EOF

%w(app/views/**/*.html.erb app/helpers/**/*.rb).each do |file_pattern|
# <%= h user.login %> => <%= user.login %>
within_files file_pattern do
with_node type: 'send', receiver: nil, message: 'h' do
replace_with "{{arguments}}"
end
end

%w(form_for form_tag).each do |message|
# <% form_for post do |f| %>
# <% end %>
# =>
# <%= form_for post do |f| %>
# <% end %>
within_files file_pattern do
with_node type: 'block', caller: {type: 'send', receiver: nil, message: message} do
replace_erb_stmt_with_expr
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/rails/convert_routes_2_3_to_3_0_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'Convert rails queries from 2.3 to 3.0' do
describe 'Convert rails routes from 2.3 to 3.0' do
before do
rewriter_path = File.join(File.dirname(__FILE__), '../../lib/rails/convert_routes_2_3_to_3_0.rb')
@rewriter = eval(File.read(rewriter_path))
Expand Down
38 changes: 38 additions & 0 deletions spec/rails/convert_views_2_3_to_3_0_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# encoding: utf-8

require 'spec_helper'

describe 'Convert rails views from 2.3 to 3.0' do
before do
rewriter_path = File.join(File.dirname(__FILE__), '../../lib/rails/convert_views_2_3_to_3_0.rb')
@rewriter = eval(File.read(rewriter_path))
end

describe 'with fakefs', fakefs: true do
let(:posts_show_content) {"""
<%= h user.login %>
<%= post.title %>
<% form_for post, :html => {:id => 'post-form'} do |f| %>
<% end %>
<%= form_for post do |f| %>
<% end %>
"""}
let(:posts_show_rewritten_content) {"""
<%= user.login %>
<%= post.title %>
<%= form_for post, :html => {:id => 'post-form'} do |f| %>
<% end %>
<%= form_for post do |f| %>
<% end %>
"""}

it 'converts' do
FileUtils.mkdir_p 'app/views/posts'
File.write 'app/views/posts/show.html.erb', posts_show_content
@rewriter.process
expect(File.read 'app/views/posts/show.html.erb').to eq posts_show_rewritten_content
end
end
end

0 comments on commit fc1f01b

Please sign in to comment.