Synvert

logo

write snippet code to rewrite your source code.

Official Snippets

bullet

rename_whitelist_to_safelist

It renames bullet whitelist to safelist.

Bullet.add_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
Bullet.delete_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :team)
Bullet.get_whitelist_associations(:n_plus_one_query, 'Klass')
Bullet.reset_whitelist
Bullet.clear_whitelist

=>

Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
Bullet.delete_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :team)
Bullet.get_safelist_associations(:n_plus_one_query, 'Klass')
Bullet.reset_safelist
Bullet.clear_safelist

debug_me

remove_debug_me_calls

It removes debug_me calls.

debug_me A tool to print the labeled value of variables. |__ http://github.com/MadBomber/debug_me

default

check_syntax

Just used to check if there is a syntax error.

factory_bot

convert_factory_girl_to_factory_bot

It converts FactoryGirl to FactoryBot

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
FactoryGirl.create(:user)
FactoryGirl.build(:user)

=>

FactoryBot.create(:user)
FactoryBot.build(:user)
deprecate_static_value

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

=>

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
use_short_syntax

Uses FactoryBot short syntax.

  1. it adds FactoryBot::Syntax::Methods module to RSpec, Test::Unit, Cucumber, Spainach, MiniTest, MiniTest::Spec, minitest-rails.
# rspec
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

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

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

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

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

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

# minitest-rails
class MiniTest::Rails::ActiveSupport::TestCase
  include FactoryBot::Syntax::Methods
end
  1. it converts to short syntax.
FactoryBot.create(...)
FactoryBot.build(...)
FactoryBot.attributes_for(...)
FactoryBot.build_stubbed(...)
FactoryBot.create_list(...)
FactoryBot.build_list(...)
FactoryBot.create_pair(...)
FactoryBot.build_pair(...)

=>

create(...)
build(...)
attributes_for(...)
build_stubbed(...)
create_list(...)
build_list(...)
create_pair(...)
build_pair(...)
use_string_as_class_name

It uses string as class name

FactoryBot.define do
  factory :admin, class: User do
    name { 'Admin' }
  end
end

=>

FactoryBot.define do
  factory :admin, class: 'User' do
    name { 'Admin' }
  end
end

factory_girl

fix_2_0_deprecations

It fixes factory girl 2.0 deprecations

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
Factory(:user)
Factory.next(:email)
Factory.stub(:comment)
Factory.create(:user)
Factory.build(:use)
Factory.attributes_for(:user)

=>

create(:user)
generate(:email)
build_stubbed(:comment)
create(:user)
build(:user)
attributes_for(:user)
use_new_syntax

It uses factory_girl new syntax.

use_short_syntax

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
  1. it converts to short syntax.
FactoryGirl.create(...)
FactoryGirl.build(...)
FactoryGirl.attributes_for(...)
FactoryGirl.build_stubbed(...)
FactoryGirl.create_list(...)
FactoryGirl.build_list(...)
FactoryGirl.create_pair(...)
FactoryGirl.build_pair(...)

=>

create(...)
build(...)
attributes_for(...)
build_stubbed(...)
create_list(...)
build_list(...)
create_pair(...)
build_pair(...)

minitest

assert_empty

Use assert_empty if expecting object to be empty.

assert(object.empty?)

=>

assert_empty(object)
assert_equal_arguments_order

assert_equal should always have expected value as first argument because if the assertion fails the error message would say expected “rubocop-minitest” received “rubocop” not the other way around.

assert_equal(actual, "rubocop-minitest")

=>

assert_equal("rubocop-minitest", actual)
assert_includes

Use assert_includes to assert if the object is included in the collection.

assert(collection.include?(object))

=>

assert_includes(collection, object)
assert_instance_of

Prefer assert_instance_of(class, object) over assert(object.instance_of?(class)).

assert('rubocop-minitest'.instance_of?(String))

=>

assert_instance_of(String, 'rubocop-minitest')
assert_kind_of

Prefer assert_kind_of(class, object) over assert(object.kind_of?(class)).

assert('rubocop-minitest'.kind_of?(String))

=>

assert_kind_of(String, 'rubocop-minitest')
assert_match

Use assert_match if expecting matcher regex to match actual object.

assert(pattern.match?(object))

=>

assert_match(pattern, object)
assert_nil

Use assert_nil if expecting nil.

assert_equal(nil, actual)

=>

assert_nil(actual)
assert_operator

Use assert_operator if comparing expected and actual object using operator.

assert(expected < actual)

=>

assert_operator(expected, :<, actual)
assert_path_exists

Use assert_path_exists if expecting path to exist.

assert(File.exist?(path))

=>

assert_path_exists(path)
assert_predicate

Use assert_predicate if expecting to test the predicate on the expected object and on applying predicate returns true. The benefit of using the assert_predicate over the assert or assert_equal is the user friendly error message when assertion fails.

assert expected.zero?
assert_equal 0, expected

=>

assert_predicate expected, :zero?
assert_predicate expected, :zero?
assert_respond_to

Use assert_respond_to if expecting object to respond to a method.

assert(object.respond_to?(some_method))

=>

assert_respond_to(object, some_method)
assert_silent

Use assert_silent to assert that nothing was written to stdout and stderr.

assert_output('', '') { puts object.do_something }

=>

assert_silent { puts object.do_something }
assert_truthy

Use assert if expecting truthy value.

assert_equal(true, actual)

=>

assert(actual)
better_syntax

