Skip to content

Commit

Permalink
Merge pull request #30066 from claudiob/ast-module
Browse files Browse the repository at this point in the history
`module ActiveStorage`, not `ActiveStorage::Class`
  • Loading branch information
dhh committed Aug 5, 2017
2 parents 6df24c6 + 616f361 commit a8217bb
Show file tree
Hide file tree
Showing 12 changed files with 690 additions and 665 deletions.
46 changes: 24 additions & 22 deletions activestorage/lib/active_storage/attached.rb
Expand Up @@ -2,33 +2,35 @@
require "action_dispatch/http/upload"
require "active_support/core_ext/module/delegation"

module ActiveStorage
# Abstract baseclass for the concrete `ActiveStorage::Attached::One` and `ActiveStorage::Attached::Many`
# classes that both provide proxy access to the blob association for a record.
class ActiveStorage::Attached
attr_reader :name, :record
class Attached
attr_reader :name, :record

def initialize(name, record)
@name, @record = name, record
end
def initialize(name, record)
@name, @record = name, record
end

private
def create_blob_from(attachable)
case attachable
when ActiveStorage::Blob
attachable
when ActionDispatch::Http::UploadedFile
ActiveStorage::Blob.create_after_upload! \
io: attachable.open,
filename: attachable.original_filename,
content_type: attachable.content_type
when Hash
ActiveStorage::Blob.create_after_upload!(attachable)
when String
ActiveStorage::Blob.find_signed(attachable)
else
nil
private
def create_blob_from(attachable)
case attachable
when ActiveStorage::Blob
attachable
when ActionDispatch::Http::UploadedFile
ActiveStorage::Blob.create_after_upload! \
io: attachable.open,
filename: attachable.original_filename,
content_type: attachable.content_type
when Hash
ActiveStorage::Blob.create_after_upload!(attachable)
when String
ActiveStorage::Blob.find_signed(attachable)
else
nil
end
end
end
end
end

require "active_storage/attached/one"
Expand Down
144 changes: 73 additions & 71 deletions activestorage/lib/active_storage/attached/macros.rb
@@ -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
85 changes: 44 additions & 41 deletions activestorage/lib/active_storage/attached/many.rb
@@ -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

0 comments on commit a8217bb

Please sign in to comment.