better_states_select 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/README.md +52 -0
  2. data/Rakefile +41 -0
  3. data/VERSION +1 -0
  4. data/lib/better_states_select.rb +68 -0
  5. metadata +84 -0
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ better-states-select
2
+ ====================
3
+
4
+ Originally from http://svn.techno-weenie.net/projects/plugins/us_states/
5
+ then was at https://github.com/thincloud/us-state-select-plugin.
6
+
7
+ No one seems to be maintaining this (ignoring my pull requests) so I've
8
+ improved the library (added support for Canada) and now named it
9
+ better-states-select.
10
+
11
+ To select "priority" states that show up at the top of the list, call
12
+ like so:
13
+
14
+ <%= state_select 'child', 'state', :priority => %w(TX CA) %>
15
+
16
+ ## Changing Option/Value with :show
17
+
18
+ The default...
19
+
20
+ <%= state_select 'child', 'state'%>
21
+
22
+ ...will yield this:
23
+
24
+ <option value="AK">Alaska</option>
25
+
26
+ - - -
27
+
28
+ Or you can change it up...
29
+
30
+ <%= state_select 'child', 'state', :show => :full %>
31
+
32
+ ...and get this.
33
+
34
+ <option value="Alaska">Alaska</option>
35
+
36
+ - - -
37
+
38
+ Options are:
39
+
40
+ * :full = <option value="Alaska">Alaska</option>
41
+ * :full_abb = <option value="AK">Alaska</option>
42
+ * :abbreviations = <option value="AK">AK</option>
43
+ * :abb_full_abb = <option value="AK">AK - Alaska</option>
44
+ * :country - defaults to US states, but if you pass "Canada" you'll get Canadian provinces
45
+
46
+ You can also pass a proc to show:
47
+
48
+ <%= state_select 'child', 'state', :show => Proc.new {|state| [state.first, state.first]} %>
49
+
50
+ The array you are iterating over looks like this:
51
+
52
+ [["Alaska", "AK"], ["Alabama","AL"], ...]
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "better_states_select"
9
+ s.summary = %Q{Better state selection Rails plugin}
10
+ s.homepage = "http://github.com/yyyc514/better-states-select"
11
+ s.description = "Better state selection Ruby on Rails plugin"
12
+ s.authors = ["Josh Goebel","Rick Olson","Larry Sprock"]
13
+
14
+ s.add_runtime_dependency "rails", '>= 1.2'
15
+
16
+ s.files.exclude 'init.rb'
17
+ s.files.exclude 'better_states_select.gemspec'
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib' << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = false
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |t|
33
+ t.libs << 'test'
34
+ t.test_files = FileList['test/**/*_test.rb']
35
+ t.verbose = true
36
+ end
37
+ rescue LoadError
38
+ puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+
41
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,68 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormOptionsHelper
4
+ def state_options_for_select(selected = nil, options = {})
5
+ states = US_STATES
6
+ states = CANADIAN_PROVINCES if options[:country]=="Canada"
7
+ state_options = ""
8
+ priority_states = lambda { |state| options[:priority].include?(state.last) }
9
+ options[:show] = :full if options[:with_abbreviation]
10
+ states_label = case options[:show]
11
+ when :full_abb then lambda { |state| [state.first, state.last] }
12
+ when :full then lambda { |state| [state.first, state.first] }
13
+ when :abbreviations then lambda { |state| [state.last, state.last] }
14
+ when :abb_full_abb then lambda { |state| ["#{state.last} - #{state.first}", state.last] }
15
+ else lambda { |state| state }
16
+ end
17
+ states_label = options[:show] if options[:show].is_a?(Proc)
18
+
19
+ if options[:priority]
20
+ state_options += options_for_select(states.select(&priority_states).collect(&states_label), selected)
21
+ state_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
22
+
23
+ selected = nil if options[:priority].include?(selected)
24
+ end
25
+
26
+ state_options += options_for_select(states.collect(&states_label), selected)
27
+ return state_options.html_safe if state_options.respond_to?(:html_safe)
28
+ state_options
29
+ end
30
+
31
+ def state_select(object, method, state_options = {}, options = {}, html_options = {})
32
+ options.merge!(state_options)
33
+ InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(options, html_options)
34
+ end
35
+
36
+ private
37
+ CANADIAN_PROVINCES = [["Alberta", "AB"],["British Columbia","BC"],["Manitoba","MB"],["New Brunswick","NB"],
38
+ ["Newfoundland and Labrador","NL"],["Nova Scotia","NS"],["Ontario","ON"],
39
+ ["Prince Edward Island","PE"],["Quebec","QC"],["Saskatchewan","SK"]]
40
+ US_STATES = [["Alaska", "AK"], ["Alabama", "AL"], ["Arkansas", "AR"], ["Arizona", "AZ"],
41
+ ["California", "CA"], ["Colorado", "CO"], ["Connecticut", "CT"], ["District of Columbia", "DC"],
42
+ ["Delaware", "DE"], ["Florida", "FL"], ["Georgia", "GA"], ["Hawaii", "HI"], ["Iowa", "IA"],
43
+ ["Idaho", "ID"], ["Illinois", "IL"], ["Indiana", "IN"], ["Kansas", "KS"], ["Kentucky", "KY"],
44
+ ["Louisiana", "LA"], ["Massachusetts", "MA"], ["Maryland", "MD"], ["Maine", "ME"], ["Michigan", "MI"],
45
+ ["Minnesota", "MN"], ["Missouri", "MO"], ["Mississippi", "MS"], ["Montana", "MT"], ["North Carolina", "NC"],
46
+ ["North Dakota", "ND"], ["Nebraska", "NE"], ["New Hampshire", "NH"], ["New Jersey", "NJ"],
47
+ ["New Mexico", "NM"], ["Nevada", "NV"], ["New York", "NY"], ["Ohio", "OH"], ["Oklahoma", "OK"],
48
+ ["Oregon", "OR"], ["Pennsylvania", "PA"], ["Rhode Island", "RI"], ["South Carolina", "SC"], ["South Dakota", "SD"],
49
+ ["Tennessee", "TN"], ["Texas", "TX"], ["Utah", "UT"], ["Virginia", "VA"], ["Vermont", "VT"],
50
+ ["Washington", "WA"], ["Wisconsin", "WI"], ["West Virginia", "WV"], ["Wyoming", "WY"]] unless const_defined?("US_STATES")
51
+
52
+ end
53
+
54
+ class InstanceTag #:nodoc:
55
+ def to_state_select_tag(options, html_options)
56
+ html_options = html_options.stringify_keys
57
+ add_default_name_and_id(html_options)
58
+ content_tag("select", add_options(state_options_for_select(value(object), options), options, value(object)), html_options)
59
+ end
60
+ end
61
+
62
+ class FormBuilder
63
+ def state_select(method, state_options = {}, options = {}, html_options = {})
64
+ @template.state_select(@object_name, method, state_options, options.merge(:object => @object), html_options)
65
+ end
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_states_select
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Josh Goebel
14
+ - Rick Olson
15
+ - Larry Sprock
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2012-07-23 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rails
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 11
31
+ segments:
32
+ - 1
33
+ - 2
34
+ version: "1.2"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Better state selection Ruby on Rails plugin
38
+ email:
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.md
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - VERSION
49
+ - lib/better_states_select.rb
50
+ homepage: http://github.com/yyyc514/better-states-select
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.15
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Better state selection Rails plugin
83
+ test_files: []
84
+