carmen-ruby / carmen-rails

NOT ACTIVELY MAINTAINED Rails adapter for Carmen (provides country_select and subregion_select)
MIT License
215 stars 159 forks source link

_enabled: false on subregions only works half the time? #50

Open kinman-enphase opened 9 years ago

kinman-enphase commented 9 years ago

For several countries, I'd like to suppress all subregions. I set the _enabled attribute to false on all subregions, but only every other one is actually suppressed. For example:

bd.yml


---
- code: '1'
  _enabled: false
- code: '2'
  _enabled: false
- code: '3'
  _enabled: false
- code: '4'
  _enabled: false
- code: '5'
  _enabled: false
- code: '6'
  _enabled: false

Rails console:

> Carmen::Country.coded("BD").subregions.map &:code
["2", "4", "6"]

Is there better way to suppress all subregions?

kinman-enphase commented 9 years ago

The problem appears to be in Carmen::Region#flatten_data:

flattened.each do |hash|
  flattened.delete(hash) if hash['_enabled'] == false
end

It would be better to create a new collection rather than to mutate the one being iterated over:

Ruby console

> enabled_regions = flattened.select {|r| r['_enabled']}
[]