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/lib/active_storage | |
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/lib/active_storage')
-rw-r--r-- | activestorage/lib/active_storage/attached/macros.rb | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb index f0256718ac..2b38a9b887 100644 --- a/activestorage/lib/active_storage/attached/macros.rb +++ b/activestorage/lib/active_storage/attached/macros.rb @@ -32,6 +32,10 @@ module ActiveStorage 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 @@ -73,6 +77,10 @@ module ActiveStorage 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" |