Skip to content

Commit

Permalink
Provide attachment writers
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
georgeclaghorn committed Nov 20, 2017
1 parent b22ee64 commit 1d24e47
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions activestorage/lib/active_storage/attached/macros.rb
Expand Up @@ -32,6 +32,10 @@ def has_one_attached(name, dependent: :purge_later)
def #{name}
@active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
end
def #{name}=(attachable)
#{name}.attach(attachable)
end
CODE

has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
Expand Down Expand Up @@ -73,6 +77,10 @@ def has_many_attached(name, dependent: :purge_later)
def #{name}
@active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
end
def #{name}=(attachables)
#{name}.attach(attachables)
end
CODE

has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"
Expand Down
32 changes: 32 additions & 0 deletions activestorage/test/models/attachments_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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" },
Expand Down

1 comment on commit 1d24e47

@halo
Copy link

@halo halo commented on 1d24e47 Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your hard work with ASt, @georgeclaghorn

You just solved my feature request 😌 https://groups.google.com/forum/?hl=en#!msg/rubyonrails-core/t3FPqfc2AB8/_wJDFe-xAAAJ

Please sign in to comment.