Sha256: 21254cf00186179e7d42ee5deae6ef9760be43bbe53094f9417238830124002d

Contents?: true

Size: 1.04 KB

Versions: 39

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module ActiveStorage
  class Attached::Changes::CreateMany #:nodoc:
    attr_reader :name, :record, :attachables

    def initialize(name, record, attachables)
      @name, @record, @attachables = name, record, Array(attachables)
    end

    def attachments
      @attachments ||= subchanges.collect(&:attachment)
    end

    def blobs
      @blobs ||= subchanges.collect(&:blob)
    end

    def upload
      subchanges.each(&:upload)
    end

    def save
      assign_associated_attachments
      reset_associated_blobs
    end

    private
      def subchanges
        @subchanges ||= attachables.collect { |attachable| build_subchange_from(attachable) }
      end

      def build_subchange_from(attachable)
        ActiveStorage::Attached::Changes::CreateOneOfMany.new(name, record, attachable)
      end


      def assign_associated_attachments
        record.public_send("#{name}_attachments=", attachments)
      end

      def reset_associated_blobs
        record.public_send("#{name}_blobs").reset
      end
  end
end

Version data entries

39 entries across 39 versions & 4 rubygems

Version Path
activestorage-6.0.3 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.3.rc1 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.2.2 lib/active_storage/attached/changes/create_many.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activestorage-6.0.2.1/lib/active_storage/attached/changes/create_many.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activestorage-6.0.2.1/lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.2.1 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.2 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.2.rc2 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.2.rc1 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.1 lib/active_storage/attached/changes/create_many.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activestorage-6.0.0/lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.1.rc1 lib/active_storage/attached/changes/create_many.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activestorage-6.0.0/lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0.rc2 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0.rc1 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0.beta3 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0.beta2 lib/active_storage/attached/changes/create_many.rb
activestorage-6.0.0.beta1 lib/active_storage/attached/changes/create_many.rb