aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/reflection.rb
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 /activerecord/lib/active_record/reflection.rb
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 'activerecord/lib/active_record/reflection.rb')
-rw-r--r--activerecord/lib/active_record/reflection.rb77
1 files changed, 60 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 22d195c9a4..231c785a30 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -11,27 +11,33 @@ module ActiveRecord
included do
class_attribute :_reflections, instance_writer: false, default: {}
class_attribute :aggregate_reflections, instance_writer: false, default: {}
+ class_attribute :attachment_reflections, instance_writer: false, default: {}
end
def self.create(macro, name, scope, options, ar)
- klass = \
- case macro
- when :composed_of
- AggregateReflection
- when :has_many
- HasManyReflection
- when :has_one
- HasOneReflection
- when :belongs_to
- BelongsToReflection
- else
- raise "Unsupported Macro: #{macro}"
- end
-
- reflection = klass.new(name, scope, options, ar)
+ reflection = reflection_class_for(macro).new(name, scope, options, ar)
options[:through] ? ThroughReflection.new(reflection) : reflection
end
+ def self.reflection_class_for(macro)
+ case macro
+ when :composed_of
+ AggregateReflection
+ when :has_many
+ HasManyReflection
+ when :has_one
+ HasOneReflection
+ when :belongs_to
+ BelongsToReflection
+ when :has_one_attached
+ HasOneAttachedReflection
+ when :has_many_attached
+ HasManyAttachedReflection
+ else
+ raise "Unsupported Macro: #{macro}"
+ end
+ end
+
def self.add_reflection(ar, name, reflection)
ar.clear_reflections_cache
name = name.to_s
@@ -42,14 +48,18 @@ module ActiveRecord
ar.aggregate_reflections = ar.aggregate_reflections.merge(name.to_s => reflection)
end
+ def self.add_attachment_reflection(ar, name, reflection)
+ ar.attachment_reflections.merge!(name.to_s => reflection)
+ end
+
# \Reflection enables the ability to examine the associations and aggregations of
# Active Record classes and objects. This information, for example,
# can be used in a form builder that takes an Active Record object
# and creates input fields for all of the attributes depending on their type
# and displays the associations to other objects.
#
- # MacroReflection class has info for AggregateReflection and AssociationReflection
- # classes.
+ # MacroReflection class has info for the AggregateReflection and
+ # AssociationReflection classes.
module ClassMethods
# Returns an array of AggregateReflection objects for all the aggregations in the class.
def reflect_on_all_aggregations
@@ -64,6 +74,21 @@ module ActiveRecord
aggregate_reflections[aggregation.to_s]
end
+ # 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
+
# Returns a Hash of name of the reflection as the key and an AssociationReflection as the value.
#
# Account.reflections # => {"balance" => AggregateReflection}
@@ -136,6 +161,8 @@ module ActiveRecord
# HasOneReflection
# BelongsToReflection
# HasAndBelongsToManyReflection
+ # HasOneAttachedReflection
+ # HasManyAttachedReflection
# ThroughReflection
# PolymorphicReflection
# RuntimeReflection
@@ -412,6 +439,22 @@ module ActiveRecord
end
end
+ # Holds all the metadata about a has_one_attached attachment as it was
+ # specified in the Active Record class.
+ class HasOneAttachedReflection < 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 < MacroReflection #:nodoc:
+ def macro
+ :has_many_attached
+ end
+ end
+
# Holds all the metadata about an association as it was specified in the
# Active Record class.
class AssociationReflection < MacroReflection #:nodoc: