Module: CountryFlags::Helper

Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::AssetUrlHelper
Defined in:
lib/country_flags/helper.rb

Instance Method Summary collapse

Instance Method Details

#country_flag(country_code, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/country_flags/helper.rb', line 26

def country_flag( country_code, options = {} )
  options = {format: :png}.merge(options)

  country_name = NormalizeCountry( country_code, to: :short )

  return '' unless country_name

  image_tag country_flag_path(country_code, options[:format]),
    alt: country_name, title: country_name
end

#country_flag_path(country_code, format = :png) ⇒ Object

returns path to flag image use ISO-3166 Alpha 2 country codes format can be :png or :gif



14
15
16
17
18
19
20
21
22
23
# File 'lib/country_flags/helper.rb', line 14

def country_flag_path( country_code, format = :png )
  unless [:png, :gif].include?(format)
    raise(ArgumentError, 'format must be :png or :gif')
  end

  return '' unless country_code && !country_code.empty?

  path = "country_flags/#{format}/#{country_code.downcase}.#{format}"
  image_path path
end