From 27f87b68b26e4ac68fa9bf05e6003c61cdca854d Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Fri, 7 Jul 2017 14:20:38 +0200 Subject: Add some documentation --- lib/active_storage/attached/macros.rb | 26 ++++++++++++++++++++++++-- lib/active_storage/attached/many.rb | 17 +++++++++++++++++ lib/active_storage/attached/one.rb | 17 +++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) (limited to 'lib/active_storage/attached') diff --git a/lib/active_storage/attached/macros.rb b/lib/active_storage/attached/macros.rb index 96493d1215..1e0f9a6b7e 100644 --- a/lib/active_storage/attached/macros.rb +++ b/lib/active_storage/attached/macros.rb @@ -1,7 +1,18 @@ 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. + # + # 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_get("@active_storage_attached_#{name}") || instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self)) end @@ -10,9 +21,20 @@ module ActiveStorage::Attached::Macros 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. + # + # 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_get("@active_storage_attached_#{name}") || instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self)) end diff --git a/lib/active_storage/attached/many.rb b/lib/active_storage/attached/many.rb index f1535dfbc6..99d980196a 100644 --- a/lib/active_storage/attached/many.rb +++ b/lib/active_storage/attached/many.rb @@ -1,20 +1,36 @@ +# Representation of multiple attachments to a model. class ActiveStorage::Attached::Many < ActiveStorage::Attached delegate_missing_to :attachments + # Returns all the associated attachment records. + # + # You don't have to call this method to access the attachments' methods as + # they are all available at the model level. def attachments @attachments ||= ActiveStorage::Attachment.where(record_gid: record.to_gid.to_s, name: name) end + # Associates one or several attachments with the current record, saving + # them to the database. def attach(*attachables) @attachments = attachments | Array(attachables).flatten.collect do |attachable| ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable)) end end + # Checks the presence of attachments. + # + # 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) @@ -22,6 +38,7 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached end end + # Purges each associated attachment through the queuing system. def purge_later if attached? attachments.each(&:purge_later) diff --git a/lib/active_storage/attached/one.rb b/lib/active_storage/attached/one.rb index d08d265992..80e4cb6234 100644 --- a/lib/active_storage/attached/one.rb +++ b/lib/active_storage/attached/one.rb @@ -1,18 +1,34 @@ +# Representation of a single attachment to a model. class ActiveStorage::Attached::One < ActiveStorage::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 @attachment ||= ActiveStorage::Attachment.find_by(record_gid: record.to_gid.to_s, name: name) end + # Associates a given attachment with the current record, saving it to the + # database. def attach(attachable) @attachment = ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable)) end + # Checks the presence of the attachment. + # + # class User < ActiveRecord::Base + # has_one_attached :avatar + # end + # + # User.new.avatar.attached? # => false def attached? attachment.present? 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 @@ -20,6 +36,7 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached end end + # Purges the attachment through the queuing system. def purge_later if attached? attachment.purge_later -- cgit v1.2.3