Skip to content

Commit

Permalink
OTWO-7292 Handle missing flag images for Stacks (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sig authored Jul 30, 2024
1 parent 65052d3 commit 7a2780d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/helpers/stacks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions test/helpers/stacks_helper_test.rb
Original file line number Diff line number Diff line change
@@ -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 = '<img/>'
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

0 comments on commit 7a2780d

Please sign in to comment.