Sha256: 4e2c60f94434730188838f20512d55d342fb03664ee0e60e3cc6a52187d00780

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

# CountrySelect - stolen from http://github.com/rails/iso-3166-country-select
module ActionView
  module Helpers
    module FormOptionsHelper

      def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
        InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
      end

      def country_options_for_select(selected = nil, priority_countries = nil)
        country_options = ""

        if priority_countries
          priority_countries = [*priority_countries].map {|x| [x,Country::NameIndex[x]] }
          country_options += options_for_select(priority_countries, selected)
          country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
        end

        return country_options + options_for_select(Country::Names, selected)
      end
    end
    
    class InstanceTag
      def to_country_select_tag(priority_countries, options, html_options)
        html_options = html_options.stringify_keys
        add_default_name_and_id(html_options)
        value = value(object)
        content_tag("select",
          add_options(
            country_options_for_select(value, priority_countries),
            options, value
          ), html_options
        )
      end
    end
    
    class FormBuilder
      def country_select(method, priority_countries = nil, options = {}, html_options = {})
        @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
countries-0.6.1 lib/countries/select_helper.rb
countries-0.6.0 lib/countries/select_helper.rb
countries-0.5.3 lib/countries/select_helper.rb
countries-0.5.2 lib/countries/select_helper.rb
countries-0.5.1 lib/countries/select_helper.rb
countries-0.5.0 lib/countries/select_helper.rb
countries-0.3.0 lib/countries/select_helper.rb
countries-0.2.2 lib/countries/select_helper.rb
countries-0.2.1 lib/countries/select_helper.rb
countries-0.2.0 lib/countries/select_helper.rb
countries-0.1.8 lib/countries/select_helper.rb