diff --git a/app/helpers/stacks_helper.rb b/app/helpers/stacks_helper.rb index b52b36520..85a9a78e9 100644 --- a/app/helpers/stacks_helper.rb +++ b/app/helpers/stacks_helper.rb @@ -14,8 +14,17 @@ def stack_similar_project_list(projects) end def stack_country_flag(code) - return '' unless code && code.size == 2 + img_relative_path = "flags/#{code.to_s.downcase}.gif" + return '' unless asset_exists?(img_relative_path) - haml_tag 'img', src: asset_url("flags/#{code.downcase}.gif") + haml_tag 'img', src: asset_url(img_relative_path) + end + + def asset_exists?(path) + if Rails.configuration.assets.compile + Rails.application.precompiled_assets.include? path + else + Rails.application.assets_manifest.assets[path].present? + end end end diff --git a/test/helpers/stacks_helper_test.rb b/test/helpers/stacks_helper_test.rb new file mode 100644 index 000000000..5ee9ac3bd --- /dev/null +++ b/test/helpers/stacks_helper_test.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'test_helper' + +class StacksHelperTest < ActionView::TestCase + include StacksHelper + + describe 'stack_country_flag' do + it 'must return an html with the flag image' do + dummy_html = '' + expects(:haml_tag).once.returns(dummy_html) + + _(stack_country_flag('Us')).must_equal dummy_html + end + + it 'must handle invalid country codes' do + _(stack_country_flag('invalid')).must_be :blank? + end + + it 'must handle blank codes' do + _(stack_country_flag(nil)).must_be :blank? + end + + it 'must handle missing flag images' do + _(stack_country_flag('uk')).must_be :blank? + end + + it 'must use the assets_manifest when assets are not compiled' do + Rails.configuration.assets.stubs(:compile).returns(false) + Rails.application.assets_manifest.expects(:assets).once.returns({}) + + _(stack_country_flag('')).must_be :blank? + end + end +end