It converts rspec code to better syntax, it calls all minitest sub snippets.

hooks_super

If using a module containing setup or teardown methods, be sure to call super in the test class setup or teardown.

class TestMeme < Minitest::Test
  include MyHelper

  def setup
    do_something
  end

  def teardown
    clean_something
  end
end

=>

class TestMeme < Minitest::Test
  include MyHelper

  def setup
    super
    do_something
  end

  def teardown
    clean_something
    super
  end
end
refute_empty

Use refute_empty if expecting object to be not empty.

refute(object.empty?)
assert(!object.empty?)

=>

refute_empty(object)
refute_empty(object)
refute_equal

Use refute_equal` if expected and actual should not be same.

assert("rubocop-minitest" != actual)
assert(!"rubocop-minitest" == actual)

=>

refute_equal("rubocop-minitest", actual)
refute_equal("rubocop-minitest", actual)
refute_false

Use refute if expecting false.

assert_equal(false, actual)
assert(!something)

=>

refute(actual)
refute(something)
refute_includes

Use refute_includes if the object is not included in the collection.

refute(collection.include?(object))
assert(!collection.include?(object))

=>

refute_includes(collection, object)
refute_includes(collection, object)
refute_instance_of

Prefer refute_instance_of(class, object) over refute(object.instance_of?(class)).

assert(!'rubocop-minitest'.instance_of?(String))
refute('rubocop-minitest'.instance_of?(String))

=>

refute_instance_of(String, 'rubocop-minitest')
refute_instance_of(String, 'rubocop-minitest')
refute_kind_of

Prefer refute_kind_of(class, object) over refute(object.kind_of?(class)).

assert(!'rubocop-minitest'.kind_of?(String))
refute('rubocop-minitest'.kind_of?(String))

=>

refute_kind_of(String, 'rubocop-minitest')
refute_kind_of(String, 'rubocop-minitest')
refute_match

Use refute_match if expecting matcher regex to not match actual object.

assert(!pattern.match?(object))
refute(pattern.match?(object))

=>

refute_match(pattern, object)
refute_match(pattern, object)
refute_nil

Use refute_nil if not expecting nil.

assert(!actual.nil?)
refute(actual.nil?)

=>

refute_nil(actual)
refute_nil(actual)
refute_operator

Use refute_operator if expecting expected object is not binary operator of the actual object. Assertion passes if the expected object is not binary operator(example: greater than) the actual object.

refute(expected > actual)
assert(!(expected > actual))

=>

refute_operator(expected, :>, actual)
refute_operator(expected, :>, actual)
refute_path_exists

Use refute_path_exists if expecting path to not exist.

assert(!File.exist?(path))
refute(File.exist?(path))

=>

refute_path_exists(path)
refute_path_exists(path)
refute_predicate

Use refute_predicate if expecting to test the predicate on the expected object and on applying predicate returns false.

assert(!expected.zero?)
refute(expected.zero?)

=>

refute_predicate(expected, :zero?)
refute_predicate(expected, :zero?)
refute_respond_to

Use refute_respond_to if expecting object to not respond to a method.

assert(!object.respond_to?(some_method))
refute(object.respond_to?(some_method))

=>

refute_respond_to(object, some_method)
refute_respond_to(object, some_method)

rails

add_active_record_migration_rails_version

It adds default ActiveRecord::Migration rails version.

class CreateUsers < ActiveRecord::Migration
end

=>

class CreateUsers < ActiveRecord::Migration[4.2]
end
add_application_job

It adds ApplicationJob

  1. it adds app/models/application_job.rb file.

  2. it replaces ActiveJob::Base with ApplicationJob in job files.

class PostJob < ActiveJob::Base
end

=>

class PostJob < ApplicationJob
end
add_application_mailer

It adds ApplicationMailer

  1. it adds app/mailers/application_mailer.rb file.

  2. it replaces ActionMailer::Base with ApplicationMailer in mailer files.

class UserMailer < ActionMailer::Base
end

=>

class UserMailer < ApplicationMailer
end
add_application_record

It adds ApplicationRecord

  1. it adds app/models/application_record.rb file.
  2. it replaces ActiveRecord::Base with ApplicationRecord in model files.
class Post < ActiveRecord::Base
end

=>

class Post < ApplicationRecord
end
convert_active_record_dirty_5_0_to_5_1

It converts ActiveRecord::Dirty 5.0 to 5.1

class Post < ActiveRecord::Base
  before_create :call_before_create
  before_update :call_before_update, if: :title_changed?
  before_save :call_before_save, if: -> { status_changed? || summary_changed? }
  after_create :call_after_create
  after_update :call_after_update, if: :title_changed?
  after_save :call_after_save, if: -> { status_changed? || summary_changed? }

  def call_before_create
    if title_changed?
      changes
    end
  end

  def call_after_create
    if title_changed?
      changes
    end
  end
end

=>

class Post < ActiveRecord::Base
  before_create :call_before_create
  before_update :call_before_update, if: :will_save_change_to_title?
  before_save :call_before_save, if: -> { will_save_change_to_status? || will_save_change_to_summary? }
  after_create :call_after_create
  after_update :call_after_update, if: :saved_change_to_title?
  after_save :call_after_save, if: -> { saved_change_to_status? || saved_change_to_summary? }

  def call_before_create
    if will_save_change_to_title?
      changes_to_save
    end
  end

  def call_after_create
    if saved_change_to_title?
      saved_changes
    end
  end
end
convert_after_commit

It converts active_record after_commit.

after_commit :add_to_index_later, on: :create
after_commit :update_in_index_later, on: :update
after_commit :remove_from_index_later, on: :destroy

=>

after_create_commit :add_to_index_later
after_update_commit :update_in_index_later
after_detroy_commit :remove_from_index_later
convert_controller_filter_to_action

It renames before_filter callbacks to before_action

class PostsController < ApplicationController
  skip_filter :authorize
  before_filter :load_post
  after_filter :track_post
  around_filter :log_post
end

=>

class PostsController < ApplicationController
  skip_action_callback :authorize
  before_action :load_post
  after_action :track_post
  around_action :log_post
end
convert_dynamic_finders

It converts rails dynamic finders to arel syntax.

find_all_by_...
find_by_...
find_last_by_...
scoped_by_...
find_or_initialize_by_...
find_or_create_by_...

=>

where(...)
where(...).first
where(...).last
where(...)
find_or_initialize_by(...)
find_or_create_by(...)
convert_env_to_request_env

It replaces env with request.env in controller files.

env["omniauth.auth"]

=>

request.env["omniauth.auth"]
convert_head_response

It replaces render head response in controller files.

render nothing: true
render nothing: true, status: :created

head status: 406
head location: '/foo'

=>

head :ok
head :created

head 406
head :ok, location: '/foo'
convert_mailers_2_3_to_3_0

It converts rails mailers from 2.3 to 3.0.

class Notifier < ActionMailer::Base
  def signup_notification(recipient)
    recipients      recipient.email_address_with_name
    subject         "New account information"
    from            "system@example.com"
    sent_on         Time.now
    content_type    "multipart/alternative"
    body            :account => recipient
  end
end

=>

class Notifier < ActionMailer::Base
  def signup_notification(recipient)
    @account = recipient
    mail(:to => recipient.email_address_with_name, :subject => "New account information", :from => "system@example.com", :date => Time.now)
  end
end
Notifier.deliver_signup_notification(recipient)

=>

Notifier.signup_notification(recipient).deliver
message = Notifier.create_signup_notification(recipient)
Notifier.deliver(message)

=>

message = Notifier.signup_notification(recipient)
message.deliver
convert_model_errors_add

It converts to activerecord errors.add.

errors[:base] = "author not present"
self.errors[:base] = "author not present"

=>

errors.add(:base, "author not present")
self.errors.add(:base, "author not present")
convert_model_lambda_scope

It converts activerecord scope to lambda scope.

class Post < ActiveRecord::Base
  scope :active, where(active: true)
  scope :published, Proc.new { where(published: true) }
  scope :by_user, proc { |user_id| where(user_id: user_id) }
  default_scope order("updated_at DESC")
  default_scope { order("created_at DESC") }
end

=>

class Post < ActiveRecord::Base
  scope :active, -> { where(active: true) }
  scope :published, -> { where(published: true) }
  scope :by_user, ->(user_id) { where(user_id: user_id) }
  default_scope -> { order("updated_at DESC") }
  default_scope -> { order("created_at DESC") }
end
convert_models_2_3_to_3_0

It converts rails models from 2.3 to 3.0.

named_scope :active, :conditions => {:active => true}, :order => "created_at desc"
named_scope :my_active, lambda { |user| {:conditions => ["user_id = ? and active = ?", user.id, true], :order => "created_at desc"} }

=>

scope :active, where(:active => true).order("created_at desc")
scope :my_active, lambda { |user| where("user_id = ? and active = ?", user.id, true).order("created_at desc") }
default_scope :order => "id DESC"

=>

default_scope order("id DESC")
Post.find(:all, :limit => 2)
Post.find(:all)
Post.find(:first)
Post.find(:last, :conditions => {:title => "test"})
Post.first(:conditions => {:title => "test"})
Post.all(:joins => :comments)

=>

Post.limit(2)
Post.all
Post.first
Post.where(:title => "test").last
Post.where(:title => "test").first
Post.joins(:comments)
Post.find_in_batches(:conditions => {:title => "test"}, :batch_size => 100) do |posts|
end
Post.find_in_batches(:conditions => {:title => "test"}) do |posts|
end

=>

Post.where(:title => "test").find_each(:batch_size => 100) do |post|
end
Post.where(:title => "test").find_each do |post|
end
with_scope(:find => {:conditions => {:active => true}}) { Post.first }
with_exclusive_scope(:find => {:limit =>1}) { Post.last }

=>

with_scope(where(:active => true)) { Post.first }
with_exclusive_scope(limit(1)) { Post.last }
Client.count("age", :conditions => {:active => true})
Client.average("orders_count", :conditions => {:active => true})
Client.min("age", :conditions => {:active => true})
Client.max("age", :conditions => {:active => true})
Client.sum("orders_count", :conditions => {:active => true})

=>

Client.where(:active => true).count("age")
Client.where(:active => true).average("orders_count")
Client.where(:active => true).min("age")
Client.where(:active => true).max("age")
Client.where(:active => true).sum("orders_count")
self.errors.on(:email).present?

=>

self.errors[:email].present?
self.errors.add_to_base("error message")

=>

self.errors.add(:base, "error message")
self.save(false)

=>

self.save(:validate => false)
Post.update_all({:title => "title"}, {:title => "test"})
Post.update_all("title = 'title'", "title = 'test'")
Post.update_all("title = 'title'", ["title = ?", title])
Post.update_all({:title => "title"}, {:title => "test"}, {:limit => 2})

=>

Post.where(:title => "test").update_all(:title => "title")
Post.where("title = 'test'").update_all("title = 'title'")
Post.where(["title = ?", title]).update_all("title = 'title'")
Post.where(:title => "test").limit(2).update_all(:title => "title")
Post.delete_all("title = 'test'")
Post.delete_all(["title = ?", title])

=>

Post.where("title = 'test'").delete_all
Post.where(["title = ?", title]).delete_all
Post.destroy_all("title = 'test'")
Post.destroy_all(["title = ?", title])

=>

Post.where("title = 'test'").destroy_all
Post.where(["title = ?", title]).destroy_all
convert_rails_env

It converts RAILS_ENV to Rails.env.

RAILS_ENV
"#{RAILS_ENV}"
RAILS_ENV == 'development'
'development' == RAILS_ENV
RAILS_ENV != 'development'
'development' != RAILS_ENV

=>

Rails.env
"#{Rails.env}"
Rails.env.development?
Rails.env.development?
!Rails.env.development?
!Rails.env.development?
convert_rails_logger

It converts RAILS_DEFAULT_LOGGER to Rails.logger.

RAILS_DEFAULT_LOGGER

=>

Rails.logger
convert_rails_root

It converts RAILS_ROOT to Rails.root.

RAILS_ROOT
File.join(RAILS_ROOT, 'config/database.yml')
RAILS_ROOT + 'config/database.yml'
"#{RAILS_ROOT}/config/database.yml"
File.exists?(Rails.root.join('config/database.yml'))

=>

Rails.root
Rails.root.join('config/database.yml')
Rails.root.join('config/database.yml')
Rails.root.join('config/database.yml')
Rails.root.join('config/database.yml').exist?
convert_rails_test_request_methods_4_2_to_5_0

It converts rails test request methods from 4.2 to 5.0

functional test:

get :show, { id: user.id, format: :json }, { admin: user.admin? }, { notice: 'Welcome' }

=>

get :show, params: { id: user.id }, session: { admin: user.admin? }, flash: { notice: 'Welcome' }, as: :json

integration test:

get '/posts/1', user_id: user.id, { 'HTTP_AUTHORIZATION' => 'fake' }

=>

get '/posts/1', params: { user_id: user.id }, headers: { 'HTTP_AUTHORIZATION' => 'fake' }
convert_render_text_to_render_plain

It convert render :text to render :plain

render text: 'OK'

=>

render plain: 'OK'
convert_routes_2_3_to_3_0

It converts rails routes from 2.3 to 3.0.

map.root :controller => "home", :action => :index

=>

root :to => "home#index"
map.connect "/:controller/:action/:id"

=>

match "/:controller(/:action(/:id))(.:format)"
map.admin_signup "/admin_signup", :controller => "admin_signup", :action => "new", :method => "post"

=>

post "/admin_signup", :to => "admin_signup#new", :as => "admin_signup"
map.with_options :controller => "manage" do |manage|
  manage.manage_index "manage_index", :action => "index"
  manage.manage_intro "manage_intro", :action => "intro"
end

=>

manage.manage_index "manage_index", :to => "index#manage"
manage.manage_intro "manage_intro", :to => "intro#manage"
map.namespace :admin do |admin|
  admin.resources :users
end

=>

namespace :admin do
  resources :users
end
map.resources :posts, :collection => { :generate_pdf => :get }, :member => {:activate => :post} do |posts|
  posts.resources :comments
end

=>

resources :posts do
  collection do
    get :generate_pdf
  end
  member do
    post :activate
  end
  resources :comments
end
convert_to_response_parsed_body

It converts JSON.parse(@response.body) to @response.parsed_body.

convert_update_attributes_to_update

It converts .update_attributes to .update

user.update_attributes(title: 'new')
user.update_attributes!(title: 'new')

=>

user.update(title: 'new')
user.update!(title: 'new')
convert_views_2_3_to_3_0
  1. remove h helper, rails 3 uses it by default.
<%= h user.login %>

=>

<%= user.login %>
  1. use erb expression instead of erb statement for view helpers.
<% form_for post do |f| %>
<% end %>

=>

<%= form_for post do |f| %>
<% end %>
fix_4_0_deprecations

It fixes rails 4.0 deprecations.

ActiveRecord::Fixtures
ActiveRecord::TestCase
ActionController::Integration
ActionController::IntegrationTest
ActionController::PerformanceTest
ActionController::AbstractRequest
ActionController::Request
ActionController::AbstractResponse
ActionController::Response
ActionController::Routing

=>

ActiveRecord::FixtureSet
ActiveSupport::TestCase
ActionDispatch::Integration
ActionDispatch::IntegrationTest
ActionDispatch::PerformanceTest
ActionDispatch::Request
ActionDispatch::Request
ActionDispatch::Response
ActionDispatch::Response
ActionDispatch::Routing
fix_controller_3_2_deprecations

It fixes rails 3.2 controller deprecations.

ActionController::UnknownAction

=>

AbstractController::ActionNotFound
ActionController::DoubleRenderError

=>

AbstractController::DoubleRenderError
fix_model_3_2_deprecations

It fixes rails 3.2 model deprecations.

set_table_name "project"

=>

self.table_name = "project"
set_inheritance_column = "type"

=>

self.inheritance_column = "type"
set_sequence_name = "seq"

=>

self.sequence_name = "seq"
set_primary_key = "id"

=>

self.primary_key = "id"
set_locking_column = "lock"

=>

self.locking_column = "lock"
redirect_with_flash

Fold flash setting into redirect_to.

  flash[:notice] = "huzzah"
  redirect_to root_path

=>

  redirect_to root_path, notice: "huzzah"

and

  flash[:error] = "booo"
  redirect_to root_path

=>

  redirect_to root_path, flash: {error: "huzzah"}
strong_parameters

It uses string_parameters to replace attr_accessible.

  1. it removes active_record configurations.
config.active_record.whitelist_attributes = ...
config.active_record.mass_assignment_sanitizer = ...
  1. it removes attr_accessible and attr_protected code in models.

  2. it adds xxx_params in controllers

def xxx_params
  params.require(:xxx).permit(...)
end
  1. it replaces params[:xxx] with xxx_params.
params[:xxx] => xxx_params
upgrade_2_3_to_3_0

It converts rails from 2.3 to 3.0.

upgrade_3_0_to_3_1

It upgrade rails from 3.0 to 3.1.

  1. it enables asset pipeline.
config.assets.enabled = true
config.assets.version = '1.0'
  1. it removes config.action_view.debug_rjs in config/environments/development.rb

  2. it adds asset pipeline configs in config/environments/development.rb

# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true
  1. it adds asset pipeline configs in config/environments/production.rb
# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs
config.assets.digest = true
  1. it adds asset pipeline configs in config/environments/test.rb
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
  1. it creates config/environments/wrap_parameters.rb.

  2. it replaces session_store in config/initializers/session_store.rb

Application.session_store :cookie_store, key: '_xxx-session'
  1. Migrations now use instance methods rather than class methods
def self.up
end

def self.down
end

=>

def up
end

def down
end
upgrade_3_1_to_3_2

It upgrades rails from 3.1 to 3.2.

  1. it inserts new configs in config/environments/development.rb.
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
  1. it inserts new configs in config/environments/test.rb.
config.active_record.mass_assignment_sanitizer = :strict
upgrade_3_2_to_4_0

It upgrades rails from 3.2 to 4.0.

  1. it removes assets group in config/application.rb.
if defined?(Bundler)
  Bundler.require(*Rails.groups(:assets => %w(development test)))
end

=>

Bundler.require(:default, Rails.env)
  1. it removes config.active_record.identity_map = true from config files. it removes config.active_record.auto_explain_threshold_in_seconds = 0.5 from config files.

  2. it changes config.assets.compress = ... to config.assets.js_compressor = ...

  3. it removes include_root_in_json from config/initializers/wrap_parameters.rb.

  4. it inserts secret_key_base to config/initializers/secret_token.rb.

Application.config.secret_key_base = '...'
  1. it removes config.action_dispatch.best_standards_support = ... from config files.

  2. it inserts config.eager_load = true in config/environments/production.rb.

  3. it inserts config.eager_load = false in config/environments/development.rb.

  4. it inserts config.eager_load = false in config/environments/test.rb.

  5. it removes any code using ActionDispatch::BestStandardsSupport in config files.

  6. it replaces ActionController::Base.page_cache_extension = ... with ActionController::Base.default_static_extension = ... in config files.

  7. it removes Rack::Utils.escape in config/routes.rb.

Rack::Utils.escape('こんにちは') => 'こんにちは'
  1. it replaces match in config/routes.rb.
match "/" => "root#index"

=>

get "/" => "root#index"
  1. it replaces instance method serialized_attributes with class method.
self.serialized_attributes

=>

self.class.serialized_attributes
  1. it replaces dependent: :restrict to dependent: :restrict_with_exception.

  2. it removes config.whiny_nils = true.

  3. it replaces

link_to 'delete', post_path(post), confirm: 'Are you sure to delete post?'

=>

link_to 'delete', post_path(post), data: { confirm: 'Are you sure to delete post?' }
upgrade_4_0_to_4_1

It upgrades rails from 4.0 to 4.1.

  1. config/secrets.yml Create a secrets.yml file in your config folder Copy the existing secret_key_base from the secret_token.rb initializer to secrets.yml under the production section. Remove the secret_token.rb initializer

  2. remove ActiveRecord::Migration.check_pending! in test/test_helper.rb, and add require 'test_help'

  3. add config/initializers/cookies_serializer.rb

  4. replace MultiJson.dump with obj.to_json, replace MultiJson.load with JSON.parse(str)

  5. warn return within inline callback blocks before_save { return false }

upgrade_4_1_to_4_2

It upgrades rails from 4.1 to 4.2

  1. it replaces config.serve_static_assets = ... with config.serve_static_files = ... in config files.

  2. it inserts config.active_record.raise_in_transactional_callbacks = true in config/application.rb

upgrade_4_2_to_5_0

It upgrades rails 4.2 to 5.0

  1. it replaces config.static_cache_control = ... with config.public_file_server.headers = ... in config files.

  2. it replaces config.serve_static_files = ... with config.public_file_server.enabled = ... in config files.

  3. it replaces middleware.use "Foo::Bar" with middleware.use Foo::Bar in config files.

  4. it replaces MissingSourceFile with LoadError.

  5. it adds config/initializers/new_framework_defaults.rb.

  6. it removes raise_in_transactional_callbacks= in config/application.rb.

upgrade_5_0_to_5_1

It upgrades rails 5.0 to 5.1.

  1. it replaces HashWithIndifferentAccess with ActiveSupport::HashWithIndifferentAccess.

  2. it replaces Rails.application.config.secrets[:smtp_settings]["address"] with Rails.application.config.secrets[:smtp_settings][:address]

upgrade_5_1_to_5_2

It upgrades rails 5.1 to 5.2

  1. it replaces dalli_store with mem_cache_store
upgrade_5_2_to_6_0
It upgrades rails 5.2 to 6.0
use_migrations_instance_methods

It uses instance methods rather than class methods in migrations.

def self.up
end

def self.down
end

=>

def up
end

def down
end

rspec

be_close_to_be_within

It converts rspec be_close matcher to be_within matcher.

expect(1.0 / 3.0).to be_close(0.333, 0.001)

=>

expect(1.0 / 3.0).to be_within(0.001).of(0.333)
block_to_expect

It converts rspec block to expect.

lambda { do_something }.should raise_error
proc { do_something }.should raise_error
-> { do_something }.should raise_error

=>

expect { do_something }.to raise_error
expect { do_something }.to raise_error
expect { do_something }.to raise_error
boolean_matcher

It converts rspec boolean matcher.

be_true
be_false

=>

be_truthy
be_falsey
collection_matcher

It converts rspec collection matcher.

expect(collection).to have(3).items
expect(collection).to have_exactly(3).items
expect(collection).to have_at_least(3).items
expect(collection).to have_at_most(3).items

expect(team).to have(3).players

=>

expect(collection.size).to eq(3)
expect(collection.size).to eq(3)
expect(collection.size).to be >= 3
expect(collection.size).to be <= 3

expect(team.players.size).to eq 3
custom_matcher_new_syntax

It uses RSpec::Matchers new syntax.

RSpec::Matchers.define :be_awesome do
  match_for_should { }
  match_for_should_not { }
  failure_message_for_should { }
  failure_message_for_should_not { }
end

=>

RSpec::Matchers.define :be_awesome do
  match { }
  match_when_negated { }
  failure_message { }
  failure_message_when_negated { }
end
explicit_spec_type

It explicits spec type.

RSpec.configure do |rspec|
end

=>

RSpec.configure do |rspec|
  rspec.infer_spec_type_from_file_location!
end
describe SomeModel do
end

=>

describe SomeModel, :type => :model do
end
its_to_it

It converts rspec its to it.

describe 'example' do
  subject { { foo: 1, bar: 2 } }
  its(:size) { should == 2 }
  its([:foo]) { should == 1 }
  its('keys.first') { should == :foo }
end

=>

describe 'example' do
  subject { { foo: 1, bar: 2 } }

  describe '#size' do
    subject { super().size }
    it { should == 2 }
  end

  describe '[:foo]' do
    subject { super()[:foo] }
    it { should == 1 }
  end

  describe '#keys' do
    subject { super().keys }
    describe '#first' do
      subject { super().first }
      it { should == :foo }
    end
  end
end
message_expectation

It convert rspec message expectation.

obj.should_receive(:message)
Klass.any_instance.should_receive(:message)

expect(obj).to receive(:message).and_return { 1 }

expect(obj).to receive(:message).and_return

=>

expect(obj).to receive(:message)
expect_any_instance_of(Klass).to receive(:message)

expect(obj).to receive(:message) { 1 }

expect(obj).to receive(:message)
method_stub

It converts rspec method stub.

obj.stub!(:message)
obj.unstub!(:message)

obj.stub(:message).any_number_of_times
obj.stub(:message).at_least(0)

obj.stub(:message)
Klass.any_instance.stub(:message)

obj.stub_chain(:foo, :bar, :baz)

obj.stub(:foo => 1, :bar => 2)

allow(obj).to receive(:message).and_return { 1 }

allow(obj).to receive(:message).and_return

=>

obj.stub(:message)
obj.unstub(:message)

allow(obj).to receive(:message)
allow(obj).to receive(:message)

allow(obj).to receive(:message)
allow_any_instance_of(Klass).to receive(:message)

allow(obj).to receive_message_chain(:foo, :bar, :baz)

allow(obj).to receive_messages(:foo => 1, :bar => 2)

allow(obj).to receive(:message) { 1 }

allow(obj).to receive(:message)
negative_error_expectation

It converts rspec negative error expectation.

expect { do_something }.not_to raise_error(SomeErrorClass)
expect { do_something }.not_to raise_error('message')
expect { do_something }.not_to raise_error(SomeErrorClass, 'message')

=>

expect { do_something }.not_to raise_error
expect { do_something }.not_to raise_error
expect { do_something }.not_to raise_error
new_config_options

It converts rspec configuration options.

  1. it removes config.treat_symbols_as_metadata_keys_with_true_values = true

  2. it converts

RSpec.configure do |c|
  c.backtrace_clean_patterns
  c.backtrace_clean_patterns = [/lib/something/]
  c.color_enabled = true

  c.out
  c.out = File.open('output.txt', 'w')
  c.output
  c.output = File.open('output.txt', 'w')

  c.backtrace_cleaner
  c.color?(output)
  c.filename_pattern
  c.filename_pattern = '**/*_test.rb'
  c.warnings
end

=>

RSpec.configure do |c|
  c.backtrace_exclusion_patterns
  c.backtrace_exclusion_patterns = [/lib/something/]
  c.color = true

  c.output_stream
  c.output_stream = File.open('output.txt', 'w')
  c.output_stream
  c.output_stream = File.open('output.txt', 'w')

  c.backtrace_formatter
  c.color_enabled?(output)
  c.pattern
  c.pattern = '**/*_test.rb'
  c.warnings?
end
new_hook_scope

It converts new hook scope.

before(:each) { do_something }
before(:all) { do_something }

=>

before(:example) { do_something }
before(:context) { do_something }
one_liner_expectation

It convers rspec one liner expectation.

it { should matcher }
it { should_not matcher }

it { should have(3).items }

it { should have_at_least(3).players }

=>

it { is_expected.to matcher }
it { is_expected.not_to matcher }

it 'has 3 items' do
  expect(subject.size).to eq(3)
end

it 'has at least 3 players' do
  expect(subject.players.size).to be >= 3
end
pending_to_skip

It converts rspec pending to skip.

it 'is skipped', :pending => true do
  do_something_possibly_fail
end

pending 'is skipped' do
  do_something_possibly_fail
end

it 'is skipped' do
  pending
  do_something_possibly_fail
end

it 'is run and expected to fail' do
  pending do
    do_something_surely_fail
  end
end

=>

it 'is skipped', :skip => true do
  do_something_possibly_fail
end

skip 'is skipped' do
  do_something_possibly_fail
end

it 'is skipped' do
  skip
  do_something_possibly_fail
end

it 'is run and expected to fail' do
  skip
  do_something_surely_fail
end
remove_monkey_patches

It removes monkey patching of the top level methods like describe

RSpec.configure do |rspec|
end

=>

RSpec.configure do |rspec|
  rspec.expose_dsl_globally = false
end
describe 'top-level example group' do
  describe 'nested example group' do
  end
end

=>

RSpec.describe 'top-level example group' do
  describe 'nested example group' do
  end
end
should_to_expect

It converts rspec should to expect.

obj.should matcher
obj.should_not matcher

1.should == 1
1.should < 1
Integer.should === 1

'string'.should =~ /^str/
[1, 2, 3].should =~ [2, 1, 3]

=>

expect(obj).to matcher
expect(obj).not_to matcher

expect(1).to eq 1
expect(1).to be < 2
expect(Integer).to be === 1

expect('string').to match /^str/
expect([1, 2, 3]).to match_array [2, 1, 3]
stub_and_mock_to_double

It converts stub and mock to double.

stub('something')
mock('something')

=>

double('something')
double('something')
use_new_syntax

It converts rspec code to new syntax, it calls all rspec sub snippets.

ruby

block_to_yield

It converts block to yield.

def slow(&block)
  block.call
end

=>

def slow
  yield
end
deprecate_big_decimal_new

It converts BigDecimal.new to BigDecimal

BigDecimal.new('1.1')

=>

BigDecimal('1.1')
fast_syntax

Use ruby fast syntax.

Reference: https://speakerdeck.com/sferik/writing-fast-ruby

gsub_to_tr

It converts String#gsub to String#tr

'slug from title'.gsub(' ', '_')

=>

'slug from title'.tr(' ', '_')
hash_short_syntax

Use ruby 3.1 hash short syntax.

some_method(a: a, b: b, c: c, d: d + 4)

=>

some_method(a:, b:, c:, d: d + 4)
hash_value_shorthand

Values in Hash literals and keyword arguments can be omitted.

{x: x, y: y}

foo(x: x, y: y)

=>

{x:, y:}

foo(x:, y:)
kernel_open_to_uri_open

It converts Kernel#open to URI.open

open('http://test.com')

=>

URI.open('http://test.com')
keys_each_to_each_key

It convert Hash#keys.each to Hash#each_key

params.keys.each {}

=>

params.each_key {}
map_and_flatten_to_flat_map

It converts map and flatten to flat_map

enum.map do
  # do something
end.flatten

=>

enum.flat_map do
  # do something
end
merge_to_square_brackets

It converts Hash#merge and Hash#merge! methods to Hash#[]=

enum.inject({}) do |h, e|
  h.merge(e => e)
end

=>

enum.inject({}) do |h, e|
  h[e] = e
  h
end
enum.inject({}) { |h, e| h.merge!(e => e) }

=>

enum.inject({}) { |h, e| h[e] = e; h }
enum.each_with_object({}) do |e, h|
  h.merge!(e => e)
end

=>

enum.each_with_object({}) do |e, h|
  h[e] = e
end
nested_class_definition

It converts compact class definition to nested class definition.

class Foo::Bar < Base
  def test; end
end

=>

module Foo
  class Bar < Base
    def test; end
  end
end
new_1_9_hash_syntax

Use ruby 1.9 new hash syntax.

{ :foo => 'bar' }

=>

{ foo: 'bar' }
new_2_2_hash_syntax

Use ruby 2.2 new hash syntax.

{ :foo => 'bar' }
{ :'foo-x' => 'bar' }
{ :"foo-#{suffix}" 'bar' }

=>

{ foo: 'bar' }
{ 'foo-x': 'bar' }
{ "foo-#{suffix}": 'bar' }
new_lambda_syntax

Use ruby new lambda syntax

lambda { # do some thing }

=>

-> { # do some thing }
new_safe_navigation_operator

Use ruby new safe navigation operator.

u.try!(:profile).try(:thumbnails).try(:large, 100, format: 'jpg')

=>

u&.profile&.thumbnails&.large(100, format: 'jpg')
numbered parameters

It uses numbered parameters.

squared_numbers = (1...10).map { |num| num ** 2 }

city_populations.each { |city, population| puts "Population of #{city} is #{population}" }

=>

squared_numbers = (1...10).map { _1 ** 2 }

city_populations.each { puts "Population of #{_1} is #{_2}" }
parallel_assignment_to_sequential_assignment

It converts parallel assignment to sequential assignment.

a, b = 1, 2

=>

a = 1
b = 2
prefer_dig

It prefers Hash#dig

params[one] && params[one][two] && params[one][two][three]

=>

params.dig(one, two, three)
remove_debug_code

It removes puts and p calls.

use_symbol_to_proc

It uses &: (short for symbol to proc)

(1..100).each { |i| i.to_s }
(1..100).map { |i| i.to_s }

=>

(1..100).each(&:to_s)
(1..100).map(&:to_s)

shoulda

fix_1_5_deprecations

It fixes shoulda 1.5 deprecations.

models:

should validate_format_of(:email).with('user@example.com')

=>

should allow_value('user@example.com').for(:email)

controllers:

should assign_to(:user)

should assign_to(:user) { @user }

should respond_with_content_type "application/json"

=>

should "assigns user" do
  assert_not_nil assigns(:user)
end

should "assigns user" do
  assert_equal @user, assigns(:user)
end

should "responds with application/json" do
  assert_equal "application/json", response.content_type
end
fix_2_6_deprecations

It fixes shoulda 2.6 deprecations.

should ensure_inclusion_of(:age).in_range(0..100)
should ensure_exclusion_of(:age).in_range(0..100)

=>

should validate_inclusion_of(:age).in_range(0..100)
should validate_exclusion_of(:age).in_range(0..100)
use_matcher_syntax

It converts shoulda macros to matcher syntax.

models:

should_belongs_to :user
should_have_one :category
should_have_many :comments
should_have_and_belong_to_many :tags

should_validate_presence_of :title, :body

should_validate_uniqueness_of :name, :message => 'O NOES! SOMEONE STOELED YER NAME!'
should_validate_numericality_of :age
should_validate_acceptance_of :eula

should_ensure_length_in_range :password, (6..20)
should_ensure_length_is :ssn, 9
should_ensure_value_in_range :age, (0..100)

should_allow_values_for :isbn, 'isbn 1 2345 6789 0', 'ISBN 1-2345-6789-0'
should_not_allow_values_for :isbn, "bad1", "bad 2"

should_allow_mass_assignment_of :first_name, :last_name
should_not_allow_mass_assignment_of :password, :admin_flag

should_have_readonly_attributes :password, :admin_flag

=>

should belong_to(:user)
should have_one(:category)
should have_many(:comments)
should have_and_belong_to_many(:tags)

should validate_presence_of(:title)
should validate_presence_of(:body)

should validate_uniqueness_of(:name).with_message('O NOES! SOMEONE STOELED YER NAME!')
should validate_numericality_of(:age)
should validate_acceptance_of(:eula)

should ensure_length_of(:password).is_at_least(6).is_at_most(20)
should ensure_length_of(:ssn).is_equal_to(9)
should ensure_inclusion_of(:age).in_range(0..100)

should allow_value('isbn 1 2345 6789 0').for(:isbn)
should allow_value('isbn 1-2345-6789-0').for(:isbn)
should_not allow_value("bad1").for(:isbn)
should_not allow_value("bad2").for(:isbn)

should allow_mass_assignment_of(:first_name)
should allow_mass_assignment_of(:last_name)
should_not allow_mass_assignment_of(:password)
should_not allow_mass_assignment_of(:admin_flag)

should have_readonly_attributes(:password)
should have_readonly_attributes(:admin_flag)

controllers:

should_set_the_flash_to "Thank you for placing this order."
should_not_set_the_flash

should_filter_params :password, :ssn

should_assign_to :user, :posts

should_assign_to :user, :class => User
should_assign_to(:user) { @user }

should_not_assign_to :user, :posts

should_set_session(:user_id) { @user.id }

should_respond_with :success

should_respond_with_content_type :rss

should_render_template :new

should_render_with_layout "special"

should_render_without_layout

should_route :get, "/posts", :controller => :posts, :action => :index

should_redirect_to("the user profile") { user_url(@user) }

=>

should set_the_flash.to("Thank you for placing this order.")
should_not set_the_flash

should filter_param(:password)
should filter_param(:ssn)

should assign_to(:user)
should assign_to(:posts)

should assign_to(:user).with_kind_of(User)
should assign_to(:user).with(@user)

should_not assign_to(:user)
should_not assign_to(:posts)

should set_session(:user_id).to(@user.id)

should respond_with(:success)

should respond_with_content_type(:rss)

should render_template(:new)

should render_with_layout("special")

should_not render_layout

should route(:get, "/posts").to(:controller => :posts, :action => :index)

should redirect_to("the user profile") { user_url(@user) }
use_new_syntax

It uses shoulda new syntax and fix deprecations.

will_paginate

use_new_syntax

It uses will_paginate new syntax.

Post.paginate(:conditions => {:active => true}, :order => "created_at DESC", :per_page => 10, :page => 1)

Post.paginated_each(:conditions => {:active => true}, :order => "created_at DESC", :per_page => 10) do |post|
end

=>

Post.where(:active => true).order("created_at DESC").paginate(:per_page => 10, :page => 1)

Post.where(:active => true).order("created_at DESC").find_each(:batch_size => 10) do |post|
end