Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions lib/factory_bot/convert_factory_girl_to_factory_bot.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
Synvert::Rewriter.new 'factory_bot', 'convert_factory_girl_to_factory_bot' do
description <<-EOF
It converts factory_girl to factory_bot

require 'factory_girl'
require 'factory_girl_rails'
=>
require 'factory_bot'
require 'factory_bot_rails'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
=>
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end


FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
username Faker::Name.first_name.downcase
password "Sample:1"
password_confirmation "Sample:1"
end
end
=>
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
username Faker::Name.first_name.downcase
password "Sample:1"
password_confirmation "Sample:1"
end
end

user = FactoryGirl.create(:user)
=>
user = FactoryBot.create(:user)
description <<~EOF
It converts factory_girl to factory_bot
require 'factory_girl'
require 'factory_girl_rails'
=>
require 'factory_bot'
require 'factory_bot_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
=>
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
username Faker::Name.first_name.downcase
password "Sample:1"
password_confirmation "Sample:1"
end
end
=>
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
username Faker::Name.first_name.downcase
password "Sample:1"
password_confirmation "Sample:1"
end
end
user = FactoryGirl.create(:user)
=>
user = FactoryBot.create(:user)
EOF

within_files '{test,spec}/**/*.rb' do
Expand Down
88 changes: 44 additions & 44 deletions lib/factory_bot/deprecate_static_value.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
Synvert::Rewriter.new 'factory_bot', 'deprecate_static_value' do
description <<-EOF
It deprecates factory_bot static value

FactoryBot.define do
factory :post do
user
association :user
title "Something"
comments_count 0
tag Tag::MAGIC
recent_statuses []
status([:draft, :published].sample)
published_at 1.day.from_now
created_at(1.day.ago)
updated_at Time.current
update_times [Time.current]
meta_tags(foo: Time.current)
other_tags({ foo: Time.current })
options color: :blue
trait :old do
published_at 1.week.ago
description <<~EOF
It deprecates factory_bot static value

FactoryBot.define do
factory :post do
user
association :user
title "Something"
comments_count 0
tag Tag::MAGIC
recent_statuses []
status([:draft, :published].sample)
published_at 1.day.from_now
created_at(1.day.ago)
updated_at Time.current
update_times [Time.current]
meta_tags(foo: Time.current)
other_tags({ foo: Time.current })
options color: :blue
trait :old do
published_at 1.week.ago
end
end
end
end
end
=>
FactoryBot.define do
factory :post do
user
association :user
title { "Something" }
comments_count { 0 }
tag { Tag::MAGIC }
recent_statuses { [] }
status { [:draft, :published].sample }
published_at { 1.day.from_now }
created_at { 1.day.ago }
updated_at { Time.current }
update_times { [Time.current] }
meta_tags { { foo: Time.current } }
other_tags { { foo: Time.current } }
options { { color: :blue } }
trait :old do
published_at { 1.week.ago }
=>
FactoryBot.define do
factory :post do
user
association :user
title { "Something" }
comments_count { 0 }
tag { Tag::MAGIC }
recent_statuses { [] }
status { [:draft, :published].sample }
published_at { 1.day.from_now }
created_at { 1.day.ago }
updated_at { Time.current }
update_times { [Time.current] }
meta_tags { { foo: Time.current } }
other_tags { { foo: Time.current } }
options { { color: :blue } }
trait :old do
published_at { 1.week.ago }
end
end
end
end
end
EOF

within_files '{test,spec}/factories/**/*.rb' do
Expand Down
76 changes: 38 additions & 38 deletions lib/factory_girl/fix_deprecations.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
Synvert::Rewriter.new 'factory_girl', 'fix_deprecations' do
description <<-EOF
It converts deprecations

Factory

Factory.sequence :login do |n|
"new_user_\#{n}"
end
Factory.define :user do |user|
user.admin true
user.login { Factory.next(:login) }
user.sequence(:email) { |n| "user\#{n}@gmail.com" }
user.after_create { |instance| create_list(:post, 5, user: instance) }
end

=>

FactoryGirl.define do
sequence :user do |n|
"new_user_\#{n}"
end
factory :user do |user|
admin true
login { generate(:login) }
sequence(:email) { |n| "user\#{n}@gmail.com" }
after(:create) { |instance| create_list(:post, 5, user: instance) }
end
end

Test

Factory(:user) => create(:user)
Factory.next(:email) => generate(:email)
Factory.stub(:comment) => build_stubbed(:comment)
Factory.create(:user) => create(:user)
Factory.build(:use) => build(:user)
Factory.attributes_for(:user) => attributes_for(:user)

