Created
September 30, 2014 14:46
-
-
Save rummelonp/6f69a45e998275029646 to your computer and use it in GitHub Desktop.
今あるデータから雑に FactoryGirl の定義生成するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scope_to_factory(scope) | |
class_name = scope.first.class.name | |
class_underscore_name = class_name.underscore.gsub('/', '_') | |
f = File.new("#{Rails.root}/spec/factories/#{class_underscore_name}.rb", 'w') | |
f.puts "# coding: utf-8" | |
f.puts "# Read about factories at https://github.com/thoughtbot/factory_girl" | |
f.puts "" | |
f.puts "FactoryGirl.define do" | |
f.puts " factory :#{class_underscore_name}, class: \"#{class_name}\" do" | |
scope.each do |record| | |
f.puts " factory :#{class_underscore_name}_#{record.id} do" | |
record.attributes.reject { |(k, v)| | |
k =~ /updated_at|created_at/ | |
}.each do |(k, v)| | |
f.puts " #{k} #{v.inspect}" | |
end | |
f.puts " end" | |
end | |
f.puts " end" | |
f.puts "end" | |
f.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment