Sha256: da7d111664986e79e5cbf19fb2f920087637210706c0c3eb54e98b3c1f07c45c

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Koi
  module Controller
    module HasAttachments
      extend ActiveSupport::Concern

      # Store attachments in the given object so that they can be preserved for
      # the next form submission.
      #
      # Example:
      #     def create
      #       @document = Document.new(document_params)
      #       if @document.save
      #         redirect_to [:admin, @document], status: :see_other
      #       else
      #         store_attachments(@document)
      #         render :new, status: :unprocessable_entity
      #       end
      #     end
      #
      # @param resource [ActiveRecord::Base] The object being edited
      def save_attachments!(resource)
        resource.attachment_changes.each_value do |change|
          case change
          when ActiveStorage::Attached::Changes::CreateOne
            change.upload
            change.blob.save!
          when ActiveStorage::Attached::Changes::CreateMany
            change.upload
            change.blobs.each(&:save!)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
katalyst-koi-4.7.3 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.6.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.5.9 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.5.8 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.5.7 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.5.6 app/controllers/concerns/koi/controller/has_attachments.rb