Sha256: 2e426cde79c01f39c2e3a5dde7cdea04bf085e53ed61131832729309b7872db1

Contents?: true

Size: 1.98 KB

Versions: 39

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module ActiveStorage
  module Reflection
    class HasAttachedReflection < ActiveRecord::Reflection::MacroReflection # :nodoc:
      def variant(name, transformations)
        variants[name] = transformations
      end

      def variants
        @variants ||= {}
      end
    end

    # Holds all the metadata about a has_one_attached attachment as it was
    # specified in the Active Record class.
    class HasOneAttachedReflection < HasAttachedReflection # :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 < HasAttachedReflection # :nodoc:
      def macro
        :has_many_attached
      end
    end

    module ReflectionExtension # :nodoc:
      def add_attachment_reflection(model, name, reflection)
        model.attachment_reflections = model.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

Version data entries

39 entries across 37 versions & 5 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.8.7/lib/active_storage/reflection.rb
activestorage-7.0.8.7 lib/active_storage/reflection.rb
activestorage-7.0.8.6 lib/active_storage/reflection.rb
activestorage-7.0.8.5 lib/active_storage/reflection.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activestorage-7.0.8.4/lib/active_storage/reflection.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/lib/active_storage/reflection.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/lib/active_storage/reflection.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/lib/active_storage/reflection.rb
activestorage-7.0.8.4 lib/active_storage/reflection.rb
activestorage-7.0.8.1 lib/active_storage/reflection.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.3.1/lib/active_storage/reflection.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/lib/active_storage/reflection.rb
activestorage-7.0.8 lib/active_storage/reflection.rb
activestorage-7.0.7.2 lib/active_storage/reflection.rb
activestorage-7.0.7.1 lib/active_storage/reflection.rb
activestorage-7.0.7 lib/active_storage/reflection.rb
activestorage-7.0.6 lib/active_storage/reflection.rb
activestorage-7.0.5.1 lib/active_storage/reflection.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activestorage-7.0.3.1/lib/active_storage/reflection.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/lib/active_storage/reflection.rb