mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
e8be4f0891
### Summary In a Rails application using Ruby 2.6.0-dev, when running `bin/rails g migration` with `RUBYOPT=-w`, an ERB deprecation warnings will be displayed. ```console % ruby -v ruby 2.6.0dev (2018-03-03 trunk 62644) [x86_64-darwin17] % bin/rails -v Rails 6.0.0.alpha % RUBYOPT=-w bin/rails g migration create_foos (snip) /Users/koic/src/github.com/rails/rails/railties/lib/rails/generators/migration.rb:66: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. /Users/koic/src/github.com/rails/rails/railties/lib/rails/generators/migration.rb:66: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead. /Users/koic/src/github.com/rails/rails/railties/lib/rails/generators/migration.rb:66: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead. create db/migrate/20180304002144_create_foos.rb ``` This PR suppresses the above deprecation warnings in Ruby 2.6.0-dev. This warning is due to the interface of `ERB.new` will change from Ruby 2.6. > Add :trim_mode and :eoutvar keyword arguments to ERB.new. > Now non-keyword arguments other than first one are softly deprecated > and will be removed when Ruby 2.5 becomes EOL. [Feature #14256]2311087b68/NEWS (stdlib-updates-outstanding-ones-only)
The following addresses are related Ruby's commit. https://github.com/ruby/ruby/commit/cc777d0 Also this PR will change `ERB.new` used in `tasks/release.rb`. ### Other Information This PR uses `ERB.version` to switch `ERB.new` interface. Because Rails 6 supports multiple Ruby versions (Ruby 2.4.1 or higher), it need to use the appropriate interface. Using `ERB.version` instead of `RUBY_VERSON` is based on the following patch. https://github.com/ruby/ruby/pull/1826 This patch is built into Ruby.40db89c093
76 lines
2.8 KiB
Ruby
76 lines
2.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "active_support/concern"
|
|
require "rails/generators/actions/create_migration"
|
|
|
|
module Rails
|
|
module Generators
|
|
# Holds common methods for migrations. It assumes that migrations have the
|
|
# [0-9]*_name format and can be used by other frameworks (like Sequel)
|
|
# just by implementing the next migration version method.
|
|
module Migration
|
|
extend ActiveSupport::Concern
|
|
attr_reader :migration_number, :migration_file_name, :migration_class_name
|
|
|
|
module ClassMethods #:nodoc:
|
|
def migration_lookup_at(dirname)
|
|
Dir.glob("#{dirname}/[0-9]*_*.rb")
|
|
end
|
|
|
|
def migration_exists?(dirname, file_name)
|
|
migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
|
|
end
|
|
|
|
def current_migration_number(dirname)
|
|
migration_lookup_at(dirname).collect do |file|
|
|
File.basename(file).split("_").first.to_i
|
|
end.max.to_i
|
|
end
|
|
|
|
def next_migration_number(dirname)
|
|
raise NotImplementedError
|
|
end
|
|
end
|
|
|
|
def create_migration(destination, data, config = {}, &block)
|
|
action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config)
|
|
end
|
|
|
|
def set_migration_assigns!(destination)
|
|
destination = File.expand_path(destination, destination_root)
|
|
|
|
migration_dir = File.dirname(destination)
|
|
@migration_number = self.class.next_migration_number(migration_dir)
|
|
@migration_file_name = File.basename(destination, ".rb")
|
|
@migration_class_name = @migration_file_name.camelize
|
|
end
|
|
|
|
# Creates a migration template at the given destination. The difference
|
|
# to the default template method is that the migration version is appended
|
|
# to the destination file name.
|
|
#
|
|
# The migration version, migration file name, migration class name are
|
|
# available as instance variables in the template to be rendered.
|
|
#
|
|
# migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
|
|
def migration_template(source, destination, config = {})
|
|
source = File.expand_path(find_in_source_paths(source.to_s))
|
|
|
|
set_migration_assigns!(destination)
|
|
context = instance_eval("binding")
|
|
|
|
dir, base = File.split(destination)
|
|
numbered_destination = File.join(dir, ["%migration_number%", base].join("_"))
|
|
|
|
create_migration numbered_destination, nil, config do
|
|
match = ERB.version.match(/\Aerb\.rb \[(?<version>[^ ]+) /)
|
|
if match && match[:version] >= "2.2.0" # Ruby 2.6+
|
|
ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(context)
|
|
else
|
|
ERB.new(::File.binread(source), nil, "-", "@output_buffer").result(context)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|