diff options
Diffstat (limited to 'railties/lib/rails/generators')
23 files changed, 195 insertions, 35 deletions
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index ea88afe9f4..ebe8cfea60 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -82,6 +82,9 @@ module Rails          class_option :skip_test,          type: :boolean, aliases: "-T", default: false,                                            desc: "Skip test files" +        class_option :skip_system_test,   type: :boolean, default: false, +                                          desc: "Skip system test files" +          class_option :dev,                type: :boolean, default: false,                                            desc: "Setup the #{name} with Gemfile pointing to your Rails checkout" @@ -190,7 +193,7 @@ module Rails        def webserver_gemfile_entry # :doc:          return [] if options[:skip_puma]          comment = "Use Puma as the app server" -        GemfileEntry.new("puma", "~> 3.0", comment) +        GemfileEntry.new("puma", "~> 3.7", comment)        end        def include_all_railties? # :doc: @@ -243,7 +246,6 @@ module Rails        def rails_gemfile_entry          dev_edge_common = [ -          GemfileEntry.github("arel", "rails/arel")          ]          if options.dev?            [ @@ -261,14 +263,13 @@ module Rails        end        def rails_version_specifier(gem_version = Rails.gem_version) -        if gem_version.prerelease? -          next_series = gem_version -          next_series = next_series.bump while next_series.segments.size > 2 - -          [">= #{gem_version}", "< #{next_series}"] -        elsif gem_version.segments.size == 3 +        if gem_version.segments.size == 3 || gem_version.release.segments.size == 3 +          # ~> 1.2.3 +          # ~> 1.2.3.pre4            "~> #{gem_version}"          else +          # ~> 1.2.3, >= 1.2.3.4 +          # ~> 1.2.3, >= 1.2.3.4.pre5            patch = gem_version.segments[0, 3].join(".")            ["~> #{patch}", ">= #{gem_version}"]          end @@ -279,7 +280,7 @@ module Rails          case options[:database]          when "mysql"          then ["mysql2", [">= 0.3.18", "< 0.5"]]          when "postgresql"     then ["pg", ["~> 0.18"]] -        when "oracle"         then ["ruby-oci8", nil] +        when "oracle"         then ["activerecord-oracle_enhanced-adapter", nil]          when "frontbase"      then ["ruby-frontbase", nil]          when "sqlserver"      then ["activerecord-sqlserver-adapter", nil]          when "jdbcmysql"      then ["activerecord-jdbcmysql-adapter", nil] @@ -295,7 +296,6 @@ module Rails            case options[:database]            when "postgresql" then options[:database].replace "jdbcpostgresql"            when "mysql"      then options[:database].replace "jdbcmysql" -          when "oracle"     then options[:database].replace "jdbc"            when "sqlite3"    then options[:database].replace "jdbcsqlite3"            end          end @@ -321,7 +321,7 @@ module Rails          return [] unless options[:webpack]          comment = "Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker" -        GemfileEntry.github "webpacker", "rails/webpacker", nil, comment +        GemfileEntry.new "webpacker", nil, comment        end        def jbuilder_gemfile_entry diff --git a/railties/lib/rails/generators/erb.rb b/railties/lib/rails/generators/erb.rb index d5e326d6ee..97d9ab29d4 100644 --- a/railties/lib/rails/generators/erb.rb +++ b/railties/lib/rails/generators/erb.rb @@ -17,8 +17,8 @@ module Erb # :nodoc:            :erb          end -        def filename_with_extensions(name, format = self.format) -          [name, format, handler].compact.join(".") +        def filename_with_extensions(name, file_format = format) +          [name, file_format, handler].compact.join(".")          end      end    end diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 3cf923faf0..442258c9d1 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -32,6 +32,14 @@ module Rails    # This allows you to override entire operations, like the creation of the    # Gemfile, README, or JavaScript files, without needing to know exactly    # what those operations do so you can create another template action. +  # +  #  class CustomAppBuilder < Rails::AppBuilder +  #    def test +  #      @generator.gem "rspec-rails", group: [:development, :test] +  #      run "bundle install" +  #      generate "rspec:install" +  #    end +  #  end    class AppBuilder      def rakefile        template "Rakefile" @@ -150,6 +158,12 @@ module Rails        template "test/test_helper.rb"      end +    def system_test +      empty_directory_with_keep_file "test/system" + +      template "test/application_system_test_case.rb" +    end +      def tmp        empty_directory_with_keep_file "tmp"        empty_directory "tmp/cache" @@ -160,7 +174,7 @@ module Rails        empty_directory_with_keep_file "vendor"        unless options[:skip_yarn] -        template "package.json", "vendor/package.json" +        template "package.json"        end      end    end @@ -262,6 +276,10 @@ module Rails          build(:test) unless options[:skip_test]        end +      def create_system_test_files +        build(:system_test) unless options[:skip_system_test] || options[:skip_test] || options[:api] +      end +        def create_tmp_files          build(:tmp)        end @@ -270,7 +288,7 @@ module Rails          build(:vendor)          if options[:skip_yarn] -          remove_file "vendor/package.json" +          remove_file "package.json"          end        end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 24d2fa1284..b082d70cba 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -32,6 +32,11 @@ end  group :development, :test do    # Call 'byebug' anywhere in the code to stop execution and get a debugger console    gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] +  <%- unless options.skip_system_test? || options.api? -%> +  # Adds support for Capybara system testing and selenium driver +  gem 'capybara', '~> 2.7.0' +  gem 'selenium-webdriver' +  <%- end -%>  end  group :development do diff --git a/railties/lib/rails/generators/rails/app/templates/bin/yarn b/railties/lib/rails/generators/rails/app/templates/bin/yarn index 872438cecb..4ae896a8d3 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/yarn +++ b/railties/lib/rails/generators/rails/app/templates/bin/yarn @@ -1,4 +1,4 @@ -VENDOR_PATH = File.expand_path('../vendor', __dir__) +VENDOR_PATH = File.expand_path('..', __dir__)  Dir.chdir(VENDOR_PATH) do    begin      exec "yarnpkg #{ARGV.join(" ")}" diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml index d2499ea4fb..6da0601b24 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml @@ -1,4 +1,4 @@ -# Oracle/OCI 8i, 9, 10g +# Oracle/OCI 11g or higher recommended  #  # Requires Ruby/OCI8:  #  https://github.com/kubo/ruby-oci8 @@ -17,7 +17,7 @@  #  cursor_sharing: similar  #  default: &default -  adapter: oracle +  adapter: oracle_enhanced    pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>    username: <%= app_name %>    password: @@ -45,7 +45,9 @@ test:  # On Heroku and other platform providers, you may have a full connection URL  # available as an environment variable. For example:  # -#   DATABASE_URL="oracle://myuser:mypass@localhost/somedatabase" +#   DATABASE_URL="oracle-enhanced://myuser:mypass@localhost/somedatabase" +# +# Note that the adapter name uses a dash instead of an underscore.  #  # You can use this database configuration with:  # 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 4a39e43e57..9c4a77fd1d 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 @@ -14,6 +14,11 @@ Rails.application.configure do    config.consider_all_requests_local       = false    config.action_controller.perform_caching = true +  # Attempt to read encrypted secrets from `config/secrets.yml.enc`. +  # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or +  # `config/secrets.yml.key`. +  config.read_encrypted_secrets = true +    # Disable serving static files from the `/public` folder by default since    # Apache or NGINX already handles this.    config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt index f5d03fb117..51196ae743 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt @@ -7,7 +7,7 @@ Rails.application.config.assets.version = '1.0'  # Rails.application.config.assets.paths << Emoji.images_path  <%- unless options[:skip_yarn] -%>  # Add Yarn node_modules folder to the asset load path. -Rails.application.config.assets.paths << Rails.root.join('vendor/node_modules') +Rails.application.config.assets.paths << Rails.root.join('node_modules')  <%- end -%>  # Precompile additional assets. diff --git a/railties/lib/rails/generators/rails/app/templates/config/secrets.yml b/railties/lib/rails/generators/rails/app/templates/config/secrets.yml index 8e995a5df1..816efcc5b1 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/secrets.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/secrets.yml @@ -23,8 +23,10 @@ development:  test:    secret_key_base: <%= app_secret %> -# Do not keep production secrets in the repository, -# instead read values from the environment. +# Do not keep production secrets in the unencrypted secrets file. +# Instead, either read values from the environment. +# Or, use `bin/rails secrets:setup` to configure encrypted secrets +# and move the `production:` environment over there.  production:    secret_key_base: <%%= ENV["SECRET_KEY_BASE"] %> diff --git a/railties/lib/rails/generators/rails/app/templates/gitignore b/railties/lib/rails/generators/rails/app/templates/gitignore index 1768b700d9..7221c26729 100644 --- a/railties/lib/rails/generators/rails/app/templates/gitignore +++ b/railties/lib/rails/generators/rails/app/templates/gitignore @@ -22,8 +22,8 @@  <% end -%>  <% unless options[:skip_yarn] -%> -/vendor/node_modules -/vendor/yarn-error.log +/node_modules +/yarn-error.log  <% end -%>  .byebug_history diff --git a/railties/lib/rails/generators/rails/app/templates/test/application_system_test_case.rb b/railties/lib/rails/generators/rails/app/templates/test/application_system_test_case.rb new file mode 100644 index 0000000000..d19212abd5 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/test/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase +  driven_by :selenium, using: :chrome, screen_size: [1400, 1400] +end 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 new file mode 100644 index 0000000000..8b29213610 --- /dev/null +++ b/railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb @@ -0,0 +1,66 @@ +require "rails/generators/base" +require "rails/secrets" + +module Rails +  module Generators +    class EncryptedSecretsGenerator < Base +      def add_secrets_key_file +        unless File.exist?("config/secrets.yml.key") || File.exist?("config/secrets.yml.enc") +          key = Rails::Secrets.generate_key + +          say "Adding config/secrets.yml.key to store the encryption key: #{key}" +          say "" +          say "Save this in a password manager your team can access." +          say "" +          say "If you lose the key, no one, including you, can access any encrypted secrets." + +          say "" +          create_file "config/secrets.yml.key", key +          say "" +        end +      end + +      def ignore_key_file +        if File.exist?(".gitignore") +          unless File.read(".gitignore").include?(key_ignore) +            say "Ignoring config/secrets.yml.key so it won't end up in Git history:" +            say "" +            append_to_file ".gitignore", key_ignore +            say "" +          end +        else +          say "IMPORTANT: Don't commit config/secrets.yml.key. Add this to your ignore file:" +          say key_ignore, :on_green +          say "" +        end +      end + +      def add_encrypted_secrets_file +        unless File.exist?("config/secrets.yml.enc") +          say "Adding config/secrets.yml.enc to store secrets that needs to be encrypted." +          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 + +          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" +        end +      end + +      private +        def key_ignore +          [ "", "# Ignore encrypted secrets key file.", "config/secrets.yml.key", "" ].join("\n") +        end +    end +  end +end 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 new file mode 100644 index 0000000000..70426a66a5 --- /dev/null +++ b/railties/lib/rails/generators/rails/encrypted_secrets/templates/config/secrets.yml.enc @@ -0,0 +1,3 @@ +# See `secrets.yml` for tips on generating suitable keys. +# production: +#  external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289… diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index 49259f32c8..ca48919f9a 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -91,6 +91,8 @@ task default: :test        opts[:skip_bundle] = true        opts[:api] = options.api?        opts[:skip_listen] = true +      opts[:skip_git] = true +      opts[:skip_turbolinks] = true        invoke Rails::Generators::AppGenerator,          [ File.expand_path(dummy_path, destination_root) ], opts @@ -112,7 +114,6 @@ task default: :test      def test_dummy_clean        inside dummy_path do -        remove_file ".gitignore"          remove_file "db/seeds.rb"          remove_file "doc"          remove_file "Gemfile" @@ -432,7 +433,7 @@ end        end        def inside_application? -        rails_app_path && app_path =~ /^#{rails_app_path}/ +        rails_app_path && destination_root.start_with?(rails_app_path.to_s)        end        def relative_path 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 c0fbb84a93..8385e6a8a2 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt @@ -1,10 +1,4 @@ -$: << File.expand_path(File.expand_path('../../test', __FILE__)) +$: << File.expand_path(File.expand_path("../../test", __FILE__)) -require 'bundler/setup' -require 'rails/test_unit/minitest_plugin' - -Rails::TestUnitReporter.executable = 'bin/test' - -Minitest.run_via[:rails] = true - -require "active_support/testing/autorun" +require "bundler/setup" +require "rails/plugin/test" diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/application_system_test_case.rb b/railties/lib/rails/generators/rails/plugin/templates/test/application_system_test_case.rb new file mode 100644 index 0000000000..d19212abd5 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase +  driven_by :selenium, using: :chrome, screen_size: [1400, 1400] +end diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index ed6bf7f7d7..12d6bc85b2 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -6,6 +6,7 @@ module Rails        remove_hook_for :resource_controller        remove_class_option :actions +      class_option :api, type: :boolean        class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets"        class_option :stylesheet_engine, desc: "Engine for Stylesheets"        class_option :assets, type: :boolean @@ -15,10 +16,13 @@ module Rails        def handle_skip          @options = @options.merge(stylesheets: false) unless options[:assets]          @options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] && options[:scaffold_stylesheet] +        @options = @options.merge(system_tests: false) if options[:api]        end        hook_for :scaffold_controller, required: true +      hook_for :system_tests, as: :system +        hook_for :assets do |assets|          invoke assets, [controller_name]        end diff --git a/railties/lib/rails/generators/rails/system_test/USAGE b/railties/lib/rails/generators/rails/system_test/USAGE new file mode 100644 index 0000000000..f11a99e008 --- /dev/null +++ b/railties/lib/rails/generators/rails/system_test/USAGE @@ -0,0 +1,10 @@ +Description: +    Stubs out a new system test. Pass the name of the test, either +    CamelCased or under_scored, as an argument. + +    This generator invokes the current system tool, which defaults to +    TestUnit. + +Example: +    `rails generate system_test GeneralStories` creates a GeneralStories +    system test in test/system/general_stories_test.rb diff --git a/railties/lib/rails/generators/rails/system_test/system_test_generator.rb b/railties/lib/rails/generators/rails/system_test/system_test_generator.rb new file mode 100644 index 0000000000..901120e892 --- /dev/null +++ b/railties/lib/rails/generators/rails/system_test/system_test_generator.rb @@ -0,0 +1,7 @@ +module Rails +  module Generators +    class SystemTestGenerator < NamedBase # :nodoc: +      hook_for :system_tests, as: :system +    end +  end +end diff --git a/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb b/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb index dea7e22196..118e0f1271 100644 --- a/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb +++ b/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb @@ -1,7 +1,9 @@  require 'test_helper' +<% module_namespacing do -%>  class <%= class_name %>Test < ActionDispatch::IntegrationTest    # test "the truth" do    #   assert true    # end  end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/system/system_generator.rb b/railties/lib/rails/generators/test_unit/system/system_generator.rb new file mode 100644 index 0000000000..aec415a4e5 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/system/system_generator.rb @@ -0,0 +1,17 @@ +require "rails/generators/test_unit" + +module TestUnit # :nodoc: +  module Generators # :nodoc: +    class SystemGenerator < Base # :nodoc: +      check_class_collision suffix: "Test" + +      def create_test_files +        if !File.exist?(File.join("test/application_system_test_case.rb")) +          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") +      end +    end +  end +end diff --git a/railties/lib/rails/generators/test_unit/system/templates/application_system_test_case.rb b/railties/lib/rails/generators/test_unit/system/templates/application_system_test_case.rb new file mode 100644 index 0000000000..d19212abd5 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/system/templates/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase +  driven_by :selenium, using: :chrome, screen_size: [1400, 1400] +end diff --git a/railties/lib/rails/generators/test_unit/system/templates/system_test.rb b/railties/lib/rails/generators/test_unit/system/templates/system_test.rb new file mode 100644 index 0000000000..b5ce2ba5c8 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/system/templates/system_test.rb @@ -0,0 +1,9 @@ +require "application_system_test_case" + +class <%= class_name.pluralize %>Test < ApplicationSystemTestCase +  # test "visiting the index" do +  #   visit <%= plural_table_name %>_url +  # +  #   assert_selector "h1", text: "<%= class_name %>" +  # end +end  | 
