state_select 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [Akhil Bansal]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ MIT-LICENSE
2
+ README
3
+ Rakefile
4
+ init.rb
5
+ install.rb
6
+ lib/state_select.rb
7
+ test/state_select_test.rb
8
+ Manifest
data/README ADDED
@@ -0,0 +1,29 @@
1
+ StateSelect
2
+ =============
3
+ state_select plugin allows you to easyly create dropdown list of states for a country provided as an option.
4
+ Default counrty is US.
5
+
6
+ Usage:
7
+ state_select(object, method, country='US', options = {}, html_options = {})
8
+
9
+ Currently it supports only INDIA, US, UK, CANADA, AUSTRALIA, SPAIN, FRANCE, UGANDA, GERMAN and NETHERLANDS
10
+
11
+ =============================================
12
+ Tested with Rails 2.3.* and Rails 3.0.0.beta
13
+ =============================================
14
+
15
+ *Thanks keith(http://rubygreenblue.com/) for Australia states
16
+ *Thanks Vicent (http://www.vicentgozalbes.com/) for Spain states
17
+ *Thanks Chuck Phillips for Uganda states
18
+ *Thanks Jonathan Tron for France states
19
+ *Thanks Gavin Morrice(http://handyrailstips.com/) for UK States
20
+
21
+ If you want to add other country plese send me the list of states and I will add them, or you can fork and make a pull request.
22
+
23
+
24
+ Author:
25
+ Akhil Bansal
26
+ bansalakhil30.10@gmail.com
27
+ http://webonrails.com
28
+ New Delhi, India
29
+
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('state_select', '0.1.0') do |p|
6
+ p.description = "State select"
7
+ p.url = "http://github.com/buger/state_select"
8
+ p.author = "Leonid Bugaev"
9
+ p.email = "leonsbox@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require "state_select"
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,53 @@
1
+ module ActionView::Helpers::FormOptionsHelper
2
+
3
+ # Return select and option tags for the given object and method, using state_options_for_select to generate the list of option tags.
4
+ def state_select(object, method, country='US', options = {}, html_options = {})
5
+ ActionView::Helpers::InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(country, options, html_options)
6
+ end
7
+
8
+ # Returns a string of option tags for states in a country. Supply a state name as +selected+ to
9
+ # have it marked as the selected option tag.
10
+ #
11
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
12
+
13
+ def state_options_for_select(selected = nil, country = 'US')
14
+ state_options = ""
15
+ if country
16
+ state_options += options_for_select(eval(country.upcase+'_STATES'), selected)
17
+ end
18
+ return state_options
19
+ end
20
+
21
+ private
22
+
23
+ US_STATES=["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "Washington D.C.", "West Virginia", "Wisconsin", "Wyoming"] unless const_defined?("US_STATES")
24
+ UK_STATES=["Avon", "Bedfordshire", "Berkshire", "Borders", "Buckinghamshire", "Cambridgeshire", "Central", "Cheshire", "Cleveland", "Clwyd", "Cornwall", "County Antrim", "County Armagh", "County Down", "County Fermanagh", "County Londonderry", "County Tyrone", "Cumbria", "Derbyshire", "Devon", "Dorset", "Dumfries and Galloway", "Durham", "Dyfed", "East Sussex", "Essex", "Fife", "Gloucestershire", "Grampian", "Greater Manchester", "Gwent", "Gwynedd County", "Hampshire", "Herefordshire", "Hertfordshire", "Highlands and Islands", "Humberside", "Isle of Wight", "Kent", "Lancashire", "Leicestershire", "Lincolnshire", "Lothian", "Merseyside", "Mid Glamorgan", "Norfolk", "North Yorkshire", "Northamptonshire", "Northumberland", "Nottinghamshire", "Oxfordshire", "Powys", "Rutland", "Shropshire", "Somerset", "South Glamorgan", "South Yorkshire", "Staffordshire", "Strathclyde", "Suffolk", "Surrey", "Tayside", "Tyne and Wear", "Warwickshire", "West Glamorgan", "West Midlands", "West Sussex", "West Yorkshire", "Wiltshire", "Worcestershire"] unless const_defined?("UK_STATES")
25
+ INDIA_STATES=["Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "New Delhi", "Goa", "Gujarat", "Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Tripura", "Uttaranchal", "Uttar Pradesh", "West Bengal"] unless const_defined?("INDIA_STATES")
26
+ CANADA_STATES=["Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland", "Northwest Territories", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon"] unless const_defined?("CANADA_STATES")
27
+ AUSTRALIA_STATES=["Australian Capital Territory", "New South Wales", "Northern Territory", "Queensland", "South Australia", "Tasmania", "Victoria", "Western Australia"] unless const_defined?("AUSTRALIA_STATES")
28
+ SPAIN_STATES=["Alava", "Albacete", "Alicante", "Almeria", "Asturias", "Avila", "Badajoz", "Barcelona", "Burgos", "Caceres", "Cadiz", "Cantrabria", "Castellon", "Ceuta", "Ciudad Real", "Cordoba", "Cuenca", "Girona", "Granada", "Guadalajara", "Guipuzcoa", "Huelva", "Huesca", "Islas Baleares", "Jaen", "La Coruna","Leon", "Lleida", "Lugo", "Madrid", "Malaga", "Melilla", "Murcia", "Navarra", "Ourense", "Palencia", "Palmas, Las", "Pontevedra", "Rioja, La", "Salamanda", "Santa Cruz de Tenerife", "Segovia", "Sevila", "Soria", "Tarragona", "Teruel", "Toledo", "Valencia", "Valladolid", "Vizcaya", "Zamora", "Zaragoza"] unless const_defined?("SPAIN_STATES")
29
+ UGANDA_STATES=["Abim", "Adjumani", "Amolatar", "Amuria", "Apac", "Arua", "Budaka", "Bugiri", "Bukwa", "Bulisa", "Bundibugyo", "Bushenyi", "Busia", "Busiki", "Butaleja", "Dokolo", "Gulu", "Hoima", "Ibanda", "Iganga", "Jinja", "Kaabong", "Kabale", "Kabarole", "Kaberamaido", "Kabingo", "Kalangala", "Kaliro", "Kampala", "Kamuli", "Kamwenge", "Kanungu", "Kapchorwa", "Kasese", "Katakwi", "Kayunga", "Kibale", "Kiboga", "Kilak", "Kiruhura", "Kisoro", "Kitgum", "Koboko", "Kotido", "Kumi", "Kyenjojo", "Lira", "Luwero", "Manafwa", "Maracha", "Masaka", "Masindi", "Mayuge", "Mbale", "Mbarara", "Mityana", "Moroto", "Moyo", "Mpigi", "Mubende", "Mukono", "Nakapiripirit", "Nakaseke", "Nakasongola", "Nebbi", "Ntungamo", "Oyam", "Pader", "Pallisa", "Rakai", "Rukungiri", "Sembabule", "Sironko", "Soroti", "Tororo", "Wakiso", "Yumbe"] unless const_defined?("UGANDA_STATES")
30
+ FRANCE_STATES=["Alsace","Aquitaine","Auvergne","Bourgogne","Bretagne","Centre","Champagne-Ardenne","Corse","Franche-Comte","Ile-de-France","Languedoc-Roussillon","Limousin","Lorraine","Midi-Pyrenees","Nord-Pas-de-Calais","Basse-Normandie","Haute-Normandie","Pays de la Loire","Picardie","Poitou-Charentes","Provence-Alpes-Cote d'Azur","Rhone-Alpes"] unless const_defined?("FRENCE_STATES")
31
+ GERMAN_STATES=["Baden-Wurttemberg", "Bayern", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg- Vorpommern", "Niedersachsen", "Nordrhein-Westfalen", "Rhineland- Pflaz", "Saarland", "Sachsen", "Sachsen-Anhalt", "Schleswig- Holstein", "Thuringen"] unless const_defined?("GERMAN_STATES")
32
+ NETHERLANDS_STATES=["Drenthe", "Flevoland", "Friesland", "Gelderland", "Groningen", "Limburg", "Noord-Brabant", "Noord-Holland", "Overijssel", "Utrecht", "Zeeland", "Zuid-Holland"] unless const_defined?("NETHERLANDS_STATES")
33
+ end
34
+
35
+ class ActionView::Helpers::InstanceTag
36
+
37
+
38
+ def to_state_select_tag(country, options, html_options)
39
+ html_options = html_options.stringify_keys
40
+ add_default_name_and_id(html_options)
41
+ value = value(object)
42
+ selected_value = options.has_key?(:selected) ? options[:selected] : value
43
+ content_tag("select", add_options(state_options_for_select(selected_value, country), options, value), html_options)
44
+ end
45
+ end
46
+
47
+
48
+ class ActionView::Helpers::FormBuilder
49
+ def state_select(method, country = 'US', options = {}, html_options = {})
50
+ @template.state_select(@object_name, method, country, options.merge(:object => @object), html_options)
51
+ end
52
+ end
53
+
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{state_select}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Leonid Bugaev"]
9
+ s.date = %q{2011-03-18}
10
+ s.description = %q{State select}
11
+ s.email = %q{leonsbox@gmail.com}
12
+ s.extra_rdoc_files = ["README", "lib/state_select.rb"]
13
+ s.files = ["MIT-LICENSE", "README", "Rakefile", "init.rb", "install.rb", "lib/state_select.rb", "test/state_select_test.rb", "Manifest", "state_select.gemspec"]
14
+ s.homepage = %q{http://github.com/buger/state_select}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "State_select", "--main", "README"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{state_select}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{State select}
20
+ s.test_files = ["test/state_select_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ #require 'test/unit'
2
+ #
3
+ #class USStateSelectTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ # def test_this_plugin
6
+ # flunk
7
+ # end
8
+ #end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_select
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Leonid Bugaev
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-18 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: State select
23
+ email: leonsbox@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ - lib/state_select.rb
31
+ files:
32
+ - MIT-LICENSE
33
+ - README
34
+ - Rakefile
35
+ - init.rb
36
+ - install.rb
37
+ - lib/state_select.rb
38
+ - test/state_select_test.rb
39
+ - Manifest
40
+ - state_select.gemspec
41
+ has_rdoc: true
42
+ homepage: http://github.com/buger/state_select
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --line-numbers
48
+ - --inline-source
49
+ - --title
50
+ - State_select
51
+ - --main
52
+ - README
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 11
70
+ segments:
71
+ - 1
72
+ - 2
73
+ version: "1.2"
74
+ requirements: []
75
+
76
+ rubyforge_project: state_select
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: State select
81
+ test_files:
82
+ - test/state_select_test.rb