lib/active_storage/attached/model.rb in activestorage-6.1.0.rc1 vs lib/active_storage/attached/model.rb in activestorage-6.1.0.rc2
- old
+ new
@@ -38,11 +38,18 @@
#
# class User < ActiveRecord::Base
# has_one_attached :avatar, service: :s3
# end
#
- def has_one_attached(name, dependent: :purge_later, service: nil)
+ # If you need to enable +strict_loading+ to prevent lazy loading of attachment,
+ # pass the +:strict_loading+ option. You can do:
+ #
+ # class User < ApplicationRecord
+ # has_one_attached :avatar, strict_loading: true
+ # end
+ #
+ def has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
validate_service_configuration(name, service)
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@@ -58,12 +65,12 @@
ActiveStorage::Attached::Changes::CreateOne.new("#{name}", self, attachable)
end
end
CODE
- has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: :destroy
- has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
+ has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: :destroy, strict_loading: strict_loading
+ has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading
scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) }
after_save { attachment_changes[name.to_s]&.save }
@@ -109,11 +116,18 @@
#
# class Gallery < ActiveRecord::Base
# has_many_attached :photos, service: :s3
# end
#
- def has_many_attached(name, dependent: :purge_later, service: nil)
+ # If you need to enable +strict_loading+ to prevent lazy loading of attachments,
+ # pass the +:strict_loading+ option. You can do:
+ #
+ # class Gallery < ApplicationRecord
+ # has_many_attached :photos, strict_loading: true
+ # end
+ #
+ def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
validate_service_configuration(name, service)
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@@ -136,21 +150,21 @@
end
end
end
CODE
- has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy do
+ has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy, strict_loading: strict_loading do
def purge
each(&:purge)
reset
end
def purge_later
each(&:purge_later)
reset
end
end
- has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
+ has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) }
after_save { attachment_changes[name.to_s]&.save }