Sha256: e1c905988756cf9d6f1ee1423afe414d9572657fe9c47f71de5869970c16bcba

Contents?: true

Size: 1.07 KB

Versions: 30

Compression:

Stored size: 1.07 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_content
      #       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

30 entries across 30 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.15.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.14.3 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.14.2 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.14.1 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.14.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.13.2 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.13.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.6 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.5 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.4 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.3 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.2 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.1 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.12.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.11.2 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.11.1 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.11.0 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.10.3 app/controllers/concerns/koi/controller/has_attachments.rb
katalyst-koi-4.10.2 app/controllers/concerns/koi/controller/has_attachments.rb