From 7c89948c416fbc32b59e33a0ab454545b4f6fed7 Mon Sep 17 00:00:00 2001 From: Anton Khamets Date: Mon, 7 Aug 2017 17:38:51 +0300 Subject: Extend image_tag to accept ActiveStorage Attachments and Variants (#30084) * Extend image_tag to accept ActiveStorage's Attachments and Variants * Flip resolve_image_source around * Add tests for the new use-cases of image_tag * Remove the higher-level test * Update image_tag documentation * Add error states into the test suite * Re-raise polymorhic_url's NoMethodError as ArgumentError * delegate_missing_to will raise DelegationError instead of NoMethodError --- activestorage/test/template/image_tag_test.rb | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 activestorage/test/template/image_tag_test.rb (limited to 'activestorage/test') diff --git a/activestorage/test/template/image_tag_test.rb b/activestorage/test/template/image_tag_test.rb new file mode 100644 index 0000000000..83c95c01a1 --- /dev/null +++ b/activestorage/test/template/image_tag_test.rb @@ -0,0 +1,40 @@ +require "test_helper" +require "database/setup" + +class User < ActiveRecord::Base + has_one_attached :avatar +end + +class ActiveStorage::ImageTagTest < ActionView::TestCase + tests ActionView::Helpers::AssetTagHelper + + setup do + @blob = create_image_blob filename: "racecar.jpg" + end + + test "blob" do + assert_dom_equal %(Racecar), image_tag(@blob) + end + + test "variant" do + variant = @blob.variant(resize: "100x100") + assert_dom_equal %(Racecar), image_tag(variant) + end + + test "attachment" do + attachment = ActiveStorage::Attachment.new(blob: @blob) + assert_dom_equal %(Racecar), image_tag(attachment) + end + + test "error when attachment's empty" do + @user = User.create!(name: "DHH") + + assert_not @user.avatar.attached? + assert_raises(ArgumentError) { image_tag(@user.avatar) } + end + + test "error when object can't be resolved into url" do + unresolvable_object = ActionView::Helpers::AssetTagHelper + assert_raises(ArgumentError) { image_tag(unresolvable_object) } + end +end -- cgit v1.2.3