diff options
Diffstat (limited to 'railties/lib/rails')
15 files changed, 100 insertions, 22 deletions
diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb index 09082282f3..aa5c0d0b5b 100644 --- a/railties/lib/rails/code_statistics.rb +++ b/railties/lib/rails/code_statistics.rb @@ -44,7 +44,7 @@ class CodeStatistics #:nodoc: Dir.foreach(directory) do |file_name| path = "#{directory}/#{file_name}" - if File.directory?(path) && (/^\./ !~ file_name) + if File.directory?(path) && !(/^\./.match?(file_name)) stats.add(calculate_directory_statistics(path, pattern)) elsif file_name&.match?(pattern) stats.add_by_file_path(path) diff --git a/railties/lib/rails/code_statistics_calculator.rb b/railties/lib/rails/code_statistics_calculator.rb index 85f86bdbd0..8dd415d9d1 100644 --- a/railties/lib/rails/code_statistics_calculator.rb +++ b/railties/lib/rails/code_statistics_calculator.rb @@ -58,20 +58,20 @@ class CodeStatisticsCalculator #:nodoc: @lines += 1 if comment_started - if patterns[:end_block_comment] && line =~ patterns[:end_block_comment] + if patterns[:end_block_comment] && patterns[:end_block_comment].match?(line) comment_started = false end next else - if patterns[:begin_block_comment] && line =~ patterns[:begin_block_comment] + if patterns[:begin_block_comment] && patterns[:begin_block_comment].match?(line) comment_started = true next end end - @classes += 1 if patterns[:class] && line =~ patterns[:class] - @methods += 1 if patterns[:method] && line =~ patterns[:method] - if line !~ /^\s*$/ && (patterns[:line_comment].nil? || line !~ patterns[:line_comment]) + @classes += 1 if patterns[:class] && patterns[:class].match?(line) + @methods += 1 if patterns[:method] && patterns[:method].match?(line) + if !line.match?(/^\s*$/) && (patterns[:line_comment].nil? || !line.match?(patterns[:line_comment])) @code_lines += 1 end end diff --git a/railties/lib/rails/command/base.rb b/railties/lib/rails/command/base.rb index a22b198c66..415bab199f 100644 --- a/railties/lib/rails/command/base.rb +++ b/railties/lib/rails/command/base.rb @@ -52,7 +52,7 @@ module Rails def inherited(base) #:nodoc: super - if base.name && base.name !~ /Base$/ + if base.name && !base.name.match?(/Base$/) Rails::Command.subclasses << base end end diff --git a/railties/lib/rails/command/behavior.rb b/railties/lib/rails/command/behavior.rb index 7fb2a99e67..90650059f4 100644 --- a/railties/lib/rails/command/behavior.rb +++ b/railties/lib/rails/command/behavior.rb @@ -44,7 +44,7 @@ module Rails require path return rescue LoadError => e - raise unless e.message =~ /#{Regexp.escape(path)}$/ + raise unless /#{Regexp.escape(path)}$/.match?(e.message) rescue Exception => e warn "[WARNING] Could not load #{command_type} #{path.inspect}. Error: #{e.message}.\n#{e.backtrace.join("\n")}" end diff --git a/railties/lib/rails/command/environment_argument.rb b/railties/lib/rails/command/environment_argument.rb index 9945fd1430..df3cc1b2bb 100644 --- a/railties/lib/rails/command/environment_argument.rb +++ b/railties/lib/rails/command/environment_argument.rb @@ -28,7 +28,7 @@ module Rails if available_environments.include? env env else - %w( production development test ).detect { |e| e =~ /^#{env}/ } || env + %w( production development test ).detect { |e| /^#{env}/.match?(e) } || env end end diff --git a/railties/lib/rails/command/helpers/pretty_credentials.rb b/railties/lib/rails/command/helpers/pretty_credentials.rb new file mode 100644 index 0000000000..873ed0e825 --- /dev/null +++ b/railties/lib/rails/command/helpers/pretty_credentials.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require "fileutils" + +module Rails + module Command + module Helpers + module PrettyCredentials + Error = Class.new(StandardError) + + def opt_in_pretty_credentials + unless already_answered? || already_opted_in? + answer = yes?("Would you like to make the credentials diff from git more readable in the future? [Y/n]") + end + + opt_in! if answer + FileUtils.touch(tracker) unless answer.nil? + rescue Error + say("Couldn't setup git to prettify the credentials diff") + end + + private + def already_answered? + tracker.exist? + end + + def already_opted_in? + system_call("git config --get 'diff.rails_credentials.textconv'", accepted_codes: [0, 1]) + end + + def opt_in! + system_call("git config diff.rails_credentials.textconv 'bin/rails credentials:show'", accepted_codes: [0]) + + git_attributes = Rails.root.join(".gitattributes") + File.open(git_attributes, "a+") do |file| + file.write(<<~EOM) + config/credentials/*.yml.enc diff=rails_credentials + config/credentials.yml.enc diff=rails_credentials + EOM + end + end + + def tracker + Rails.root.join("tmp", "rails_pretty_credentials") + end + + def system_call(command_line, accepted_codes:) + result = system(command_line) + raise(Error) if accepted_codes.exclude?($?.exitstatus) + result + end + end + end + end +end diff --git a/railties/lib/rails/commands/credentials/credentials_command.rb b/railties/lib/rails/commands/credentials/credentials_command.rb index e23a1b3008..772e105007 100644 --- a/railties/lib/rails/commands/credentials/credentials_command.rb +++ b/railties/lib/rails/commands/credentials/credentials_command.rb @@ -2,12 +2,15 @@ require "active_support" require "rails/command/helpers/editor" +require "rails/command/helpers/pretty_credentials" require "rails/command/environment_argument" +require "pathname" module Rails module Command class CredentialsCommand < Rails::Command::Base # :nodoc: include Helpers::Editor + include Helpers::PrettyCredentials include EnvironmentArgument self.environment_desc = "Uses credentials from config/credentials/:environment.yml.enc encrypted by config/credentials/:environment.key key" @@ -34,20 +37,29 @@ module Rails end say "File encrypted and saved." + opt_in_pretty_credentials rescue ActiveSupport::MessageEncryptor::InvalidMessage say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?" end - def show - extract_environment_option_from_argument(default_environment: nil) + def show(git_textconv_path = nil) + if git_textconv_path + default_environment = extract_environment_from_path(git_textconv_path) + fallback_message = File.read(git_textconv_path) + end + + extract_environment_option_from_argument(default_environment: default_environment) require_application! - say credentials.read.presence || missing_credentials_message + say credentials(git_textconv_path).read.presence || fallback_message || missing_credentials_message + rescue => e + raise(e) unless git_textconv_path + fallback_message end private - def credentials - Rails.application.encrypted(content_path, key_path: key_path) + def credentials(content = nil) + Rails.application.encrypted(content || content_path, key_path: key_path) end def ensure_encryption_key_has_been_added @@ -77,7 +89,6 @@ module Rails end end - def content_path options[:environment] ? "config/credentials/#{options[:environment]}.yml.enc" : "config/credentials.yml.enc" end @@ -86,6 +97,17 @@ module Rails options[:environment] ? "config/credentials/#{options[:environment]}.key" : "config/master.key" end + def extract_environment_from_path(path) + regex = %r{ + ([A-Za-z0-9]+) # match the environment + (?<!credentials) # don't match if file contains the word "credentials" + # in such case, the environment should be the default one + \.yml\.enc # look for `.yml.enc` file extension + }x + path.match(regex) + + Regexp.last_match(1) + end def encryption_key_file_generator require "rails/generators" diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 46f1d38b96..b4c0028b1f 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -362,7 +362,7 @@ module Rails base.called_from = begin call_stack = caller_locations.map { |l| l.absolute_path || l.path } - File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] }) + File.dirname(call_stack.detect { |p| !p.match?(%r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack]) }) end end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index dbfb7337f0..ed0215bda9 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -400,7 +400,7 @@ module Rails end def os_supports_listen_out_of_the_box? - RbConfig::CONFIG["host_os"] =~ /darwin|linux/ + /darwin|linux/.match?(RbConfig::CONFIG["host_os"]) end def run_bundle diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index a153923ce3..1d3c947cb0 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -233,7 +233,7 @@ module Rails # Invoke source_root so the default_source_root is set. base.source_root - if base.name && base.name !~ /Base$/ + if base.name && !base.name.match?(/Base$/) Rails::Generators.subclasses << base Rails::Generators.templates_path.each do |path| diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index 4e348be9be..377a5dfc65 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -131,7 +131,7 @@ module Rails end def foreign_key? - !!(name =~ /_id$/) + /_id$/.match?(name) end def reference? diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile.tt b/railties/lib/rails/generators/rails/app/templates/Gemfile.tt index cf5462f7dc..f13dab59b1 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile.tt +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile.tt @@ -72,7 +72,7 @@ group :test do # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end -<%- end -%> +<%- end -%> # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt index b8c1f21c0b..437bf84ce3 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt @@ -2,6 +2,7 @@ <html> <head> <title><%= camelized %></title> + <meta name="viewport" content="width=device-width,initial-scale=1"> <%%= csrf_meta_tags %> <%%= csp_meta_tag %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt index 59385cdf37..3c56b21b3c 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt @@ -1,7 +1,7 @@ # Be sure to restart your server when you modify this file. # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. -# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } +# Rails.backtrace_cleaner.add_silencer { |line| /my_noisy_library/.match?(line) } # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. # Rails.backtrace_cleaner.remove_silencers! diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 7b294751fc..b8bce8c772 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -61,7 +61,7 @@ module Rails private def extract_filters(argv) # Extract absolute and relative paths but skip -n /.*/ regexp filters. - argv.select { |arg| arg =~ %r%^/?\w+/% && !arg.end_with?("/") }.map do |path| + argv.select { |arg| %r%^/?\w+/%.match?(arg) && !arg.end_with?("/") }.map do |path| case when /(:\d+)+$/.match?(path) file, *lines = path.split(":") |