Fix resizer strip on nil.
Alexander Merkulov
committed Mar 28, 2017
commit da3ffd5ab24f835004296f797a54a2613f7399b0
Showing 2
changed files with
23 additions
and 4 deletions
locomotive/steam/services/image_resizer_service.rb b/lib/locomotive/steam/services/image_resizer_service.rb
+4
-4
| @@ | @@ -26,25 +26,25 @@ module Locomotive |
| def fetch_file(source) | |
| return nil if source.blank? | |
| - | |
| url_or_path = get_url_or_path(source) | |
| if url_or_path =~ Steam::IsHTTP | |
| resizer.fetch_url(url_or_path) | |
| - | else |
| + | elsif url_or_path |
| path = url_or_path.sub(/(\?.*)$/, '') | |
| resizer.fetch_file(File.join(asset_path || '', path)) | |
| end | |
| end | |
| def get_url_or_path(source) | |
| - | if source.is_a?(Hash) |
| + | value = if source.is_a?(Hash) |
| source['url'] | |
| elsif source.respond_to?(:url) | |
| source.url | |
| else | |
| source | |
| - | end.strip |
| + | end |
| + | value.strip if value |
| end | |
| end | |
spec/unit/services/image_resizer_service_spec.rb
+19
-0
| @@ | @@ -68,6 +68,25 @@ describe Locomotive::Steam::ImageResizerService do |
| end | |
| + | describe 'nil file' do |
| + | let(:geometry) { '' } |
| + | let(:input) { nil } |
| + | |
| + | it { is_expected.to eq nil } |
| + | end |
| + | |
| + | describe 'nil hash file' do |
| + | let(:input) { { 'url' => nil } } |
| + | |
| + | it { is_expected.to eq nil } |
| + | end |
| + | |
| + | describe 'nil object file' do |
| + | let(:input) { instance_double('UploadedFile', url: nil) } |
| + | |
| + | it { is_expected.to eq nil } |
| + | end |
| + | |
| describe 'additional filters' do | |
| let(:input) { '/sites/42/theme/images/banner.png' } | |
| let (:filters) { [{"quality" => 70, "auto_orient" => true, "filters" => "-swirl 180"}] } | |