Active Storage 文件上传回调

问题描述 投票:0回答:7

模型上的活动存储文件是否有回调

当模型上的字段发生更改时,会调用

after_update
after_save
。但是,当您更新(或者更确切地说上传新文件)时,似乎没有调用回调?

上下文:

class Person < ApplicationRecord
  #name :string
  has_one_attached :id_document

  after_update :call_some_service

  def call_some_service
    #do something
  end
end

当上传新的

id_document
时,不会调用
after_update
,但是当人员的
name
更改时,会执行
after_update
回调

ruby-on-rails ruby rails-activestorage
7个回答
4
投票

目前看来,这种情况还没有回调。

您可以做的是创建一个模型来处理活动存储附件的创建,这是当您将文件附加到个人模型时创建的。

因此创建一个新模型

class ActiveStorageAttachment < ActiveRecord::Base
  after_update :after_update

  private
  def after_update
    if record_type == 'Person'
      record.do_something
    end
  end
end

您通常已经在数据库中创建了模型表,因此无需迁移,只需创建此模型即可


3
投票

就我而言,跟踪附件时间戳有效

class Person < ApplicationRecord
  has_one_attached :id_document

  after_save do    
    if id_document.attached? && (Time.now  - id_document.attachment.created_at)<5
      Rails.logger.info "id_document change detected"
    end
  end
end

2
投票

呃,我只是发表评论,但因为没有代表这是不可能的..

Uelb 的答案有效,但您需要修复注释中的错误并将其添加为初始化程序而不是模型。例如:

require 'active_storage/attachment'

class ActiveStorage::Attachment
  before_save :do_something

  def do_something
    puts 'yeah!'
  end
end

2
投票

这些都没有真正击中要害,但您可以通过阅读这篇博文来实现您想要的目标https://redgreen.no/2021/01/25/active-storage-callbacks.html

我能够修改那里的代码以处理附件而不是像这样的斑点

Rails.configuration.to_prepare do
  module ActiveStorage::Attachment::Callbacks
    # Gives us some convenient shortcuts, like `prepended`
    extend ActiveSupport::Concern

    # When prepended into a class, define our callback
    prepended do
      after_commit :attachment_changed, on: %i[create update]
    end

    # callback method
    def attachment_changed
      record.after_attachment_update(self) if record.respond_to? :after_attachment_update
    end
  end

  # After defining the module, call on ActiveStorage::Blob to prepend it in.
  ActiveStorage::Attachment.prepend ActiveStorage::Attachment::Callbacks
end


1
投票

@Uleb 的答案让我完成了 90%,但为了完成,我将发布我的最终解决方案。

我遇到的问题是我无法猴子修补该类(不知道为什么,即使按照@user10692737要求该类也没有帮助)

所以我复制了源代码(https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activestorage/app/models/active_storage/attachment.rb#L20

并修改它以包含回调

require "active_support/core_ext/module/delegation"

# Attachments associate records with blobs. Usually that's a one record-many blobs relationship,
# but it is possible to associate many different records with the same blob. If you're doing that,
# you'll want to declare with <tt>has_one/many_attached :thingy, dependent: false</tt>, so that destroying
# any one record won't destroy the blob as well. (Then you'll need to do your own garbage collecting, though).
class ActiveStorage::Attachment < ActiveRecord::Base
  self.table_name = "active_storage_attachments"

  belongs_to :record, polymorphic: true, touch: true
  belongs_to :blob, class_name: "ActiveStorage::Blob"

  delegate_missing_to :blob

  #CUSTOMIZED AT THE END:
  after_create_commit :analyze_blob_later, :identify_blob, :do_something

  # Synchronously purges the blob (deletes it from the configured service) and destroys the attachment.
  def purge
    blob.purge
    destroy
  end

  # Destroys the attachment and asynchronously purges the blob (deletes it from the configured service).
  def purge_later
    blob.purge_later
    destroy
  end


  private
    def identify_blob
      blob.identify
    end

    def analyze_blob_later
      blob.analyze_later unless blob.analyzed?
    end

    #CUSTOMIZED:
    def do_something

    end
end

不确定这是最好的方法,如果我找到更好的解决方案,我会更新


1
投票

我所做的就是在我的记录中添加回调:

after_touch :check_after_touch_data

如果添加、编辑或删除 ActiveStorage 对象,则会调用此函数。我使用此回调来检查是否发生了变化。


0
投票

您可以访问 Rails 7 中的“attachment_changes['name of your columns']”

因此,为了检查徽标是否发生更改,我将其用作

after_commit: saved_change_to_logo_attached?

def saved_change_to_logo_attached?
   attachment_changes['submitted_file'].present?
end
© www.soinside.com 2019 - 2024. All rights reserved.