diff options
author | George Claghorn <george.claghorn@gmail.com> | 2017-11-20 18:06:06 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-20 18:06:06 -0500 |
commit | 1d24e47140356f136471d15e3ce3fa427f4430c2 (patch) | |
tree | e6939039d0c925d1a489c70d95135b441aad4765 /activestorage/test | |
parent | b22ee64b5b30c6d5039c292235e10b24b1057f6d (diff) | |
download | rails-1d24e47140356f136471d15e3ce3fa427f4430c2.tar.gz rails-1d24e47140356f136471d15e3ce3fa427f4430c2.tar.bz2 rails-1d24e47140356f136471d15e3ce3fa427f4430c2.zip |
Provide attachment writers
Permit creating a record and attaching files in a single step.
# Before:
User.create!(user_params.except(:avatar)).tap do |user|
user.avatar.attach(user_params[:avatar])
end
# After:
User.create!(user_params)
[Yoshiyuki Hirano & George Claghorn]
Diffstat (limited to 'activestorage/test')
-rw-r--r-- | activestorage/test/models/attachments_test.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb index edd68d3051..20eec3c220 100644 --- a/activestorage/test/models/attachments_test.rb +++ b/activestorage/test/models/attachments_test.rb @@ -76,6 +76,20 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "funky.jpg", user.avatar.filename.to_s end + test "build new record with attached blob" do + assert_no_difference -> { ActiveStorage::Attachment.count } do + @user = User.new(name: "Jason", avatar: { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }) + end + + assert @user.new_record? + assert @user.avatar.attached? + assert_equal "town.jpg", @user.avatar.filename.to_s + + @user.save! + assert @user.reload.avatar.attached? + assert_equal "town.jpg", @user.avatar.filename.to_s + end + test "access underlying associations of new blob" do @user.avatar.attach create_blob(filename: "funky.jpg") assert_equal @user, @user.avatar_attachment.record @@ -204,6 +218,24 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "country.jpg", user.highlights.second.filename.to_s end + test "build new record with attached blobs" do + assert_no_difference -> { ActiveStorage::Attachment.count } do + @user = User.new(name: "Jason", highlights: [ + { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, + { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" }]) + end + + assert @user.new_record? + assert @user.highlights.attached? + assert_equal "town.jpg", @user.highlights.first.filename.to_s + assert_equal "country.jpg", @user.highlights.second.filename.to_s + + @user.save! + assert @user.reload.highlights.attached? + assert_equal "town.jpg", @user.highlights.first.filename.to_s + assert_equal "country.jpg", @user.highlights.second.filename.to_s + end + test "find attached blobs" do @user.highlights.attach( { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, |