Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Dec 21, 2010
0 parents commit 990532f
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# States select

Based on [us_states plugin](http://svn.techno-weenie.net/projects/plugins/us_states/) by techno-weenie

This project has been extended to be available as either a plugin or a gem and is now targeting Rails 3.
The states selection has been made more flexible in order to support multiple countries and regions in the world (USA, Canada, ...).
The select tag now takes a new :region option which currently supports either :usa, :canada and :australia with :usa as the default if the option is not set.

Please help provide states for other regions/countries, fx EU, Africa, South America, ...

## Install Rails 3

In Gemfile
<pre>
gem 'states_select', '>= 1.0.1'
</pre>

In console/terminal:
<pre>
$ bundle install
</pre>

## Install as system gem

<pre>
$ gem install states_select
</pre>

## Install as plugin

$ rails install plugin http://github.com/kristianmandrup/states_select.git

Or something like that... (i.e install as gem is recommended)

## Usage

To select "priority" states that show up at the top of the list, call like so:
<pre>
<%= state_select 'child', 'state', :priority => %w(TX CA) %>
<%= state_select 'child', 'state', :priority => %w(ON), :country => :canada %>
</pre>

To select the way states display option and value:

this (default):
<pre>
<%= state_select 'child', 'state'%>
</pre>

will yield this:
<pre>
<option value="AK">Alaska</option>
</pre>

this:
<pre>
<%= state_select 'child', 'state', :show => :full %>
</pre>

will yield this:
<pre>
<option value="Alaska">Alaska</option>
</pre>

Options are:

:full = <option value="Alaska">Alaska</option>
:full_abb = <option value="AK">Alaska</option>
:abbreviations = <option value="AK">AK</option>
:abb_full_abb = <option value="AK">AK - Alaska</option>
19 changes: 19 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "states_select"
gem.summary = %Q{State and Provinces select Rails plugin}
gem.description = "State select Rails 3 plugin"
gem.homepage = "http://github.com/kristianmandrup/states_select"
gem.email = "kmandrup@gmail.com"
gem.authors = ["Kristian Mandrup"]

gem.add_dependency "rails", ">= 2.2.0"

# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler not available. Install it with: gem install jeweler"
end

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.1
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'states_select'
Binary file added lib/.DS_Store
Binary file not shown.
99 changes: 99 additions & 0 deletions lib/states_select.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module ActionView
module Helpers
module FormOptionsHelper
def state_options_for_select(selected = nil, country_state_options = {})
state_options = ""
priority_states = lambda { |state| country_state_options[:priority].include?(state.last) }
country_state_options[:show] = :full if country_state_options[:with_abbreviation]

country = country_state_options[:region] || :usa

country_states = case country
when :usa
usa_states
when :canada
canada_provinces
when :australia
australia_provinces
else
raise ArgumentError, "Country #{country} is not currently supported for this state plugin"
end

states_label = case country_state_options[:show]
when :full_abb then lambda { |state| [state.first, state.last] }
when :full then lambda { |state| [state.first, state.first] }
when :abbreviations then lambda { |state| [state.last, state.last] }
when :abb_full_abb then lambda { |state| ["#{state.last} - #{state.first}", state.last] }
else lambda { |state| state }
end

if country_state_options[:include_blank]
if country_state_options[:include_blank].class == TrueClass
state_options += "<option value=\"\">--</option>\n"
else
state_options += "<option value=\"\">#{country_state_options[:include_blank].to_s}</option>\n"
end
end

if country_state_options[:priority]
state_options += options_for_select(country_states.select(&priority_states).collect(&states_label), selected)
state_options += "<option value=\"\">--</option>\n"
end

if country_state_options[:priority] && country_state_options[:priority].include?(selected)
state_options += options_for_select(country_states.reject(&priority_states).collect(&states_label), selected)
else
state_options += options_for_select(country_states.collect(&states_label), selected)
end

return state_options
end

def state_select(object, method, country_state_options = {}, options = {}, html_options = {})
InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(country_state_options, options, html_options)
end

private

def usa_states
require 'states_select/usa'
USA::States.names
end

def canada_provinces
require 'states_select/canada'
Canada::Provinces.names
end

def australia_provinces
require 'states_select/australia'
Australia::Provinces.names
end
end

class InstanceTag #:nodoc:
# lets the us_states plugin handle Rails 1.1.2 AND trunk
def value_with_compat(object=nil)
if method(:value_without_compat).arity == 1
value_without_compat(object)
else
value_without_compat
end
end
alias_method :value_without_compat, :value
alias_method :value, :value_with_compat

def to_state_select_tag(country_state_options, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
content_tag("select", add_options(state_options_for_select(value(object), country_state_options), options, value(object)), html_options)
end
end

class FormBuilder
def state_select(method, country_state_options = {}, options = {}, html_options = {})
@template.state_select(@object_name, method, country_state_options, options.merge(:object => @object), html_options)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/states_select/australia.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Australia
class Provinces
def self.names
[
["Western Australia", "WA"], ["Northern Territory", "NO"],
["Queensland", "QE"], ["South Australia", "SA"],
["New South Wales", "NSW"], ["Victoria", "VI"],
["Tasmania", "TA"]
]
end
end
end
13 changes: 13 additions & 0 deletions lib/states_select/canada.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Canada
class Provinces
def self.names
[
["Yukon", "YO"], ["Territoires du Norde Ouest", "TNO"],
["Nunavut", "NU"], ["Columbie-Britanique", "CB"],
["Alberta", "AL"], ["Saskatchewan", "SA"],
["Manitoba", "MA"], ["Ontario", "ON"],
["Quebec", "QE"], ["Terre neuve et Labrador", "TN"]
]
end
end
end
19 changes: 19 additions & 0 deletions lib/states_select/usa.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class USA
class States
def self.names
[
["Alaska", "AK"], ["Alabama", "AL"], ["Arkansas", "AR"], ["Arizona", "AZ"],
["California", "CA"], ["Colorado", "CO"], ["Connecticut", "CT"], ["District of Columbia", "DC"],
["Delaware", "DE"], ["Florida", "FL"], ["Georgia", "GA"], ["Hawaii", "HI"], ["Iowa", "IA"],
["Idaho", "ID"], ["Illinois", "IL"], ["Indiana", "IN"], ["Kansas", "KS"], ["Kentucky", "KY"],
["Louisiana", "LA"], ["Massachusetts", "MA"], ["Maryland", "MD"], ["Maine", "ME"], ["Michigan", "MI"],
["Minnesota", "MN"], ["Missouri", "MO"], ["Mississippi", "MS"], ["Montana", "MT"], ["North Carolina", "NC"],
["North Dakota", "ND"], ["Nebraska", "NE"], ["New Hampshire", "NH"], ["New Jersey", "NJ"],
["New Mexico", "NM"], ["Nevada", "NV"], ["New York", "NY"], ["Ohio", "OH"], ["Oklahoma", "OK"],
["Oregon", "OR"], ["Pennsylvania", "PA"], ["Rhode Island", "RI"], ["South Carolina", "SC"], ["South Dakota", "SD"],
["Tennessee", "TN"], ["Texas", "TX"], ["Utah", "UT"], ["Virginia", "VA"], ["Vermont", "VT"],
["Washington", "WA"], ["Wisconsin", "WI"], ["West Virginia", "WV"], ["Wyoming", "WY"]
]
end
end
end

0 comments on commit 990532f

Please sign in to comment.