description <<~EOF
It converts deprecations
Factory
Factory.sequence :login do |n|
"new_user_\#{n}"
end
Factory.define :user do |user|
user.admin true
user.login { Factory.next(:login) }
user.sequence(:email) { |n| "user\#{n}@gmail.com" }
user.after_create { |instance| create_list(:post, 5, user: instance) }
end
=>
FactoryGirl.define do
sequence :user do |n|
"new_user_\#{n}"
end
factory :user do |user|
admin true
login { generate(:login) }
sequence(:email) { |n| "user\#{n}@gmail.com" }
after(:create) { |instance| create_list(:post, 5, user: instance) }
end
end
Test
Factory(:user) => create(:user)
Factory.next(:email) => generate(:email)
Factory.stub(:comment) => build_stubbed(:comment)
Factory.create(:user) => create(:user)
Factory.build(:use) => build(:user)
Factory.attributes_for(:user) => attributes_for(:user)
EOF

if_gem 'factory_girl', { gte: '2.0.0' }
Expand Down
96 changes: 48 additions & 48 deletions lib/factory_girl/use_short_syntax.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
Synvert::Rewriter.new 'factory_girl', 'use_short_syntax' do
description <<-EOF
Uses FactoryGirl short syntax.

1. it adds FactoryGirl::Syntax::Methods module to RSpec, Test::Unit, Cucumber, Spainach, MiniTest, MiniTest::Spec, minitest-rails.

# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

# Test::Unit
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
end

# Cucumber
World(FactoryGirl::Syntax::Methods)

# Spinach
class Spinach::FeatureSteps
include FactoryGirl::Syntax::Methods
end

# MiniTest
class MiniTest::Unit::TestCase
include FactoryGirl::Syntax::Methods
end

# MiniTest::Spec
class MiniTest::Spec
include FactoryGirl::Syntax::Methods
end

# minitest-rails
class MiniTest::Rails::ActiveSupport::TestCase
include FactoryGirl::Syntax::Methods
end

2. it converts to short syntax.

FactoryGirl.create(...) => create(...)
FactoryGirl.build(...) => build(...)
FactoryGirl.attributes_for(...) => attributes_for(...)
FactoryGirl.build_stubbed(...) => build_stubbed(...)
FactoryGirl.create_list(...) => create_list(...)
FactoryGirl.build_list(...) => build_list(...)
FactoryGirl.create_pair(...) => create_pair(...)
FactoryGirl.build_pair(...) => build_pair(...)
description <<~EOF
Uses FactoryGirl short syntax.
1. it adds FactoryGirl::Syntax::Methods module to RSpec, Test::Unit, Cucumber, Spainach, MiniTest, MiniTest::Spec, minitest-rails.
# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# Test::Unit
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
end
# Cucumber
World(FactoryGirl::Syntax::Methods)
# Spinach
class Spinach::FeatureSteps
include FactoryGirl::Syntax::Methods
end
# MiniTest
class MiniTest::Unit::TestCase
include FactoryGirl::Syntax::Methods
end
# MiniTest::Spec
class MiniTest::Spec
include FactoryGirl::Syntax::Methods
end
# minitest-rails
class MiniTest::Rails::ActiveSupport::TestCase
include FactoryGirl::Syntax::Methods
end
2. it converts to short syntax.
FactoryGirl.create(...) => create(...)
FactoryGirl.build(...) => build(...)
FactoryGirl.attributes_for(...) => attributes_for(...)
FactoryGirl.build_stubbed(...) => build_stubbed(...)
FactoryGirl.create_list(...) => create_list(...)
FactoryGirl.build_list(...) => build_list(...)
FactoryGirl.create_pair(...) => create_pair(...)
FactoryGirl.build_pair(...) => build_pair(...)
EOF

if_gem 'factory_girl', { gte: '2.0.0' }
Expand Down
20 changes: 10 additions & 10 deletions lib/rails/add_active_record_migration_rails_version.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Synvert::Rewriter.new 'rails', 'add_active_record_migration_rails_version' do
description <<-EOF
It adds default ActiveRecord::Migration rails version.

class CreateUsers < ActiveRecord::Migration
end

=>

class CreateUsers < ActiveRecord::Migration[4.2]
end
description <<~EOF
It adds default ActiveRecord::Migration rails version.
class CreateUsers < ActiveRecord::Migration
end
=>
class CreateUsers < ActiveRecord::Migration[4.2]
end
EOF

if_gem 'rails', { gte: '5.0.0' }
Expand Down
Loading