module CountrySelect::TagHelper
Public Instance Methods
Private Instance Methods
Source
# File lib/country_select/tag_helper.rb, line 58 def country_options codes = ISO3166::Country.codes if only_country_codes.present? codes = only_country_codes & codes sort = @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided]) else codes -= except_country_codes if except_country_codes.present? sort = true end country_options_for(codes, sorted: sort) end
Source
# File lib/country_select/tag_helper.rb, line 72 def country_options_for(country_codes, sorted: true) I18n.with_locale(locale) do country_list = country_codes.map { |code_or_name| get_formatted_country(code_or_name) } country_list.sort_by! { |name, _| transliterated_name = I18n.transliterate(name.to_s) if transliterated_name.include?('?') # For languages that cannot be transliterated (e.g. languages with non-Latin scripts) [name, name] # If transliteration fails, duplicate the original name to maintain a consistent two-element array structure. else [transliterated_name, name] end } if sorted country_list end end
Source
# File lib/country_select/tag_helper.rb, line 50 def except_country_codes @options.fetch(:except, ::CountrySelect::DEFAULTS[:except]) end
Source
# File lib/country_select/tag_helper.rb, line 54 def format @options.fetch(:format, ::CountrySelect::DEFAULTS[:format]) end
Source
# File lib/country_select/tag_helper.rb, line 107 def get_formatted_country(code_or_name) country = ISO3166::Country.new(code_or_name) || ISO3166::Country.find_country_by_any_name(code_or_name) raise(CountryNotFoundError, "Could not find Country with string '#{code_or_name}'") unless country.present? code = country.alpha2 formatted_country = ::CountrySelect::FORMATS[format].call(country) if formatted_country.is_a?(Array) formatted_country else [formatted_country, code] end end
Source
# File lib/country_select/tag_helper.rb, line 38 def locale @options.fetch(:locale, ::CountrySelect::DEFAULTS[:locale]) end
Source
# File lib/country_select/tag_helper.rb, line 46 def only_country_codes @options.fetch(:only, ::CountrySelect::DEFAULTS[:only]) end
Source
# File lib/country_select/tag_helper.rb, line 88 def options_for_select_with_priority_countries(country_options, tags_options) sorted = @options.fetch(:sort_provided, ::CountrySelect::DEFAULTS[:sort_provided]) priority_countries_options = country_options_for(priority_countries, sorted:) option_tags = priority_options_for_select(priority_countries_options, tags_options) tags_options[:selected] = Array(tags_options[:selected]).delete_if do |selected| priority_countries_options.map(&:second).include?(selected) end option_tags += "\n".html_safe + options_for_select(country_options, tags_options) option_tags end
Source
# File lib/country_select/tag_helper.rb, line 42 def priority_countries @options.fetch(:priority_countries, ::CountrySelect::DEFAULTS[:priority_countries]) end
Source
# File lib/country_select/tag_helper.rb, line 103 def priority_options_for_select(priority_countries_options, tags_options) options_for_select(priority_countries_options, tags_options) + "\n<hr>".html_safe end