Sha256: 941452934a5226e361348801ef7eb3359445dee07748a2086c4d09d3185509c6

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require "country_state_select/version"
require "city-state"
require "rails"
require "sass-rails"
require "compass-rails"
require "chosen-rails"
require "simple_form"

module CountryStateSelect

  #Collect the Countries
  def self.countries_collection
    CS.countries.except!(:COUNTRY_ISO_CODE).collect {|p| [ p[ 1], p[0] ] }.compact
  end

  #Pass array of unwanted countries to get back all except those in the array
  def self.countries_except(*except)
    countries_collection.collect { |c| c unless except.include?(c.second) }.compact
  end

  #Return either the State (String) or States (Array)
  def self.states_collection(f, options)
    states = collect_states(f.object.send(options[:country]))
    return f.object.send(options[:state]) if states.size == 0
    states
  end

  #Return the collected States for a given Country
  def self.collect_states(country)
    CS.states(country).collect {|p| [ p[1], p[0] ] }.compact
  end

  #Return the cities of given state and country
  def self.collect_cities(state_id = '', country_id = '')
    CS.cities(state_id.to_sym, country_id.to_sym)
  end

  #Return a hash for use in the simple_form
  def self.state_options(options)
    states = states_collection(options[:form], options[:field_names])
    options = options.merge(collection: states)
    options = options.merge(:as => :string) if states.class == String
    options
  end

end

case ::Rails.version.to_s
  when /^4/
    require 'country_state_select/engine'
  when /^3\.[12]/
    require 'country_state_select/engine3'
  when /^3\.[0]/
    require 'country_state_select/railtie'
  else
    raise 'Unsupported rails version'
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
country_state_select-3.0.0 lib/country_state_select.rb