Module: WorldFlags::ViewHelper

Defined in:
lib/world_flags/view_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flag_sizesObject



3
4
5
# File 'lib/world_flags/view_helper.rb', line 3

def self.flag_sizes
	[16, 32, 64]
end

Instance Method Details

#flag(code, name, options = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/world_flags/view_helper.rb', line 44

def flag code, name, options = {}
	label = options[:content] ? name : ' '
	extra_options = options[:title] ? {:title => name } : {}			
	selected = flag_selected?(code, options) ? 'selected' : ''
	 :li,  label.html_safe, {:class => "flag #{code} #{selected}", :'data-country' => name, :'data-cc' => code}.merge(options[:html] || {}).merge(extra_options)
end

#flag_code(code) ⇒ Object

define more mappings by setting WorldFlags.locale_flag_map to a Hash map en.wikipedia.org/wiki/ISO_639-1_language_matrix



9
10
11
# File 'lib/world_flags/view_helper.rb', line 9

def flag_code code
	WorldFlags.flag_code code
end

#flag_selected?(code, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/world_flags/view_helper.rb', line 51

def flag_selected? code, options = {}
	selected = options[:selected] || options[code.to_sym]
	selected ||= (flag_code(I18n.locale.to_sym) == code.to_sym) if WorldFlags.auto_select?
end

#flag_title(code, name, options = {}) ⇒ Object



56
57
58
# File 'lib/world_flags/view_helper.rb', line 56

def flag_title code, name, options = {}
	flag code, name, options.merge(:title => true)
end

#flags(flags_arr, options = {}) ⇒ Object

should look up translation for each code



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/world_flags/view_helper.rb', line 24

def flags flags_arr, options = {}
	flags_arr.inject("") do |res, elem|
		case elem
		when Array
			code = elem.first
			name = elem.last
		when String, Symbol
			code = elem
			name = WorldFlags.label(code, options)
		else
			raise ArgumentError, "Bad argument: #{flags_arr}, must be Hash or Array"
		end				
		res << flag(code, name, options)
	end.html_safe
end

#flags_list(size = 16, &block) ⇒ Object Also known as: flag_list



13
14
15
16
17
18
# File 'lib/world_flags/view_helper.rb', line 13

def flags_list size = 16, &block
	raise "Missing block" unless block_given?
	raise "Supported sizes are only #{WorldFlags::ViewHelper.flag_sizes}" unless WorldFlags::ViewHelper.flag_sizes.include?(size.to_i)
	content = capture(&block)
	 :ul, content, :class => "f#{size}"
end

#flags_title(flags_arr, options = {}) ⇒ Object



40
41
42
# File 'lib/world_flags/view_helper.rb', line 40

def flags_title flags_arr, options = {}
	flags flags_arr, options.merge(:title => true)
end

#use_flags(size = 16) ⇒ Object



60
61
62
# File 'lib/world_flags/view_helper.rb', line 60

def use_flags size = 16
	stylesheet_link_tag "flags/flags#{size}"
end