1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Commit graph

16 commits

Author SHA1 Message Date
George Claghorn
d5a2f7ec14
Mirror direct uploads 2019-05-22 15:07:35 -04:00
Abhishek Chandrasekhar
32438ed64f [ActiveStorage] Ensure that the _blob association is properly loaded when attaching ::One
Consider a model with `One` and `Many` attachments configured:

    class User < ActiveRecord::Base
      has_one_attached :avatar
      has_many_attached :highlights
    end

=== One Attachment

After attaching `One` attachment (`:avatar`), we can see that the associated
`_blob` record (`:avatar_blob`) still returns as `nil`.

    user.avatar.attach(blob)
    user.avatar_attachment.present?  => true
    user.avatar_blob.present?        => false    # Incorrect!

This is a false negative. It happens because after the attachment and blob
are built:

  1. The record already has its `_blob` association loaded, as `nil`
  2. the `::Attachment` is associated with the record but the `::Blob` only gets
    associated with the `::Attachment`, not the record itself

In reality, the blob does in fact exist. We can verify this as follows:

    user.avatar.attach(blob)
    user.avatar_attachment.blob.present?    => true  # Blob does exist!

The fix in this change is to simply assign the `::Blob` when assigning
the `::Attachment`. After this fix is applied, we correctly observe:

    user.avatar.attach(blob)
    user.avatar_attachment.present?  => true
    user.avatar_blob.present?        => true    # Woohoo!

=== Many Attachments

We don't see this issue with `Many` attachments because the `_blob` association
is already loaded as part of attaching more/newer blobs.

    user.highlights.attach(blob)
    user.highlights_attachments.any?    => true
    user.highlights_blobs.any?          => true
2019-02-26 11:10:38 -05:00
Ryuta Kamizono
8034dde023 Module#{define_method,alias_method,undef_method,remove_method} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14133
2018-12-21 01:39:18 +09:00
Jasper Martin
934fccd522 Ignore ActiveRecord::InvalidForeignKey in ActiveStorage::Blob#purge
Do nothing instead of raising an error when it’s called on an attached blob.
2018-07-26 09:24:31 -04:00
George Claghorn
2ae3a29508 Add a foreign-key constraint to the attachments table for blobs 2018-07-19 20:43:33 -04:00
George Claghorn
af02b9b78f Remove unnecessary tap 2018-07-17 16:23:53 -04:00
George Claghorn
e13e16f5ad Fix replacing many attachments via assign and attach 2018-07-17 09:33:48 -04:00
George Claghorn
379d98dcd4 Correct test name 2018-07-16 15:59:17 -04:00
George Claghorn
36ec5428bf Fix that successive ActiveStorage::Attached::Many#attach calls would overwrite previous attachments 2018-07-16 15:57:43 -04:00
George Claghorn
1d13de4e39 Test removing attachments via #attach 2018-07-16 08:59:23 -04:00
George Claghorn
bd5eba1adf Clear attachment changes on reload 2018-07-13 14:48:45 -04:00
George Claghorn
28db8ba60e Implement ActiveStorage::Attached::{One,Many}#attach in terms of changes 2018-07-13 13:29:33 -04:00
George Claghorn
d20d6c7326 Fix that detaching could purge 2018-07-13 12:17:33 -04:00
George Claghorn
63951072af Fix analyzing new blobs from uploaded files on attach 2018-07-13 06:33:45 -04:00
George Claghorn
0b85123cd8 Raise an ArgumentError instead of a RuntimeError 2018-07-08 11:35:27 -04:00
George Claghorn
e8682c5bf0
Store newly-uploaded files on save rather than assignment 2018-07-07 23:25:33 -04:00