Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save labocho/950145461b6aadce24a0c2807ffc7f21 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/950145461b6aadce24a0c2807ffc7f21 to your computer and use it in GitHub Desktop.
Synvert snippet that converts machinist style fixture generation (like `Foo::Bar.make`) to factory_girl's one (like `build(:foo_bar)`) etc.
# synvert --load use_factory_girl_instead_of_machinist.rb --run factory_girl/use_factory_girl_instead_of_machinist .
# convert `Foo::Bar.make` to `build(:foo_bar)` etc.
require "active_support/core_ext/string"
Synvert::Rewriter.new 'factory_girl', 'use_factory_girl_instead_of_machinist' do
%w(test/**/*.rb spec/**/*.rb features/**/*.rb).each do |file_pattern|
within_files file_pattern do
with_node type: 'send', message: "make" do
name = current_node.receiver.to_source.underscore.gsub("/", "_")
puts current_node.to_source
case current_node.arguments.size
when 0
replace_with "build(:#{name})"
else
replace_with "build(:#{name}, #{current_node.arguments.map(&:to_source).join(", ")})"
end
end
with_node type: 'send', message: "make!" do
name = current_node.receiver.to_source.underscore.gsub("/", "_")
puts current_node.to_source
case current_node.arguments.size
when 0
replace_with "create(:#{name})"
else
replace_with "create(:#{name}, #{current_node.arguments.map(&:to_source).join(", ")})"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment