Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 uf_checkbox(name, label, value, options = {})
check = options[:check] || false
checked = options[:checked] || []
id = options[:id] || ""
check_box_tag(name, value, (checked.include?(value) || check), :id => id) << label_tag(id, label)
end
def tokenize(name)
Iconv.new('ascii//translit', 'utf-8').iconv(name).tr(' .-', '_').downcase.gsub(/[^0-9a-z-]/, '')
end
def all_included?(small, big)
((small & big).size == big.size)
end
def states_with_regions_checkboxes(object_name, method, options = {})
checked_states = eval("@#{object_name}").send(method)
checked = (checked_states.nil?) ? [] : checked_states.split(" ")
html = ""
REGIOES_BRASILEIRAS.each_key do |region|
items = ""; token = tokenize region
items << content_tag(:ul) do
REGIOES_BRASILEIRAS[region].collect {|state| content_tag(:li, uf_checkbox("#{object_name}[#{method}][]", state.first, state.second, {:check => false, :checked => checked, :id => "#{object_name}_#{method}_#{state.second}"})) }
end
region_options = options.merge(:id => (options[:id].nil?) ? 'region_' << token : options[:id] << "_#{token}")
states = REGIOES_BRASILEIRAS[region].collect(&:second)
list_items = content_tag(:li, uf_checkbox(token, region, token, {:check => all_included?(checked, states), :checked => checked, :id => "#{object_name}_#{method}_#{region}"}) << items)
html << content_tag(:ul, list_items, region_options)
end
html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.