aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorKevin Deisz <kevin.deisz@gmail.com>2018-05-29 15:57:56 -0400
committerKevin Deisz <kevin.deisz@gmail.com>2018-05-30 13:28:22 -0400
commitbc3b6ea461ee82a4c34877168fd498b81f12763c (patch)
tree96dd7f0ddf3b1bfd95a345b8ed41cecbe2939d61 /activestorage
parent0d7f13a622259ccafe3822d4e4eb7177107974ab (diff)
downloadrails-bc3b6ea461ee82a4c34877168fd498b81f12763c.tar.gz
rails-bc3b6ea461ee82a4c34877168fd498b81f12763c.tar.bz2
rails-bc3b6ea461ee82a4c34877168fd498b81f12763c.zip
Reflection for attachments
Add the ability to reflect on the attachments that have been defined using ActiveRecord::Reflection.
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/CHANGELOG.md5
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb12
-rw-r--r--activestorage/test/models/reflection_test.rb29
3 files changed, 46 insertions, 0 deletions
diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md
index c8911fe611..4aa551781b 100644
--- a/activestorage/CHANGELOG.md
+++ b/activestorage/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Add the ability to reflect on defined attachments using the existing
+ ActiveRecord reflection mechanism.
+
+ *Kevin Deisz*
+
* Variant arguments of `false` or `nil` will no longer be passed to the
processor. For example, the following will not have the monochrome
variation applied:
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index f99cf35680..6ad9fc43d7 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -48,6 +48,12 @@ module ActiveStorage
else
before_destroy { public_send(name).detach }
end
+
+ ActiveRecord::Reflection.add_attachment_reflection(
+ self,
+ name,
+ ActiveRecord::Reflection.create(:has_one_attached, name, nil, { dependent: dependent }, self)
+ )
end
# Specifies the relation between multiple attachments and the model.
@@ -105,6 +111,12 @@ module ActiveStorage
else
before_destroy { public_send(name).detach }
end
+
+ ActiveRecord::Reflection.add_attachment_reflection(
+ self,
+ name,
+ ActiveRecord::Reflection.create(:has_many_attached, name, nil, { dependent: dependent }, self)
+ )
end
end
end
diff --git a/activestorage/test/models/reflection_test.rb b/activestorage/test/models/reflection_test.rb
new file mode 100644
index 0000000000..1ddfafc0f0
--- /dev/null
+++ b/activestorage/test/models/reflection_test.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+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?(ActiveRecord::Reflection::HasOneAttachedReflection) ||
+ reflection.is_a?(ActiveRecord::Reflection::HasManyAttachedReflection)
+ end
+
+ assert expected_classes
+ end
+
+ test "allows reflecting on a singular has_one_attached attachment" do
+ reflection = User.reflect_on_attachment(:avatar)
+
+ assert_equal :avatar, reflection.name
+ assert_equal :has_one_attached, reflection.macro
+ end
+
+ test "allows reflecting on a singular has_many_attached attachment" do
+ reflection = User.reflect_on_attachment(:highlights)
+
+ assert_equal :highlights, reflection.name
+ assert_equal :has_many_attached, reflection.macro
+ end
+end