aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/app_updater.rb3
-rw-r--r--railties/lib/rails/command/spellchecker.rb51
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb12
-rw-r--r--railties/lib/rails/generators/rails/app/templates/Gemfile.tt2
-rw-r--r--railties/lib/rails/tasks/framework.rake2
-rw-r--r--railties/lib/rails/templates/rails/mailers/email.html.erb17
6 files changed, 69 insertions, 18 deletions
diff --git a/railties/lib/rails/app_updater.rb b/railties/lib/rails/app_updater.rb
index a076d082d5..a243968a39 100644
--- a/railties/lib/rails/app_updater.rb
+++ b/railties/lib/rails/app_updater.rb
@@ -21,12 +21,15 @@ module Rails
private
def generator_options
options = { api: !!Rails.application.config.api_only, update: true }
+ options[:skip_yarn] = !File.exist?(Rails.root.join("bin", "yarn"))
options[:skip_active_record] = !defined?(ActiveRecord::Railtie)
options[:skip_active_storage] = !defined?(ActiveStorage::Engine) || !defined?(ActiveRecord::Railtie)
options[:skip_action_mailer] = !defined?(ActionMailer::Railtie)
options[:skip_action_cable] = !defined?(ActionCable::Engine)
options[:skip_sprockets] = !defined?(Sprockets::Railtie)
options[:skip_puma] = !defined?(Puma)
+ options[:skip_bootsnap] = !defined?(Bootsnap)
+ options[:skip_spring] = !defined?(Spring)
options
end
end
diff --git a/railties/lib/rails/command/spellchecker.rb b/railties/lib/rails/command/spellchecker.rb
index 154358cd45..04485097fa 100644
--- a/railties/lib/rails/command/spellchecker.rb
+++ b/railties/lib/rails/command/spellchecker.rb
@@ -3,8 +3,55 @@
module Rails
module Command
module Spellchecker # :nodoc:
- def self.suggest(word, from:)
- DidYouMean::SpellChecker.new(dictionary: from.map(&:to_s)).correct(word).first
+ class << self
+ def suggest(word, from:)
+ if defined?(DidYouMean::SpellChecker)
+ DidYouMean::SpellChecker.new(dictionary: from.map(&:to_s)).correct(word).first
+ else
+ from.sort_by { |w| levenshtein_distance(word, w) }.first
+ end
+ end
+
+ private
+
+ # This code is based directly on the Text gem implementation.
+ # Copyright (c) 2006-2013 Paul Battley, Michael Neumann, Tim Fletcher.
+ #
+ # Returns a value representing the "cost" of transforming str1 into str2.
+ def levenshtein_distance(str1, str2) # :doc:
+ s = str1
+ t = str2
+ n = s.length
+ m = t.length
+
+ return m if (0 == n)
+ return n if (0 == m)
+
+ d = (0..m).to_a
+ x = nil
+
+ # avoid duplicating an enumerable object in the loop
+ str2_codepoint_enumerable = str2.each_codepoint
+
+ str1.each_codepoint.with_index do |char1, i|
+ e = i + 1
+
+ str2_codepoint_enumerable.with_index do |char2, j|
+ cost = (char1 == char2) ? 0 : 1
+ x = [
+ d[j + 1] + 1, # insertion
+ e + 1, # deletion
+ d[j] + cost # substitution
+ ].min
+ d[j] = e
+ e = x
+ end
+
+ d[m] = x
+ end
+
+ x
+ end
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 395ac7ef2f..34067240d7 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -95,11 +95,9 @@ module Rails
end
def bin_when_updating
- bin_yarn_exist = File.exist?("bin/yarn")
-
bin
- if options[:api] && !bin_yarn_exist
+ if options[:skip_yarn]
remove_file "bin/yarn"
end
end
@@ -146,6 +144,10 @@ module Rails
template "config/storage.yml"
end
+ if options[:skip_sprockets] && !assets_config_exist
+ remove_file "config/initializers/assets.rb"
+ end
+
unless rack_cors_config_exist
remove_file "config/initializers/cors.rb"
end
@@ -155,10 +157,6 @@ module Rails
remove_file "config/initializers/cookies_serializer.rb"
end
- unless assets_config_exist
- remove_file "config/initializers/assets.rb"
- end
-
unless csp_config_exist
remove_file "config/initializers/content_security_policy.rb"
end
diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile.tt b/railties/lib/rails/generators/rails/app/templates/Gemfile.tt
index 5e7455cdc7..1567333023 100644
--- a/railties/lib/rails/generators/rails/app/templates/Gemfile.tt
+++ b/railties/lib/rails/generators/rails/app/templates/Gemfile.tt
@@ -23,7 +23,7 @@ ruby <%= "'#{RUBY_VERSION}'" -%>
<% unless skip_active_storage? -%>
# Use ActiveStorage variant
-# gem 'mini_magick', '~> 4.8'
+# gem 'image_processing', '~> 1.2'
<% end -%>
# Use Capistrano for deployment
diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake
index 7dfcd14bd0..1a3711c446 100644
--- a/railties/lib/rails/tasks/framework.rake
+++ b/railties/lib/rails/tasks/framework.rake
@@ -40,7 +40,7 @@ namespace :app do
namespace :update do
require "rails/app_updater"
- # desc "Update config/boot.rb from your current rails install"
+ # desc "Update config files from your current rails install"
task :configs do
Rails::AppUpdater.invoke_from_app_generator :create_boot_file
Rails::AppUpdater.invoke_from_app_generator :update_config_files
diff --git a/railties/lib/rails/templates/rails/mailers/email.html.erb b/railties/lib/rails/templates/rails/mailers/email.html.erb
index 2a41c29602..e46364ba8a 100644
--- a/railties/lib/rails/templates/rails/mailers/email.html.erb
+++ b/railties/lib/rails/templates/rails/mailers/email.html.erb
@@ -98,7 +98,7 @@
<dt>Format:</dt>
<% if @email.multipart? %>
<dd>
- <select id="part" onchange="refreshBody();">
+ <select id="part" onchange="refreshBody(false);">
<option <%= request.format == Mime[:html] ? 'selected' : '' %> value="<%= part_query('text/html') %>">View as HTML email</option>
<option <%= request.format == Mime[:text] ? 'selected' : '' %> value="<%= part_query('text/plain') %>">View as plain-text email</option>
</select>
@@ -110,7 +110,7 @@
<% if I18n.available_locales.count > 1 %>
<dt>Locale:</dt>
<dd>
- <select id="locale" onchange="refreshBody();">
+ <select id="locale" onchange="refreshBody(true);">
<% I18n.available_locales.each do |locale| %>
<option <%= I18n.locale == locale ? 'selected' : '' %> value="<%= locale_query(locale) %>"><%= locale %></option>
<% end %>
@@ -130,7 +130,7 @@
<% end %>
<script>
- function refreshBody() {
+ function refreshBody(reload) {
var part_select = document.querySelector('select#part');
var locale_select = document.querySelector('select#locale');
var iframe = document.getElementsByName('messageBody')[0];
@@ -146,10 +146,13 @@
}
iframe.contentWindow.location = fresh_location;
- if (history.replaceState) {
- var url = location.pathname.replace(/\.(txt|html)$/, '');
- var format = /html/.test(part_param) ? '.html' : '.txt';
- var state_to_replace = locale_param ? (url + format + '?' + locale_param) : (url + format);
+ var url = location.pathname.replace(/\.(txt|html)$/, '');
+ var format = /html/.test(part_param) ? '.html' : '.txt';
+ var state_to_replace = locale_param ? (url + format + '?' + locale_param) : (url + format);
+
+ if (reload) {
+ location.href = state_to_replace;
+ } else if (history.replaceState) {
window.history.replaceState({}, '', state_to_replace);
}
}