@@ -146,4 +146,59 @@ def value
146146 expect ( rails_admin_field . with ( object : FieldTest . new ( string_field : 'dummy.txt' ) ) . attachments . map ( &:value ) ) . to eq [ 'dummy.txt' ]
147147 end
148148 end
149+
150+ describe '#image?' do
151+ let ( :filename ) { 'dummy.txt' }
152+ let :rails_admin_field do
153+ RailsAdmin . config ( 'FieldTest' ) . fields . detect do |f |
154+ f . name == :string_field
155+ end . with (
156+ object : FieldTest . new ( string_field : filename ) ,
157+ view : ApplicationController . new . view_context ,
158+ )
159+ end
160+ before do
161+ RailsAdmin . config FieldTest do
162+ field :string_field , :multiple_file_upload do
163+ attachment do
164+ def resource_url
165+ "http://example.com/#{ value } "
166+ end
167+ end
168+ end
169+ end
170+ end
171+
172+ context 'when the file is not an image' do
173+ let ( :filename ) { 'dummy.txt' }
174+
175+ it 'returns false' do
176+ expect ( rails_admin_field . attachments . first . image? ) . to be false
177+ end
178+ end
179+
180+ context 'when the file is an image' do
181+ let ( :filename ) { 'dummy.jpg' }
182+
183+ it 'returns true' do
184+ expect ( rails_admin_field . attachments . first . image? ) . to be true
185+ end
186+ end
187+
188+ context 'when the file is an image but suffixed with a query string' do
189+ let ( :filename ) { 'dummy.jpg?foo=bar' }
190+
191+ it 'returns true' do
192+ expect ( rails_admin_field . attachments . first . image? ) . to be true
193+ end
194+ end
195+
196+ context "when the filename can't be represented as a valid URI" do
197+ let ( :filename ) { 'du mmy.jpg' }
198+
199+ it 'returns false' do
200+ expect ( rails_admin_field . attachments . first . image? ) . to be false
201+ end
202+ end
203+ end
149204end
0 commit comments