Skip to content

Commit

Permalink
Support ActiveStorage on Rails 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tegon committed Sep 27, 2019
1 parent f8c1ce0 commit 3a4c01d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ def file_method?(attribute_name)
end

def activestorage?(attribute_name)
@object.class.try(:reflect_on_attachment, attribute_name).present?
@object.class.try(:reflect_on_attachment, attribute_name).present? ||
@object.class.try(:reflect_on_association, "#{attribute_name}_attachment").present? ||
@object.class.try(:reflect_on_association, "#{attribute_name}_attachments").present?
end

def shrine?(attribute_name)
Expand Down
10 changes: 10 additions & 0 deletions test/form_builder/general_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ def with_custom_form_for(object, *args, &block)
assert_select 'form input#user_with_attachment_avatar.file'
end

test 'builder generates file input for activestorage in Rails 5.2' do
with_form_for UserWithAttachment.build, :image
assert_select 'form input#user_with_attachment_image.file'
end

test 'builder generates file input for activestorage in Rails 5.2 with multiple files' do
with_form_for UserWithAttachment.build, :images
assert_select 'form input#user_with_attachment_images.file'
end

test 'builder generates file input for carrierwave' do
with_form_for UserWithAttachment.build, :photo
assert_select 'form input#user_with_attachment_photo.file'
Expand Down
4 changes: 4 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ def self.reflect_on_attachment(attachment)
OpenStruct.new if attachment == :avatar
end

def self.reflect_on_association(association)
OpenStruct.new if ["image_attachment", "images_attachments"].include?(association)
end

def photo_url
"/photos/1.png"
end
Expand Down

0 comments on commit 3a4c01d

Please sign in to comment.