Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvez/160143 to your computer and use it in GitHub Desktop.
Save galvez/160143 to your computer and use it in GitHub Desktop.
REGIOES_BRASILEIRAS = {
'Norte' => [
['Acre', 'AC'],
['Amapá','AP'],
['Amazonas', 'AM'],
['Pará', 'PA'],
['Rondônia', 'RO'],
['Roraima', 'RR'],
['Tocantins', 'TO']
],
'Nordeste' => [
['Alagoas', 'AL'],
['Bahia', 'BA'],
['Ceará', 'CE'],
['Maranhão', 'MA'],
['Paraíba', 'PB'],
['Pernambuco', 'PE'],
['Piauí', 'PI'],
['Rio Grande do Norte', 'RN'],
['Sergipe', 'SE']
],
'Centro-Oeste' => [
['Distrito Federal', 'DF'],
['Goiás', 'GO'],
['Mato Grosso', 'MT'],
['Mato Grosso do Sul', 'MS']
],
'Sudeste' => [
['Espírito Santos', 'ES'],
['Minas Gerais', 'MG'],
['Rio de Janeiro', 'RJ'],
['São Paulo', 'SP']
],
'Sul' => [
['Paraná', 'PR'],
['Rio Grande do Sul', 'RS'],
['Santa Catarina', 'SC']
]
}.freeze unless const_defined?('REGIOES_BRASILEIRAS')
def states_with_regions_checkboxes(object_name, method, checked = [], options = {})
checkbox = lambda {|name, label, value, check| check_box_tag(name, value, (checked.include?(value) || check), :id => "#{object_name}_#{method}_#{value}") << label_tag("#{object_name}_#{method}_#{value}", label) }
tokenize = lambda {|name| Iconv.new('ascii//translit', 'utf-8').iconv(name).tr(' .-', '_').downcase.gsub(/[^0-9a-z-]/, '') }
returning "" do |html|
REGIOES_BRASILEIRAS.each_key do |region|
items = ""; token = tokenize.call region
items << content_tag(:ul) do
REGIOES_BRASILEIRAS[region].collect {|state| content_tag(:li, checkbox.call("#{object_name}[#{method}][]", state.first, state.second, false)) }
end
region_options = options.merge(:id => (options[:id].nil?) ? 'region_' << token : options[:id] << "_#{token}")
states = REGIOES_BRASILEIRAS[region].collect(&:second)
all_checked = ((checked & states).size == states.size)
list_items = content_tag(:li, checkbox.call(token, region, token, all_checked) << items)
html << content_tag(:ul, list_items, region_options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment