From 7a2780db6ef987b276c3d92046b55b58e117ab69 Mon Sep 17 00:00:00 2001 From: alex-sig <143193681+alex-sig@users.noreply.github.com> Date: Tue, 30 Jul 2024 15:57:54 +0530 Subject: [PATCH] OTWO-7292 Handle missing flag images for Stacks (#1796) --- app/helpers/stacks_helper.rb | 13 +++++++++-- test/helpers/stacks_helper_test.rb | 35 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/helpers/stacks_helper_test.rb 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