2018-05-31 09:33:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
|
|
|
module Reflection
|
2020-05-04 06:01:47 -04:00
|
|
|
class HasAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
|
|
|
|
def variant(name, transformations)
|
|
|
|
variants[name] = transformations
|
|
|
|
end
|
|
|
|
|
|
|
|
def variants
|
|
|
|
@variants ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 09:33:14 -04:00
|
|
|
# Holds all the metadata about a has_one_attached attachment as it was
|
|
|
|
# specified in the Active Record class.
|
2020-05-04 06:01:47 -04:00
|
|
|
class HasOneAttachedReflection < HasAttachedReflection #:nodoc:
|
2018-05-31 09:33:14 -04:00
|
|
|
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.
|
2020-05-04 06:01:47 -04:00
|
|
|
class HasManyAttachedReflection < HasAttachedReflection #:nodoc:
|
2018-05-31 09:33:14 -04:00
|
|
|
def macro
|
|
|
|
:has_many_attached
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 21:15:51 -04:00
|
|
|
module ReflectionExtension # :nodoc:
|
2018-07-07 17:04:47 -04:00
|
|
|
def add_attachment_reflection(model, name, reflection)
|
|
|
|
model.attachment_reflections = model.attachment_reflections.merge(name.to_s => reflection)
|
2018-05-31 09:33:14 -04:00
|
|
|
end
|
2018-05-31 21:15:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def reflection_class_for(macro)
|
|
|
|
case macro
|
|
|
|
when :has_one_attached
|
|
|
|
HasOneAttachedReflection
|
|
|
|
when :has_many_attached
|
|
|
|
HasManyAttachedReflection
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2018-05-31 09:33:14 -04:00
|
|
|
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
|