spree-contrib / spree_digital

A Spree extension to enable downloadable products
http://spreecommerce.org
MIT License
116 stars 97 forks source link

ActiveStorage does not respond to expiring_url #125

Open coneybeare opened 4 years ago

coneybeare commented 4 years ago

Problematic line: https://github.com/spree-contrib/spree_digital/blob/31f31d6ad592e361816bb1b9dddbed215996da58/app/controllers/spree/digitals_controller.rb#L7

2.7.1 :004 > digital.attachment.expiring_url
  ActiveStorage::Attachment Load (0.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1021 AND `active_storage_attachments`.`record_type` = 'Spree::Digital' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ActiveStorage::Blob Load (0.6ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 6585 LIMIT 1
Traceback (most recent call last):
        1: from (irb):4
NoMethodError (undefined method `expiring_url' for #<ActiveStorage::Attached::One:0x00001256bd908050>)
AlexanderMoiseev commented 3 years ago

hey, stumbled upon this issue, are there any plans or estimations on fixing it? or may be some workaround exists? I suppose recently spree migrated to active storage, but spree_digital uses papperclip's methods? If so, do you have plans to introduce spree_digital with active storage support?

Tashows commented 3 years ago
# app/controllers/spree/digitals_controller_decorator.rb
module Spree
  module DigitalsControllerDecorator
    def show
      if attachment.present?
        if digital_link.authorize!
          if Rails.application.config.active_storage.service == :amazon
            redirect_to main_app.rails_blob_path(attachment, disposition: "attachment") and return
          else
            send_file(
              ActiveStorage::Blob.service.path_for(attachment.key),
              filename: attachment.record.attachment_file_name,
              type: attachment.record.attachment_content_type,
              status: :ok
            ) and return
          end
        end
      else
        Rails.logger.error 'Missing Digital Item: attachment'
      end

      render :unauthorized
    end
  end
end

::Spree::DigitalsController.prepend Spree::DigitalsControllerDecorator unless ::Spree::DigitalsController.included_modules.include? Spree::DigitalsControllerDecorator