aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activestorage/lib/active_storage/reflection.rb4
-rw-r--r--activestorage/test/models/reflection_test.rb33
-rw-r--r--activestorage/test/test_helper.rb4
-rw-r--r--guides/source/engines.md3
-rw-r--r--railties/lib/rails/commands/server/server_command.rb36
-rw-r--r--railties/test/commands/server_test.rb5
6 files changed, 49 insertions, 36 deletions
diff --git a/activestorage/lib/active_storage/reflection.rb b/activestorage/lib/active_storage/reflection.rb
index 04a1b20882..ce248c88b5 100644
--- a/activestorage/lib/active_storage/reflection.rb
+++ b/activestorage/lib/active_storage/reflection.rb
@@ -19,8 +19,8 @@ module ActiveStorage
end
module ReflectionExtension # :nodoc:
- def add_attachment_reflection(ar, name, reflection)
- ar.attachment_reflections.merge!(name.to_s => reflection)
+ def add_attachment_reflection(model, name, reflection)
+ model.attachment_reflections = model.attachment_reflections.merge(name.to_s => reflection)
end
private
diff --git a/activestorage/test/models/reflection_test.rb b/activestorage/test/models/reflection_test.rb
index da866ca996..98606b0617 100644
--- a/activestorage/test/models/reflection_test.rb
+++ b/activestorage/test/models/reflection_test.rb
@@ -3,27 +3,32 @@
require "test_helper"
class ActiveStorage::ReflectionTest < ActiveSupport::TestCase
- test "allows reflecting for all attachment" do
- expected_classes =
- User.reflect_on_all_attachments.all? do |reflection|
- reflection.is_a?(ActiveStorage::Reflection::HasOneAttachedReflection) ||
- reflection.is_a?(ActiveStorage::Reflection::HasManyAttachedReflection)
- end
-
- assert expected_classes
- end
-
- test "allows reflecting on a singular has_one_attached attachment" do
+ test "reflecting on a singular attachment" do
reflection = User.reflect_on_attachment(:avatar)
-
+ assert_equal User, reflection.active_record
assert_equal :avatar, reflection.name
assert_equal :has_one_attached, reflection.macro
+ assert_equal :purge_later, reflection.options[:dependent]
end
- test "allows reflecting on a singular has_many_attached attachment" do
- reflection = User.reflect_on_attachment(:highlights)
+ test "reflection on a singular attachment with the same name as an attachment on another model" do
+ reflection = Group.reflect_on_attachment(:avatar)
+ assert_equal Group, reflection.active_record
+ end
+ test "reflecting on a collection attachment" do
+ reflection = User.reflect_on_attachment(:highlights)
+ assert_equal User, reflection.active_record
assert_equal :highlights, reflection.name
assert_equal :has_many_attached, reflection.macro
+ assert_equal :purge_later, reflection.options[:dependent]
+ end
+
+ test "reflecting on all attachments" do
+ reflections = User.reflect_on_all_attachments.sort_by(&:name)
+ assert_equal [ User ], reflections.collect(&:active_record).uniq
+ assert_equal %i[ avatar cover_photo highlights vlogs ], reflections.collect(&:name)
+ assert_equal %i[ has_one_attached has_one_attached has_many_attached has_many_attached ], reflections.collect(&:macro)
+ assert_equal [ :purge_later, false, :purge_later, false ], reflections.collect { |reflection| reflection.options[:dependent] }
end
end
diff --git a/activestorage/test/test_helper.rb b/activestorage/test/test_helper.rb
index 573a8e0b0b..9985a76832 100644
--- a/activestorage/test/test_helper.rb
+++ b/activestorage/test/test_helper.rb
@@ -92,3 +92,7 @@ class User < ActiveRecord::Base
has_many_attached :highlights
has_many_attached :vlogs, dependent: false
end
+
+class Group < ActiveRecord::Base
+ has_one_attached :avatar
+end
diff --git a/guides/source/engines.md b/guides/source/engines.md
index 78a699a15e..470bb50ba7 100644
--- a/guides/source/engines.md
+++ b/guides/source/engines.md
@@ -313,6 +313,9 @@ The engine that this guide covers provides submitting articles and commenting
functionality and follows a similar thread to the [Getting Started
Guide](getting_started.html), with some new twists.
+NOTE: For this section, make sure to run the commands in the root of the
+`blorgh` engine's directory.
+
### Generating an Article Resource
The first thing to generate for a blog engine is the `Article` model and related
diff --git a/railties/lib/rails/commands/server/server_command.rb b/railties/lib/rails/commands/server/server_command.rb
index cf17f0ef12..194db23f14 100644
--- a/railties/lib/rails/commands/server/server_command.rb
+++ b/railties/lib/rails/commands/server/server_command.rb
@@ -97,10 +97,6 @@ module Rails
end
end
- def restart_command
- "bin/rails server #{ARGV.join(' ')}"
- end
-
def use_puma?
server.to_s == "Rack::Handler::Puma"
end
@@ -132,15 +128,16 @@ module Rails
desc: "Specifies the Rack server used to run the application (thin/puma/webrick).", banner: :name
class_option :pid, aliases: "-P", type: :string, default: DEFAULT_PID_PATH,
desc: "Specifies the PID file."
- class_option "dev-caching", aliases: "-C", type: :boolean, default: nil,
+ class_option :dev_caching, aliases: "-C", type: :boolean, default: nil,
desc: "Specifies whether to perform caching in development."
- class_option "restart", type: :boolean, default: nil, hide: true
- class_option "early_hints", type: :boolean, default: nil, desc: "Enables HTTP/2 early hints."
+ class_option :restart, type: :boolean, default: nil, hide: true
+ class_option :early_hints, type: :boolean, default: nil, desc: "Enables HTTP/2 early hints."
- def initialize(args = [], local_options = {}, config = {})
- @original_options = local_options
+ def initialize(args, local_options, *)
super
- @using = deprecated_positional_rack_server(using) || options[:using]
+
+ @original_options = local_options - %w( --restart )
+ deprecate_positional_rack_server_and_rewrite_to_option(@original_options)
@log_stdout = options[:daemon].blank? && (options[:environment] || Rails.env) == "development"
end
@@ -177,7 +174,7 @@ module Rails
environment: environment,
daemonize: options[:daemon],
pid: pid,
- caching: options["dev-caching"],
+ caching: options[:dev_caching],
restart_cmd: restart_command,
early_hints: early_hints
}
@@ -210,7 +207,7 @@ module Rails
name = :Port
when :binding
name = :Host
- when :"dev-caching"
+ when :dev_caching
name = :caching
when :daemonize
name = :daemon
@@ -252,7 +249,7 @@ module Rails
end
def restart_command
- "bin/rails server #{using} #{@original_options.join(" ")} --restart"
+ "bin/rails server #{@original_options.join(" ")} --restart"
end
def early_hints
@@ -271,14 +268,19 @@ module Rails
FileUtils.rm_f(options[:pid]) if options[:restart]
end
- def deprecated_positional_rack_server(value)
- if value
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ def deprecate_positional_rack_server_and_rewrite_to_option(original_options)
+ if using
+ ActiveSupport::Deprecation.warn(<<~MSG)
Passing the Rack server name as a regular argument is deprecated
and will be removed in the next Rails version. Please, use the -u
option instead.
MSG
- value
+
+ original_options.concat [ '-u', using ]
+ else
+ # Use positional internally to get around Thor's immutable options.
+ # TODO: Replace `using` occurences with `options[:using]` after deprecation removal.
+ @using = options[:using]
end
end
diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb
index e7a56b3e6d..a20fece960 100644
--- a/railties/test/commands/server_test.rb
+++ b/railties/test/commands/server_test.rb
@@ -245,10 +245,9 @@ class Rails::Command::ServerCommandTest < ActiveSupport::TestCase
args = %w(-p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C)
ARGV.replace args
- options = parse_arguments(args)
- expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C --restart"
+ expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C --restart"
- assert_equal expected, options[:restart_cmd]
+ assert_equal expected, parse_arguments(args)[:restart_cmd]
ensure
ARGV.replace original_args
end