Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30066 from claudiob/ast-module
`module ActiveStorage`, not `ActiveStorage::Class`
- Loading branch information
Showing
12 changed files
with
690 additions
and
665 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,84 @@ | ||
# Provides the class-level DSL for declaring that an Active Record model has attached blobs. | ||
module ActiveStorage::Attached::Macros | ||
# Specifies the relation between a single attachment and the model. | ||
# | ||
# class User < ActiveRecord::Base | ||
# has_one_attached :avatar | ||
# end | ||
# | ||
# There is no column defined on the model side, Active Storage takes | ||
# care of the mapping between your records and the attachment. | ||
# | ||
# Under the covers, this relationship is implemented as a `has_one` association to a | ||
# `ActiveStorage::Attachment` record and a `has_one-through` association to a | ||
# `ActiveStorage::Blob` record. These associations are available as `avatar_attachment` | ||
# and `avatar_blob`. But you shouldn't need to work with these associations directly in | ||
# most circumstances. | ||
# | ||
# The system has been designed to having you go through the `ActiveStorage::Attached::One` | ||
# proxy that provides the dynamic proxy to the associations and factory methods, like `#attach`. | ||
# | ||
# If the +:dependent+ option isn't set, the attachment will be purged | ||
# (i.e. destroyed) whenever the record is destroyed. | ||
def has_one_attached(name, dependent: :purge_later) | ||
define_method(name) do | ||
if instance_variable_defined?("@active_storage_attached_#{name}") | ||
instance_variable_get("@active_storage_attached_#{name}") | ||
else | ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self)) | ||
module ActiveStorage | ||
# Provides the class-level DSL for declaring that an Active Record model has attached blobs. | ||
module Attached::Macros | ||
# Specifies the relation between a single attachment and the model. | ||
# | ||
# class User < ActiveRecord::Base | ||
# has_one_attached :avatar | ||
# end | ||
# | ||
# There is no column defined on the model side, Active Storage takes | ||
# care of the mapping between your records and the attachment. | ||
# | ||
# Under the covers, this relationship is implemented as a `has_one` association to a | ||
# `ActiveStorage::Attachment` record and a `has_one-through` association to a | ||
# `ActiveStorage::Blob` record. These associations are available as `avatar_attachment` | ||
# and `avatar_blob`. But you shouldn't need to work with these associations directly in | ||
# most circumstances. | ||
# | ||
# The system has been designed to having you go through the `ActiveStorage::Attached::One` | ||
# proxy that provides the dynamic proxy to the associations and factory methods, like `#attach`. | ||
# | ||
# If the +:dependent+ option isn't set, the attachment will be purged | ||
# (i.e. destroyed) whenever the record is destroyed. | ||
def has_one_attached(name, dependent: :purge_later) | ||
define_method(name) do | ||
if instance_variable_defined?("@active_storage_attached_#{name}") | ||
instance_variable_get("@active_storage_attached_#{name}") | ||
else | ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self)) | ||
end | ||
end | ||
end | ||
|
||
has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record | ||
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 | ||
has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob | ||
|
||
if dependent == :purge_later | ||
before_destroy { public_send(name).purge_later } | ||
if dependent == :purge_later | ||
before_destroy { public_send(name).purge_later } | ||
end | ||
end | ||
end | ||
|
||
# Specifies the relation between multiple attachments and the model. | ||
# | ||
# class Gallery < ActiveRecord::Base | ||
# has_many_attached :photos | ||
# end | ||
# | ||
# There are no columns defined on the model side, Active Storage takes | ||
# care of the mapping between your records and the attachments. | ||
# | ||
# To avoid N+1 queries, you can include the attached blobs in your query like so: | ||
# | ||
# Gallery.where(user: Current.user).with_attached_photos | ||
# | ||
# Under the covers, this relationship is implemented as a `has_many` association to a | ||
# `ActiveStorage::Attachment` record and a `has_many-through` association to a | ||
# `ActiveStorage::Blob` record. These associations are available as `photos_attachments` | ||
# and `photos_blobs`. But you shouldn't need to work with these associations directly in | ||
# most circumstances. | ||
# | ||
# The system has been designed to having you go through the `ActiveStorage::Attached::Many` | ||
# proxy that provides the dynamic proxy to the associations and factory methods, like `#attach`. | ||
# | ||
# If the +:dependent+ option isn't set, all the attachments will be purged | ||
# (i.e. destroyed) whenever the record is destroyed. | ||
def has_many_attached(name, dependent: :purge_later) | ||
define_method(name) do | ||
if instance_variable_defined?("@active_storage_attached_#{name}") | ||
instance_variable_get("@active_storage_attached_#{name}") | ||
else | ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self)) | ||
# Specifies the relation between multiple attachments and the model. | ||
# | ||
# class Gallery < ActiveRecord::Base | ||
# has_many_attached :photos | ||
# end | ||
# | ||
# There are no columns defined on the model side, Active Storage takes | ||
# care of the mapping between your records and the attachments. | ||
# | ||
# To avoid N+1 queries, you can include the attached blobs in your query like so: | ||
# | ||
# Gallery.where(user: Current.user).with_attached_photos | ||
# | ||
# Under the covers, this relationship is implemented as a `has_many` association to a | ||
# `ActiveStorage::Attachment` record and a `has_many-through` association to a | ||
# `ActiveStorage::Blob` record. These associations are available as `photos_attachments` | ||
# and `photos_blobs`. But you shouldn't need to work with these associations directly in | ||
# most circumstances. | ||
# | ||
# The system has been designed to having you go through the `ActiveStorage::Attached::Many` | ||
# proxy that provides the dynamic proxy to the associations and factory methods, like `#attach`. | ||
# | ||
# If the +:dependent+ option isn't set, all the attachments will be purged | ||
# (i.e. destroyed) whenever the record is destroyed. | ||
def has_many_attached(name, dependent: :purge_later) | ||
define_method(name) do | ||
if instance_variable_defined?("@active_storage_attached_#{name}") | ||
instance_variable_get("@active_storage_attached_#{name}") | ||
else | ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self)) | ||
end | ||
end | ||
end | ||
|
||
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment" | ||
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob | ||
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment" | ||
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob | ||
|
||
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) } | ||
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) } | ||
|
||
if dependent == :purge_later | ||
before_destroy { public_send(name).purge_later } | ||
if dependent == :purge_later | ||
before_destroy { public_send(name).purge_later } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
# Decorated proxy object representing of multiple attachments to a model. | ||
class ActiveStorage::Attached::Many < ActiveStorage::Attached | ||
delegate_missing_to :attachments | ||
module ActiveStorage | ||
# Decorated proxy object representing of multiple attachments to a model. | ||
class Attached::Many < Attached | ||
delegate_missing_to :attachments | ||
|
||
# Returns all the associated attachment records. | ||
# | ||
# All methods called on this proxy object that aren't listed here will automatically be delegated to `attachments`. | ||
def attachments | ||
record.public_send("#{name}_attachments") | ||
end | ||
# Returns all the associated attachment records. | ||
# | ||
# All methods called on this proxy object that aren't listed here will automatically be delegated to `attachments`. | ||
def attachments | ||
record.public_send("#{name}_attachments") | ||
end | ||
|
||
# Associates one or several attachments with the current record, saving them to the database. | ||
# Examples: | ||
# | ||
# document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects | ||
# document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload | ||
# document.images.attach(io: File.open("~/racecar.jpg"), filename: "racecar.jpg", content_type: "image/jpg") | ||
# document.images.attach([ first_blob, second_blob ]) | ||
def attach(*attachables) | ||
attachables.flatten.collect do |attachable| | ||
attachments.create!(name: name, blob: create_blob_from(attachable)) | ||
# Associates one or several attachments with the current record, saving them to the database. | ||
# Examples: | ||
# | ||
# document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects | ||
# document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload | ||
# document.images.attach(io: File.open("~/racecar.jpg"), filename: "racecar.jpg", content_type: "image/jpg") | ||
# document.images.attach([ first_blob, second_blob ]) | ||
def attach(*attachables) | ||
attachables.flatten.collect do |attachable| | ||
attachments.create!(name: name, blob: create_blob_from(attachable)) | ||
end | ||
end | ||
end | ||
|
||
# Returns true if any attachments has been made. | ||
# | ||
# class Gallery < ActiveRecord::Base | ||
# has_many_attached :photos | ||
# end | ||
# | ||
# Gallery.new.photos.attached? # => false | ||
def attached? | ||
attachments.any? | ||
end | ||
# Returns true if any attachments has been made. | ||
# | ||
# class Gallery < ActiveRecord::Base | ||
# has_many_attached :photos | ||
# end | ||
# | ||
# Gallery.new.photos.attached? # => false | ||
def attached? | ||
attachments.any? | ||
end | ||
|
||
# Directly purges each associated attachment (i.e. destroys the blobs and | ||
# attachments and deletes the files on the service). | ||
def purge | ||
if attached? | ||
attachments.each(&:purge) | ||
attachments.reload | ||
# Directly purges each associated attachment (i.e. destroys the blobs and | ||
# attachments and deletes the files on the service). | ||
def purge | ||
if attached? | ||
attachments.each(&:purge) | ||
attachments.reload | ||
end | ||
end | ||
end | ||
|
||
# Purges each associated attachment through the queuing system. | ||
def purge_later | ||
if attached? | ||
attachments.each(&:purge_later) | ||
# Purges each associated attachment through the queuing system. | ||
def purge_later | ||
if attached? | ||
attachments.each(&:purge_later) | ||
end | ||
end | ||
end | ||
end | ||
|
Oops, something went wrong.