diff options
Diffstat (limited to 'railties/lib')
36 files changed, 144 insertions, 107 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index f8a923141d..39ca2db8e1 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -260,6 +260,7 @@ module Rails            "action_dispatch.signed_cookie_salt" => config.action_dispatch.signed_cookie_salt,            "action_dispatch.encrypted_cookie_salt" => config.action_dispatch.encrypted_cookie_salt,            "action_dispatch.encrypted_signed_cookie_salt" => config.action_dispatch.encrypted_signed_cookie_salt, +          "action_dispatch.authenticated_encrypted_cookie_salt" => config.action_dispatch.authenticated_encrypted_cookie_salt,            "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer,            "action_dispatch.cookies_digest" => config.action_dispatch.cookies_digest          ) diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 27c1572357..4ffde6198a 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -77,9 +77,21 @@ module Rails              assets.unknown_asset_fallback = false            end +          if respond_to?(:action_view) +            action_view.form_with_generates_remote_forms = true +          end +          when "5.2"            load_defaults "5.1" +          if respond_to?(:active_record) +            active_record.cache_versioning = true +          end + +          if respond_to?(:action_dispatch) +            action_dispatch.use_authenticated_cookie_encryption = true +          end +          else            raise "Unknown version #{target_version.to_s.inspect}"          end diff --git a/railties/lib/rails/application_controller.rb b/railties/lib/rails/application_controller.rb index a98e51fd28..f7d112900a 100644 --- a/railties/lib/rails/application_controller.rb +++ b/railties/lib/rails/application_controller.rb @@ -1,5 +1,5 @@  class Rails::ApplicationController < ActionController::Base # :nodoc: -  self.view_paths = File.expand_path("../templates", __FILE__) +  self.view_paths = File.expand_path("templates", __dir__)    layout "application"    private diff --git a/railties/lib/rails/command.rb b/railties/lib/rails/command.rb index 0d4e6dc5a1..ee020b58f9 100644 --- a/railties/lib/rails/command.rb +++ b/railties/lib/rails/command.rb @@ -23,7 +23,7 @@ module Rails        end        def environment # :nodoc: -        ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" +        ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development"        end        # Receives a namespace, arguments and the behavior to invoke the command. diff --git a/railties/lib/rails/command/actions.rb b/railties/lib/rails/command/actions.rb index 8fda1c87c6..a00e58997c 100644 --- a/railties/lib/rails/command/actions.rb +++ b/railties/lib/rails/command/actions.rb @@ -5,7 +5,7 @@ module Rails        # This allows us to run `rails server` from other directories, but still get        # the main config.ru and properly set the tmp directory.        def set_application_directory! -        Dir.chdir(File.expand_path("../../", APP_PATH)) unless File.exist?(File.expand_path("config.ru")) +        Dir.chdir(File.expand_path("../..", APP_PATH)) unless File.exist?(File.expand_path("config.ru"))        end        def require_application_and_environment! diff --git a/railties/lib/rails/commands/secrets/secrets_command.rb b/railties/lib/rails/commands/secrets/secrets_command.rb index 03a640bd65..651411d444 100644 --- a/railties/lib/rails/commands/secrets/secrets_command.rb +++ b/railties/lib/rails/commands/secrets/secrets_command.rb @@ -13,10 +13,7 @@ module Rails        end        def setup -        require "rails/generators" -        require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" - -        Rails::Generators::EncryptedSecretsGenerator.start +        generator.start        end        def edit @@ -34,7 +31,6 @@ module Rails          require_application_and_environment!          Rails::Secrets.read_for_editing do |tmp_path| -          say "Waiting for secrets file to be saved. Abort with Ctrl-C."            system("\$EDITOR #{tmp_path}")          end @@ -43,7 +39,22 @@ module Rails          say "Aborted changing encrypted secrets: nothing saved."        rescue Rails::Secrets::MissingKeyError => error          say error.message +      rescue Errno::ENOENT => error +        raise unless error.message =~ /secrets\.yml\.enc/ + +        Rails::Secrets.read_template_for_editing do |tmp_path| +          system("\$EDITOR #{tmp_path}") +          generator.skip_secrets_file { setup } +        end        end + +      private +        def generator +          require "rails/generators" +          require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" + +          Rails::Generators::EncryptedSecretsGenerator +        end      end    end  end diff --git a/railties/lib/rails/commands/server/server_command.rb b/railties/lib/rails/commands/server/server_command.rb index cf3903f3ae..ebb4ae795a 100644 --- a/railties/lib/rails/commands/server/server_command.rb +++ b/railties/lib/rails/commands/server/server_command.rb @@ -155,9 +155,16 @@ module Rails          def user_supplied_options            @user_supplied_options ||= begin              # Convert incoming options array to a hash of flags -            #   ["-p", "3001", "-c", "foo"] # => {"-p" => true, "-c" => true} +            #   ["-p3001", "-C", "--binding", "127.0.0.1"] # => {"-p"=>true, "-C"=>true, "--binding"=>true}              user_flag = {} -            @original_options.each_with_index { |command, i| user_flag[command] = true if i.even? } +            @original_options.each do |command| +              if command.to_s.start_with?("--") +                option = command.split("=")[0] +                user_flag[option] = true +              elsif command =~ /\A(-.)/ +                user_flag[Regexp.last_match[0]] = true +              end +            end              # Collect all options that the user has explicitly defined so we can              # differentiate them from defaults diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index dc0b158bd4..2732485c5a 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -40,7 +40,7 @@ module Rails    #    #   class MyEngine < Rails::Engine    #     # Add a load path for this specific Engine -  #     config.autoload_paths << File.expand_path("../lib/some/path", __FILE__) +  #     config.autoload_paths << File.expand_path("lib/some/path", __dir__)    #    #     initializer "my_engine.add_middleware" do |app|    #       app.middleware.use MyEngine::Middleware diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 8ec805370b..8f15f3a594 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -1,4 +1,4 @@ -activesupport_path = File.expand_path("../../../../activesupport/lib", __FILE__) +activesupport_path = File.expand_path("../../../activesupport/lib", __dir__)  $:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path)  require "thor/group" diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index c715e5ac9f..8429b6c7b8 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -13,7 +13,6 @@ module Rails        DATABASES = %w( mysql postgresql sqlite3 oracle frontbase ibm_db sqlserver )        JDBC_DATABASES = %w( jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc )        DATABASES.concat(JDBC_DATABASES) -      WEBPACKS = %w( react vue angular )        attr_accessor :rails_template        add_shebang_option! @@ -31,9 +30,6 @@ module Rails          class_option :database,           type: :string, aliases: "-d", default: "sqlite3",                                            desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})" -        class_option :webpack,            type: :string, default: nil, -                                          desc: "Preconfigure for app-like JavaScript with Webpack (options: #{WEBPACKS.join('/')})" -          class_option :skip_yarn,          type: :boolean, default: false,                                            desc: "Don't use Yarn for managing JavaScript dependencies" @@ -353,7 +349,7 @@ module Rails          if defined?(JRUBY_VERSION)            GemfileEntry.version "therubyrhino", nil, comment          else -          GemfileEntry.new "therubyracer", nil, comment, { platforms: :ruby }, true +          GemfileEntry.new "mini_racer", nil, comment, { platforms: :ruby }, true          end        end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index a650c52626..e7f51dba99 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -215,7 +215,7 @@ module Rails        # Returns the base root for a common set of generators. This is used to dynamically        # guess the default source root.        def self.base_root -        File.dirname(__FILE__) +        __dir__        end        # Cache source root and add lib/generators/base/generator/templates to diff --git a/railties/lib/rails/generators/css/assets/assets_generator.rb b/railties/lib/rails/generators/css/assets/assets_generator.rb index 20baf31a34..af7b5cf609 100644 --- a/railties/lib/rails/generators/css/assets/assets_generator.rb +++ b/railties/lib/rails/generators/css/assets/assets_generator.rb @@ -3,7 +3,7 @@ require "rails/generators/named_base"  module Css # :nodoc:    module Generators # :nodoc:      class AssetsGenerator < Rails::Generators::NamedBase # :nodoc: -      source_root File.expand_path("../templates", __FILE__) +      source_root File.expand_path("templates", __dir__)        def copy_stylesheet          copy_file "stylesheet.css", File.join("app/assets/stylesheets", class_path, "#{file_name}.css") diff --git a/railties/lib/rails/generators/js/assets/assets_generator.rb b/railties/lib/rails/generators/js/assets/assets_generator.rb index 64d706ec91..52a71b58cd 100644 --- a/railties/lib/rails/generators/js/assets/assets_generator.rb +++ b/railties/lib/rails/generators/js/assets/assets_generator.rb @@ -3,7 +3,7 @@ require "rails/generators/named_base"  module Js # :nodoc:    module Generators # :nodoc:      class AssetsGenerator < Rails::Generators::NamedBase # :nodoc: -      source_root File.expand_path("../templates", __FILE__) +      source_root File.expand_path("templates", __dir__)        def copy_javascript          copy_file "javascript.js", File.join("app/assets/javascripts", class_path, "#{file_name}.js") diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 669514b37e..45b9e7bdff 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -121,7 +121,6 @@ module Rails        action_cable_config_exist = File.exist?("config/cable.yml")        rack_cors_config_exist = File.exist?("config/initializers/cors.rb")        assets_config_exist = File.exist?("config/initializers/assets.rb") -      new_framework_defaults_5_1_exist = File.exist?("config/initializers/new_framework_defaults_5_1.rb")        config @@ -145,12 +144,6 @@ module Rails          unless assets_config_exist            remove_file "config/initializers/assets.rb"          end - -        # Sprockets owns the only new default for 5.1: -        # In API-only Applications, we don't want the file. -        unless new_framework_defaults_5_1_exist -          remove_file "config/initializers/new_framework_defaults_5_1.rb" -        end        end      end @@ -208,10 +201,12 @@ module Rails    module Generators      # We need to store the RAILS_DEV_PATH in a constant, otherwise the path      # can change in Ruby 1.8.7 when we FileUtils.cd. -    RAILS_DEV_PATH = File.expand_path("../../../../../..", File.dirname(__FILE__)) +    RAILS_DEV_PATH = File.expand_path("../../../../../..", __dir__)      RESERVED_NAMES = %w[application destroy plugin runner test]      class AppGenerator < AppBase # :nodoc: +      WEBPACKS = %w( react vue angular elm ) +        add_shared_options_for "application"        # Add bin/rails options @@ -224,6 +219,9 @@ module Rails        class_option :skip_bundle, type: :boolean, aliases: "-B", default: false,                                   desc: "Don't run bundle install" +      class_option :webpack, type: :string, default: nil, +                             desc: "Preconfigure for app-like JavaScript with Webpack (options: #{WEBPACKS.join('/')})" +        def initialize(*args)          super @@ -401,7 +399,7 @@ module Rails        def delete_new_framework_defaults          unless options[:update] -          remove_file "config/initializers/new_framework_defaults_5_1.rb" +          remove_file "config/initializers/new_framework_defaults_5_2.rb"          end        end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 1911fb7a7b..747d2e6253 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -1,9 +1,5 @@  source 'https://rubygems.org' - -git_source(:github) do |repo_name| -  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") -  "https://github.com/#{repo_name}.git" -end +git_source(:github) { |repo| "https://github.com/#{repo}.git" }  <% gemfile_entries.each do |gem| -%>  <% if gem.comment -%> diff --git a/railties/lib/rails/generators/rails/app/templates/bin/bundle b/railties/lib/rails/generators/rails/app/templates/bin/bundle index 1123dcf501..a84f0afe47 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/bundle +++ b/railties/lib/rails/generators/rails/app/templates/bin/bundle @@ -1,2 +1,2 @@ -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)  load Gem.bin_path('bundler', 'bundle') diff --git a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt index 52b3de5ee5..ee9d077c30 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt +++ b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt @@ -1,9 +1,8 @@ -require 'pathname'  require 'fileutils'  include FileUtils  # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = File.expand_path('..', __dir__)  def system!(*args)    system(*args) || abort("\n== Command #{args} failed ==") diff --git a/railties/lib/rails/generators/rails/app/templates/bin/update.tt b/railties/lib/rails/generators/rails/app/templates/bin/update.tt index d385b363c6..5b6e50883e 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/update.tt +++ b/railties/lib/rails/generators/rails/app/templates/bin/update.tt @@ -1,9 +1,8 @@ -require 'pathname'  require 'fileutils'  include FileUtils  # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = File.expand_path('..', __dir__)  def system!(*args)    system(*args) || abort("\n== Command #{args} failed ==") diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index 9c4a77fd1d..d44331a888 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -36,8 +36,8 @@ Rails.application.configure do    config.assets.compile = false    # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb -  <%- end -%> +  <%- end -%>    # Enable serving of images, stylesheets, and JavaScripts from an asset server.    # config.action_controller.asset_host = 'http://assets.example.com' @@ -50,8 +50,8 @@ Rails.application.configure do    # config.action_cable.mount_path = nil    # config.action_cable.url = 'wss://example.com/cable'    # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] -  <%- end -%> +  <%- end -%>    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.    # config.force_ssl = true @@ -68,14 +68,15 @@ Rails.application.configure do    # Use a real queuing backend for Active Job (and separate queues per environment)    # config.active_job.queue_adapter     = :resque    # config.active_job.queue_name_prefix = "<%= app_name %>_#{Rails.env}" +    <%- unless options.skip_action_mailer? -%>    config.action_mailer.perform_caching = false    # Ignore bad email addresses and do not raise email delivery errors.    # Set this to true and configure the email server for immediate delivery to raise delivery errors.    # config.action_mailer.raise_delivery_errors = false -  <%- end -%> +  <%- end -%>    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to    # the I18n.default_locale when a translation cannot be found).    config.i18n.fallbacks = true diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_1.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_1.rb.tt deleted file mode 100644 index a0c7f44b60..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_1.rb.tt +++ /dev/null @@ -1,16 +0,0 @@ -# Be sure to restart your server when you modify this file. -# -# This file contains migration options to ease your Rails 5.1 upgrade. -# -# Once upgraded flip defaults one by one to migrate to the new default. -# -# Read the Guide for Upgrading Ruby on Rails for more info on each option. - -# Make `form_with` generate non-remote forms. -Rails.application.config.action_view.form_with_generates_remote_forms = false -<%- unless options[:skip_sprockets] -%> - -# Unknown asset fallback will return the path passed in when the given -# asset is not present in the asset pipeline. -# Rails.application.config.assets.unknown_asset_fallback = false -<%- end -%> diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt new file mode 100644 index 0000000000..900baa607a --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt @@ -0,0 +1,15 @@ +# Be sure to restart your server when you modify this file. +# +# This file contains migration options to ease your Rails 5.2 upgrade. +# +# Once upgraded flip defaults one by one to migrate to the new default. +# +# Read the Guide for Upgrading Ruby on Rails for more info on each option. + +# Make Active Record use stable #cache_key alongside new #cache_version method. +# This is needed for recyclable cache keys. +# Rails.application.config.active_record.cache_versioning = true + +# Use AES 256 GCM authenticated encryption for encrypted cookies. +# Existing cookies will be converted on read then written with the new scheme. +# Rails.application.config.action_dispatch.use_authenticated_cookie_encryption = true diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb index 2f92168eef..7568af5b5e 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../config/environment', __FILE__) +require File.expand_path('../config/environment', __dir__)  require 'rails/test_help'  class ActiveSupport::TestCase diff --git a/railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb b/railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb index 8b29213610..1da2fbc1a5 100644 --- a/railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb +++ b/railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb @@ -36,25 +36,29 @@ module Rails        end        def add_encrypted_secrets_file -        unless File.exist?("config/secrets.yml.enc") +        unless (defined?(@@skip_secrets_file) && @@skip_secrets_file) || File.exist?("config/secrets.yml.enc")            say "Adding config/secrets.yml.enc to store secrets that needs to be encrypted."            say "" +          say "For now the file contains this but it's been encrypted with the generated key:" +          say "" +          say Secrets.template, :on_green +          say "" -          template "config/secrets.yml.enc" do |prefill| -            say "" -            say "For now the file contains this but it's been encrypted with the generated key:" -            say "" -            say prefill, :on_green -            say "" - -            Secrets.encrypt(prefill) -          end +          Secrets.write(Secrets.template)            say "You can edit encrypted secrets with `bin/rails secrets:edit`." - -          say "Add this to your config/environments/production.rb:" -          say "config.read_encrypted_secrets = true" +          say ""          end + +        say "Add this to your config/environments/production.rb:" +        say "config.read_encrypted_secrets = true" +      end + +      def self.skip_secrets_file +        @@skip_secrets_file = true +        yield +      ensure +        @@skip_secrets_file = false        end        private diff --git a/railties/lib/rails/generators/rails/encrypted_secrets/templates/config/secrets.yml.enc b/railties/lib/rails/generators/rails/encrypted_secrets/templates/config/secrets.yml.enc deleted file mode 100644 index 70426a66a5..0000000000 --- a/railties/lib/rails/generators/rails/encrypted_secrets/templates/config/secrets.yml.enc +++ /dev/null @@ -1,3 +0,0 @@ -# See `secrets.yml` for tips on generating suitable keys. -# production: -#  external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289… diff --git a/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt index d0575772bc..178d5c3f9f 100644 --- a/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +++ b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt @@ -1,3 +1,3 @@  class <%= class_name %>Generator < Rails::Generators::NamedBase -  source_root File.expand_path('../templates', __FILE__) +  source_root File.expand_path('templates', __dir__)  end diff --git a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec index d84d1aabdb..9a8c4bf098 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec +++ b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec @@ -1,4 +1,4 @@ -$:.push File.expand_path("../lib", __FILE__) +$:.push File.expand_path("lib", __dir__)  # Maintain your gem's version:  require "<%= namespaced_name %>/version" diff --git a/railties/lib/rails/generators/rails/plugin/templates/Rakefile b/railties/lib/rails/generators/rails/plugin/templates/Rakefile index 383d2fb2d1..3581dd401a 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/Rakefile +++ b/railties/lib/rails/generators/rails/plugin/templates/Rakefile @@ -15,7 +15,7 @@ RDoc::Task.new(:rdoc) do |rdoc|  end  <% if engine? && !options[:skip_active_record] && with_dummy_app? -%> -APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) +APP_RAKEFILE = File.expand_path("<%= dummy_path -%>/Rakefile", __dir__)  load 'rails/tasks/engine.rake'  <% end %> diff --git a/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt index c03d9953d4..ffa277e334 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt @@ -1,12 +1,12 @@  # This command will automatically be run when you run "rails" with Rails gems  # installed from the root of your application. -ENGINE_ROOT = File.expand_path('../..', __FILE__) -ENGINE_PATH = File.expand_path('../../lib/<%= namespaced_name -%>/engine', __FILE__) -APP_PATH = File.expand_path('../../<%= dummy_path -%>/config/application', __FILE__) +ENGINE_ROOT = File.expand_path('..', __dir__) +ENGINE_PATH = File.expand_path('../lib/<%= namespaced_name -%>/engine', __dir__) +APP_PATH = File.expand_path('../<%= dummy_path -%>/config/application', __dir__)  # Set up gems listed in the Gemfile. -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)  require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])  require 'rails/all' diff --git a/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt b/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt index 8385e6a8a2..8e7d321626 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt @@ -1,4 +1,4 @@ -$: << File.expand_path(File.expand_path("../../test", __FILE__)) +$: << File.expand_path("../test", __dir__)  require "bundler/setup"  require "rails/plugin/test" diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb index e84e403018..32e8202e1c 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb @@ -1,8 +1,8 @@ -require File.expand_path("../../<%= options[:dummy_path] -%>/config/environment.rb", __FILE__) +require File.expand_path("../<%= options[:dummy_path] -%>/config/environment.rb", __dir__)  <% unless options[:skip_active_record] -%> -ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../<%= options[:dummy_path] -%>/db/migrate", __FILE__)] +ActiveRecord::Migrator.migrations_paths = [File.expand_path("../<%= options[:dummy_path] -%>/db/migrate", __dir__)]  <% if options[:mountable] -%> -ActiveRecord::Migrator.migrations_paths << File.expand_path('../../db/migrate', __FILE__) +ActiveRecord::Migrator.migrations_paths << File.expand_path('../db/migrate', __dir__)  <% end -%>  <% end -%>  require "rails/test_help" @@ -17,7 +17,7 @@ Rails::TestUnitReporter.executable = 'bin/test'  # Load fixtures from the engine  if ActiveSupport::TestCase.respond_to?(:fixture_path=) -  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) +  ActiveSupport::TestCase.fixture_path = File.expand_path("fixtures", __dir__)    ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path    ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files"    ActiveSupport::TestCase.fixtures :all diff --git a/railties/lib/rails/generators/test_case.rb b/railties/lib/rails/generators/test_case.rb index 3eec929aeb..575af80303 100644 --- a/railties/lib/rails/generators/test_case.rb +++ b/railties/lib/rails/generators/test_case.rb @@ -14,7 +14,7 @@ module Rails      #      #   class AppGeneratorTest < Rails::Generators::TestCase      #     tests AppGenerator -    #     destination File.expand_path("../tmp", File.dirname(__FILE__)) +    #     destination File.expand_path("../tmp", __dir__)      #   end      #      # If you want to ensure your destination root is clean before running each test, @@ -22,7 +22,7 @@ module Rails      #      #   class AppGeneratorTest < Rails::Generators::TestCase      #     tests AppGenerator -    #     destination File.expand_path("../tmp", File.dirname(__FILE__)) +    #     destination File.expand_path("../tmp", __dir__)      #     setup :prepare_destination      #   end      class TestCase < ActiveSupport::TestCase diff --git a/railties/lib/rails/generators/test_unit/system/system_generator.rb b/railties/lib/rails/generators/test_unit/system/system_generator.rb index aec415a4e5..0514957d9c 100644 --- a/railties/lib/rails/generators/test_unit/system/system_generator.rb +++ b/railties/lib/rails/generators/test_unit/system/system_generator.rb @@ -10,7 +10,7 @@ module TestUnit # :nodoc:            template "application_system_test_case.rb", File.join("test", "application_system_test_case.rb")          end -        template "system_test.rb", File.join("test/system", "#{file_name.pluralize}_test.rb") +        template "system_test.rb", File.join("test/system", class_path, "#{file_name.pluralize}_test.rb")        end      end    end diff --git a/railties/lib/rails/generators/testing/behaviour.rb b/railties/lib/rails/generators/testing/behaviour.rb index 64d641d096..ce0e42e60d 100644 --- a/railties/lib/rails/generators/testing/behaviour.rb +++ b/railties/lib/rails/generators/testing/behaviour.rb @@ -14,12 +14,12 @@ module Rails          include ActiveSupport::Testing::Stream          included do -          class_attribute :destination_root, :current_path, :generator_class, :default_arguments -            # Generators frequently change the current path using +FileUtils.cd+.            # So we need to store the path at file load and revert back to it after each test. -          self.current_path = File.expand_path(Dir.pwd) -          self.default_arguments = [] +          class_attribute :current_path, default: File.expand_path(Dir.pwd) +          class_attribute :default_arguments, default: [] +          class_attribute :destination_root +          class_attribute :generator_class          end          module ClassMethods @@ -40,7 +40,7 @@ module Rails            # Sets the destination of generator files:            # -          #   destination File.expand_path("../tmp", File.dirname(__FILE__)) +          #   destination File.expand_path("../tmp", __dir__)            def destination(path)              self.destination_root = path            end @@ -51,7 +51,7 @@ module Rails          #          #   class AppGeneratorTest < Rails::Generators::TestCase          #     tests AppGenerator -        #     destination File.expand_path("../tmp", File.dirname(__FILE__)) +        #     destination File.expand_path("../tmp", __dir__)          #     setup :prepare_destination          #          #     test "database.yml is not created when skipping Active Record" do diff --git a/railties/lib/rails/secrets.rb b/railties/lib/rails/secrets.rb index 8b644f212c..c7a8676d7b 100644 --- a/railties/lib/rails/secrets.rb +++ b/railties/lib/rails/secrets.rb @@ -1,5 +1,6 @@  require "yaml"  require "active_support/message_encryptor" +require "active_support/core_ext/string/strip"  module Rails    # Greatly inspired by Ara T. Howard's magnificent sekrets gem. 😘 @@ -37,6 +38,15 @@ module Rails          ENV["RAILS_MASTER_KEY"] || read_key_file || handle_missing_key        end +      def template +        <<-end_of_template.strip_heredoc +          # See `secrets.yml` for tips on generating suitable keys. +          # production: +          #  external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289 + +        end_of_template +      end +        def encrypt(data)          encryptor.encrypt_and_sign(data)        end @@ -54,15 +64,12 @@ module Rails          FileUtils.mv("#{path}.tmp", path)        end -      def read_for_editing -        tmp_path = File.join(Dir.tmpdir, File.basename(path)) -        IO.binwrite(tmp_path, read) - -        yield tmp_path +      def read_for_editing(&block) +        writing(read, &block) +      end -        write(IO.binread(tmp_path)) -      ensure -        FileUtils.rm(tmp_path) if File.exist?(tmp_path) +      def read_template_for_editing(&block) +        writing(template, &block)        end        private @@ -92,6 +99,17 @@ module Rails            end          end +        def writing(contents) +          tmp_path = File.join(Dir.tmpdir, File.basename(path)) +          File.write(tmp_path, contents) + +          yield tmp_path + +          write(File.read(tmp_path)) +        ensure +          FileUtils.rm(tmp_path) if File.exist?(tmp_path) +        end +          def encryptor            @encryptor ||= ActiveSupport::MessageEncryptor.new([ key ].pack("H*"), cipher: @cipher)          end diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake index 32a6b109bc..80720a42ff 100644 --- a/railties/lib/rails/tasks/framework.rake +++ b/railties/lib/rails/tasks/framework.rake @@ -16,7 +16,7 @@ namespace :app do    namespace :templates do      # desc "Copy all the templates from rails to the application directory for customization. Already existing local copies will be overwritten"      task :copy do -      generators_lib = File.expand_path("../../generators", __FILE__) +      generators_lib = File.expand_path("../generators", __dir__)        project_templates = "#{Rails.root}/lib/templates"        default_templates = { "erb"   => %w{controller mailer scaffold}, diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index fe11664d5e..1cc27f7b6c 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -3,8 +3,7 @@ require "minitest"  module Rails    class TestUnitReporter < Minitest::StatisticsReporter -    class_attribute :executable -    self.executable = "bin/rails test" +    class_attribute :executable, default: "bin/rails test"      def record(result)        super  | 
