aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/attached
diff options
context:
space:
mode:
authorclaudiob <claudiob@users.noreply.github.com>2017-08-04 16:33:40 -0700
committerclaudiob <claudiob@users.noreply.github.com>2017-08-04 16:40:10 -0700
commitbb7599a6c84aba44cbb5f21486ffdb4a549717dd (patch)
tree26dd9fba5a7f6c09fbfcec9fbf29b7088db88411 /activestorage/lib/active_storage/attached
parent552840660389e39f3ba8e47dcf35ab817c01cb48 (diff)
downloadrails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.tar.gz
rails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.tar.bz2
rails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.zip
`module ActiveStorage`, not `ActiveStorage::Class`
The reasons for this commit are: - uniformity with the other Rails libraries - (possibly) behave better with respect to autoloading - fix the index in the generated documentation Before this commit, run `rake rdoc` generates this left sidebar (ActiveStorage entries are indexed twice, both inside and outside the module): <img width="308" alt="before" src="https://user-images.githubusercontent.com/10076/28939523-7c087dec-7846-11e7-9289-38ed4a2930cd.png"> After this commit, run `rake rdoc` generates this left sidebar: (ActiveStorage entries are only indexed inside the module): <img width="303" alt="after" src="https://user-images.githubusercontent.com/10076/28939524-7c090be0-7846-11e7-8ee5-29dfecae548e.png">
Diffstat (limited to 'activestorage/lib/active_storage/attached')
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb134
-rw-r--r--activestorage/lib/active_storage/attached/many.rb85
-rw-r--r--activestorage/lib/active_storage/attached/one.rb96
3 files changed, 161 insertions, 154 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 89297e5bdf..fbd64b5edc 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -1,76 +1,78 @@
-# 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
- instance_variable_get("@active_storage_attached_#{name}") ||
- instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
- end
+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
+ instance_variable_get("@active_storage_attached_#{name}") ||
+ instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
+ 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
- instance_variable_get("@active_storage_attached_#{name}") ||
- instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
- 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
+ instance_variable_get("@active_storage_attached_#{name}") ||
+ instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
+ 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
diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb
index 035cd9c091..82989e4605 100644
--- a/activestorage/lib/active_storage/attached/many.rb
+++ b/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
+
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index 0c522e856e..6b34b30f1c 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -1,56 +1,58 @@
-# Representation of a single attachment to a model.
-class ActiveStorage::Attached::One < ActiveStorage::Attached
- delegate_missing_to :attachment
+module ActiveStorage
+ # Representation of a single attachment to a model.
+ class Attached::One < Attached
+ delegate_missing_to :attachment
- # Returns the associated attachment record.
- #
- # You don't have to call this method to access the attachment's methods as
- # they are all available at the model level.
- def attachment
- record.public_send("#{name}_attachment")
- end
-
- # Associates a given attachment with the current record, saving it to the database.
- # Examples:
- #
- # person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
- # person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
- # person.avatar.attach(io: File.open("~/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
- # person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
- def attach(attachable)
- write_attachment \
- ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
- end
+ # Returns the associated attachment record.
+ #
+ # You don't have to call this method to access the attachment's methods as
+ # they are all available at the model level.
+ def attachment
+ record.public_send("#{name}_attachment")
+ end
- # Returns true if an attachment has been made.
- #
- # class User < ActiveRecord::Base
- # has_one_attached :avatar
- # end
- #
- # User.new.avatar.attached? # => false
- def attached?
- attachment.present?
- end
+ # Associates a given attachment with the current record, saving it to the database.
+ # Examples:
+ #
+ # person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
+ # person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
+ # person.avatar.attach(io: File.open("~/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
+ # person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
+ def attach(attachable)
+ write_attachment \
+ ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
+ end
- # Directly purges the attachment (i.e. destroys the blob and
- # attachment and deletes the file on the service).
- def purge
- if attached?
- attachment.purge
- write_attachment nil
+ # Returns true if an attachment has been made.
+ #
+ # class User < ActiveRecord::Base
+ # has_one_attached :avatar
+ # end
+ #
+ # User.new.avatar.attached? # => false
+ def attached?
+ attachment.present?
end
- end
- # Purges the attachment through the queuing system.
- def purge_later
- if attached?
- attachment.purge_later
+ # Directly purges the attachment (i.e. destroys the blob and
+ # attachment and deletes the file on the service).
+ def purge
+ if attached?
+ attachment.purge
+ write_attachment nil
+ end
end
- end
- private
- def write_attachment(attachment)
- record.public_send("#{name}_attachment=", attachment)
+ # Purges the attachment through the queuing system.
+ def purge_later
+ if attached?
+ attachment.purge_later
+ end
end
+
+ private
+ def write_attachment(attachment)
+ record.public_send("#{name}_attachment=", attachment)
+ end
+ end
end