aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-06-01 14:24:06 -0400
committerGitHub <noreply@github.com>2018-06-01 14:24:06 -0400
commitc1844477a1461ffc9db3d899212f608c2a0c77c9 (patch)
tree204ef39c0a71ba5a96a79e6b18addd711aba1c0b /activestorage
parentc61d67296c2e8a0682779c2f4eb9acea0c4b4a7e (diff)
parent6c7e6abfaad149da02dbec4e4f2bd62c5d68805f (diff)
downloadrails-c1844477a1461ffc9db3d899212f608c2a0c77c9.tar.gz
rails-c1844477a1461ffc9db3d899212f608c2a0c77c9.tar.bz2
rails-c1844477a1461ffc9db3d899212f608c2a0c77c9.zip
Merge pull request #33018 from kddeisz/defined-attachments
ActiveStorage 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/lib/active_storage/engine.rb9
-rw-r--r--activestorage/lib/active_storage/reflection.rb64
-rw-r--r--activestorage/test/models/reflection_test.rb29
5 files changed, 119 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/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb
index 99588cdd4b..519b9ae283 100644
--- a/activestorage/lib/active_storage/engine.rb
+++ b/activestorage/lib/active_storage/engine.rb
@@ -10,6 +10,8 @@ require "active_storage/previewer/video_previewer"
require "active_storage/analyzer/image_analyzer"
require "active_storage/analyzer/video_analyzer"
+require "active_storage/reflection"
+
module ActiveStorage
class Engine < Rails::Engine # :nodoc:
isolate_namespace ActiveStorage
@@ -95,5 +97,12 @@ module ActiveStorage
end
end
end
+
+ initializer "active_storage.reflection" do
+ ActiveSupport.on_load(:active_record) do
+ include Reflection::ActiveRecordExtensions
+ ActiveRecord::Reflection.singleton_class.prepend(Reflection::ReflectionExtension)
+ end
+ end
end
end
diff --git a/activestorage/lib/active_storage/reflection.rb b/activestorage/lib/active_storage/reflection.rb
new file mode 100644
index 0000000000..04a1b20882
--- /dev/null
+++ b/activestorage/lib/active_storage/reflection.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+module ActiveStorage
+ module Reflection
+ # Holds all the metadata about a has_one_attached attachment as it was
+ # specified in the Active Record class.
+ class HasOneAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
+ def macro
+ :has_one_attached
+ end
+ end
+
+ # Holds all the metadata about a has_many_attached attachment as it was
+ # specified in the Active Record class.
+ class HasManyAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
+ def macro
+ :has_many_attached
+ end
+ end
+
+ module ReflectionExtension # :nodoc:
+ def add_attachment_reflection(ar, name, reflection)
+ ar.attachment_reflections.merge!(name.to_s => reflection)
+ end
+
+ private
+ def reflection_class_for(macro)
+ case macro
+ when :has_one_attached
+ HasOneAttachedReflection
+ when :has_many_attached
+ HasManyAttachedReflection
+ else
+ super
+ end
+ end
+ end
+
+ module ActiveRecordExtensions
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :attachment_reflections, instance_writer: false, default: {}
+ end
+
+ module ClassMethods
+ # Returns an array of reflection objects for all the attachments in the
+ # class.
+ def reflect_on_all_attachments
+ attachment_reflections.values
+ end
+
+ # Returns the reflection object for the named +attachment+.
+ #
+ # User.reflect_on_attachment(:avatar)
+ # # => the avatar reflection
+ #
+ def reflect_on_attachment(attachment)
+ attachment_reflections[attachment.to_s]
+ end
+ end
+ 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..da866ca996
--- /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?(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
+ 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