au_state_select 0.0.1 → 1.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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/au_state_select/form_builder.rb +7 -0
- data/lib/au_state_select/form_helpers.rb +13 -0
- data/lib/au_state_select/instance_tag.rb +37 -0
- data/lib/au_state_select/railtie.rb +3 -1
- data/lib/au_state_select/version.rb +1 -1
- data/lib/au_state_select.rb +11 -58
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c653a45089c19a6b94502fde4ede81eb294408a6
|
4
|
+
data.tar.gz: ab7d14c21012e8926ebda28cf27f171b72ba5cbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e47e3f40b21a5c47d474079a3398738e907a083bf6e35e0725cafe8869abe6d347900231ef38a3e251283c67515dbed3565da3035833e5d2640f08cfb75c7ecd
|
7
|
+
data.tar.gz: b0102516754530829c06772ecd287953d5f9b14140732822787c53a4916ea0f2a111c9d34d993199567abd19ebac373fa57f7e1dbc827fafd20c1930a8f6432b
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# AuStateSelect
|
2
2
|
|
3
|
-
|
3
|
+
Provides a simple helper to get an HTML select list of Austraila states. Compatable with Rails 3 & 4.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem 'au_state_select'
|
9
|
+
gem 'au_state_select', '~> 1.0'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module AUStateSelect
|
2
|
+
module FormHelpers
|
3
|
+
def state_select(object_name, method, priority_countries = nil, options = {}, html_options = {})
|
4
|
+
if Rails::VERSION::MAJOR >= 4
|
5
|
+
instance_tag = ActionView::Helpers::Tags::Select.new(object_name, method, self, [], options, html_options)
|
6
|
+
instance_tag.to_state_select_tag(priority_countries, html_options)
|
7
|
+
else
|
8
|
+
instance_tag = ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object))
|
9
|
+
instance_tag.to_state_select_tag(priority_countries, html_options, options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AUStateSelect
|
2
|
+
|
3
|
+
module InstanceTag
|
4
|
+
def to_state_select_tag(priority_states, html_options = {}, options = {})
|
5
|
+
# Rails 4 stores options sent when creating an InstanceTag.
|
6
|
+
# Let's use them!
|
7
|
+
options = @options if defined?(@options)
|
8
|
+
|
9
|
+
state_select(priority_states, options, html_options)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Adapted from Rails country_select. Just uses country codes instead of full names.
|
13
|
+
def state_select(priority_states, options, html_options)
|
14
|
+
selected = object.send(@method_name) if object.respond_to?(@method_name)
|
15
|
+
|
16
|
+
countries = ""
|
17
|
+
|
18
|
+
if options.present? and (options[:include_blank] or (options[:prompt] and !selected))
|
19
|
+
option = options[:include_blank] == true ? "" : options[:include_blank]
|
20
|
+
countries += "<option>#{option}</option>\n"
|
21
|
+
end
|
22
|
+
|
23
|
+
if priority_states
|
24
|
+
countries += options_for_select(priority_states, selected)
|
25
|
+
countries += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
|
26
|
+
end
|
27
|
+
p selected
|
28
|
+
countries = countries + options_for_select([['Australian Capital Territory', 'ACT'],['New South Wales', 'NSW'],['Northern Territory', 'NT'],['Queensland', 'QLD'],['South Australia', 'SA'],['Tasmania', 'TAS'],['Victoria', 'VIC'],['Western Australia', 'WA']], selected)
|
29
|
+
|
30
|
+
html_options = html_options.stringify_keys
|
31
|
+
add_default_name_and_id(html_options)
|
32
|
+
|
33
|
+
content_tag(:select, countries.html_safe, html_options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -3,15 +3,17 @@ require 'rails/railtie'
|
|
3
3
|
module AUStateSelect
|
4
4
|
class Railtie < Rails::Railtie
|
5
5
|
initializer "au_state_select.configure_rails_initialization" do
|
6
|
-
SimpleForm.input_mappings = { /state/ => :state }
|
7
6
|
|
7
|
+
SimpleForm.input_mappings = { /state/ => :state }
|
8
8
|
module ::SimpleForm
|
9
9
|
mattr_accessor :state_priority
|
10
10
|
@@state_priority = nil
|
11
|
+
|
11
12
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
12
13
|
map_type :state, to: SimpleForm::Inputs::PriorityInput
|
13
14
|
end
|
14
15
|
end
|
16
|
+
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/au_state_select.rb
CHANGED
@@ -1,62 +1,15 @@
|
|
1
1
|
require "au_state_select/version"
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# have it marked as the selected option tag. You can also supply an array of countries as +priority_states+, so
|
12
|
-
# that they will be listed above the rest of the (long) list.
|
13
|
-
#
|
14
|
-
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
|
15
|
-
def state_options_for_select(selected = nil, priority_states = nil)
|
16
|
-
state_options = ""
|
17
|
-
|
18
|
-
if priority_states
|
19
|
-
if (unlisted = priority_states - AU_STATES).any?
|
20
|
-
raise RuntimeError.new("Supplied priority countries are not in the main list: #{unlisted}")
|
21
|
-
end
|
22
|
-
state_options += options_for_select(priority_states, selected)
|
23
|
-
state_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
|
24
|
-
|
25
|
-
# prevents selected from being included twice in the HTML which causes
|
26
|
-
# some browsers to select the second selected option (not priority)
|
27
|
-
# which makes it harder to select an alternative priority state
|
28
|
-
selected = nil if priority_states.include?(selected)
|
29
|
-
end
|
30
|
-
|
31
|
-
state_options = state_options.html_safe if state_options.respond_to?(:html_safe)
|
32
|
-
|
33
|
-
return state_options + options_for_select(AU_STATES, selected)
|
34
|
-
end
|
35
|
-
|
36
|
-
# All the countries included in the state_options output.
|
37
|
-
AU_STATES = [['Australian Capital Territory', 'ACT'],['New South Wales', 'NSW'],['Northern Territory', 'NT'],['Queensland', 'QLD'],['South Australia', 'SA'],['Tasmania', 'TAS'],['Victoria', 'VIC'],['Western Australia', 'WA']] unless const_defined?("AU_STATES")
|
38
|
-
end
|
39
|
-
|
40
|
-
class InstanceTag
|
41
|
-
def to_state_select_tag(priority_states, options, html_options)
|
42
|
-
html_options = html_options.stringify_keys
|
43
|
-
add_default_name_and_id(html_options)
|
44
|
-
value = value(object)
|
45
|
-
content_tag("select",
|
46
|
-
add_options(
|
47
|
-
state_options_for_select(value, priority_states),
|
48
|
-
options, value
|
49
|
-
), html_options
|
50
|
-
)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class FormBuilder
|
55
|
-
def state_select(method, priority_states = nil, options = {}, html_options = {})
|
56
|
-
@template.state_select(@object_name, method, priority_states, options.merge(:object => @object), html_options)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
2
|
+
require "au_state_select/form_helpers"
|
3
|
+
require "au_state_select/instance_tag"
|
4
|
+
require "au_state_select/form_builder"
|
5
|
+
|
6
|
+
ActionView::Base.send(:include, AUStateSelect::FormHelpers)
|
7
|
+
if Rails::VERSION::MAJOR >= 4
|
8
|
+
ActionView::Helpers::ActiveModelInstanceTag.send(:include, AUStateSelect::InstanceTag)
|
9
|
+
else
|
10
|
+
ActionView::Helpers::InstanceTag.send(:include, AUStateSelect::InstanceTag)
|
60
11
|
end
|
12
|
+
ActionView::Helpers::FormBuilder.send(:include, AUStateSelect::FormBuilder)
|
13
|
+
|
61
14
|
|
62
15
|
require 'au_state_select/railtie' if defined?(Rails) and defined?(SimpleForm)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: au_state_select
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Zhang
|
@@ -52,6 +52,9 @@ files:
|
|
52
52
|
- Rakefile
|
53
53
|
- au_state_select.gemspec
|
54
54
|
- lib/au_state_select.rb
|
55
|
+
- lib/au_state_select/form_builder.rb
|
56
|
+
- lib/au_state_select/form_helpers.rb
|
57
|
+
- lib/au_state_select/instance_tag.rb
|
55
58
|
- lib/au_state_select/railtie.rb
|
56
59
|
- lib/au_state_select/version.rb
|
57
60
|
homepage: ''
|