carmen-rails 1.0.0.beta2 → 1.0.0.beta3

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.
data/README.md CHANGED
@@ -9,7 +9,7 @@ carmen-rails is a Rails 3 plugin that supplies two new form helper methods:
9
9
  Just add carmen-rails to your Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'carmen-rails', '1.0.0.pre'
12
+ gem 'carmen-rails', '~> 1.0.0.beta3'
13
13
  ```
14
14
 
15
15
  ## Usage
@@ -18,14 +18,35 @@ gem 'carmen-rails', '1.0.0.pre'
18
18
  <%= form_for(@order) do |f| %>
19
19
  <div class="field">
20
20
  <%= f.label :country_code %><br />
21
- <%= f.country_select :country_code, priority: %w(US CA), prompt: 'Please select a country' %>
21
+ <%= f.country_select :country_code, {priority: %w(US CA)}, prompt: 'Please select a country' %>
22
22
  </div>
23
23
  <% end %>
24
24
  ```
25
25
 
26
- More docs coming soon.
26
+ ## How do I only display a subset of countries/regions?
27
+
28
+ Carmen had a concept of excluded countries in the old days, where you could
29
+ specify certain countries or regions to not include in a select.
30
+
31
+ The new (and much more flexible) way to handle this is to write a helper method
32
+ that returns the subset of regions you want to provide:
33
+
34
+ ``` ruby
35
+ def only_us_and_canada
36
+ Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)}
37
+ end
38
+ ```
39
+
40
+ And then in your form something like this:
41
+
42
+ ``` erb
43
+ <%= f.select :country, region_options_for_select(only_us_and_canada) %>
44
+ ```
45
+
46
+ More docs coming soon. In the meantime, all of the public methods in
47
+ carmen-rails [have been thoroughly TomDoc'ed](https://github.com/jim/carmen-rails/blob/master/lib/carmen/rails/action_view/form_helper.rb).
27
48
 
28
49
  ### Demo app
29
50
 
30
51
  There is a [live demo app](http://carmen-rails-demo.herokuapp.com) that shows
31
- carmen-rails in action.
52
+ carmen-rails in action, and includes a [step-by-step setup guide](https://github.com/jim/carmen-demo-app#readme).
@@ -2,28 +2,80 @@ module ActionView
2
2
  module Helpers
3
3
  module FormOptionsHelper
4
4
 
5
- # Return select and subregion option tags for the given object and method.
5
+ # Generate select and subregion option tags for the given object and method. A
6
+ # common use of this would be to allow users to select a state subregion within
7
+ # a given country.
8
+ #
9
+ # object - The model object to generate the select for
10
+ # method - The attribute on the object
11
+ # parent_region_or_code - An instance of Carmen::Region or a 2-character
12
+ # country code.
13
+ # options - Other options pertaining to option tag generation. See
14
+ # `region_options_for_select`.
15
+ # html_options - Options to use when generating the select tag- class,
16
+ # id, etc.
6
17
  #
7
18
  # Uses region_options_or_select to generate the list of option tags.
19
+ #
20
+ # Example:
21
+ #
22
+ # subregion_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
23
+ #
24
+ # Returns an `html_safe` string containing the HTML for a select element.
8
25
  def subregion_select(object, method, parent_region_or_code, options={}, html_options={})
9
26
  parent_region = determine_parent(parent_region_or_code)
10
27
  tag = InstanceTag.new(object, method, self, options.delete(:object))
11
28
  tag.to_region_select_tag(parent_region, options, html_options)
12
29
  end
13
30
 
14
- # Return select and country option tags for the given object and method.
31
+ # Generate select and country option tags for the given object and method. A
32
+ # common use of this would be to allow users to select a state subregion within
33
+ # a given country.
34
+ #
35
+ # object - The model object to generate the select for
36
+ # method - The attribute on the object
37
+ # options - Other options pertaining to option tag generation. See
38
+ # `region_options_for_select`.
39
+ # html_options - Options to use when generating the select tag- class,
40
+ # id, etc.
15
41
  #
16
42
  # Uses region_options_or_select to generate the list of option tags.
17
- def country_select(object, method, options={}, html_options = {})
18
- InstanceTag.new(object, method, self, options.delete(:object)).to_region_select_tag(Carmen::World.instance, options, html_options)
43
+ #
44
+ # Example:
45
+ #
46
+ # country_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
47
+ #
48
+ # Returns an `html_safe` string containing the HTML for a select element.
49
+ def country_select(object, method, options={}, html_options={})
50
+ tag = InstanceTag.new(object, method, self, options.delete(:object))
51
+ tag.to_region_select_tag(Carmen::World.instance, options, html_options)
19
52
  end
20
53
 
21
- def region_options_for_select(parent_region, selected = nil, priority_region_codes)
54
+ # Generate option tags for a collection of regions.
55
+ #
56
+ # regions - An array or Carmen::RegionCollection containing Carmen::Regions
57
+ # selected - the code of the region that should be selected
58
+ # options - The hash of options used to customize the output (default: {}):
59
+ #
60
+ # To use priority regions (which are included in a special section at the
61
+ # top of the list), provide an array of region codes at the :priority
62
+ # option:
63
+ #
64
+ # region_options_for_select(@region.subregions, 'US', priority: ['US', 'CA'])
65
+ #
66
+ # Returns an `html_safe` string containing option tags.
67
+ def region_options_for_select(regions, selected=nil, options={})
68
+ options.stringify_keys!
69
+ priority_region_codes = options['priority'] || []
22
70
  region_options = ""
23
71
 
24
72
  unless priority_region_codes.empty?
73
+ unless regions.respond_to?(:coded)
74
+ regions = Carmen::RegionCollection.new(regions)
75
+ end
76
+
25
77
  priority_regions = priority_region_codes.map do |code|
26
- region = parent_region.subregions.coded(code)
78
+ region = regions.coded(code)
27
79
  [region.name, region.code] if region
28
80
  end.compact
29
81
  unless priority_regions.empty?
@@ -32,28 +84,57 @@ module ActionView
32
84
  end
33
85
  end
34
86
 
35
- main_options = parent_region.subregions.map { |r| [r.name, r.code] }
87
+ main_options = regions.map { |r| [r.name, r.code] }
88
+ main_options.sort!{|a, b| a.first.unpack('U').to_s <=> b.first.unpack('U').to_s}
36
89
  region_options += options_for_select(main_options, selected)
37
90
  region_options.html_safe
38
91
  end
39
92
 
40
- # Return select tag with the name provided containing country option
41
- # tags.
93
+ # Generate select and country option tags with the provided name. A
94
+ # common use of this would be to allow users to select a country name
95
+ # inside a web form.
96
+ #
97
+ # name - The name attribute for the select element.
98
+ # options - Other options pertaining to option tag generation. See
99
+ # `region_options_for_select`.
100
+ # html_options - Options to use when generating the select tag- class,
101
+ # id, etc.
42
102
  #
43
103
  # Uses region_options_or_select to generate the list of option tags.
104
+ #
105
+ # Example:
106
+ #
107
+ # country_select_tag('country_code', {priority: ['US', 'CA']}, class: 'region')
108
+ #
109
+ # Returns an `html_safe` string containing the HTML for a select element.
44
110
  def country_select_tag(name, value, options={})
45
111
  subregion_select_tag(name, value, Carmen::World.instance, options)
46
112
  end
113
+
114
+ # Generate select and subregion option tags for the given object and method. A
115
+ # common use of this would be to allow users to select a state subregion within
116
+ # a given country.
47
117
  #
48
- # Return select tag with the name provided containing subregion option
49
- # tags.
118
+ # name - The name attribute for the select element.
119
+ # parent_region_or_code - An instance of Carmen::Region or a 2-character
120
+ # country code.
121
+ # options - Other options pertaining to option tag generation. See
122
+ # `region_options_for_select`.
123
+ # html_options - Options to use when generating the select tag- class,
124
+ # id, etc.
50
125
  #
51
126
  # Uses region_options_or_select to generate the list of option tags.
52
- def subregion_select_tag(name, value, parent_region_or_code, options = {})
127
+ #
128
+ # Example:
129
+ #
130
+ # subregion_select_tag('state_code', 'US', {priority: ['US', 'CA']}, class: 'region')
131
+ #
132
+ # Returns an `html_safe` string containing the HTML for a select element.
133
+ def subregion_select_tag(name, value, parent_region_or_code, options = {}, html_options = {})
53
134
  options.stringify_keys!
54
135
  parent_region = determine_parent(parent_region_or_code)
55
136
  priority_regions = options.delete(:priority) || []
56
- opts = region_options_for_select(parent_region, value, priority_regions)
137
+ opts = region_options_for_select(parent_region.subregions, value, :priority => priority_regions)
57
138
  html_options = {"name" => name,
58
139
  "id" => sanitize_to_id(name)}.update(options.stringify_keys)
59
140
  content_tag(:select, opts, html_options)
@@ -81,17 +162,27 @@ module ActionView
81
162
  add_default_name_and_id(html_options)
82
163
  priority_regions = options[:priority] || []
83
164
  value = value(object)
84
- opts = add_options(region_options_for_select(parent_region, value, priority_regions), options, value)
165
+ opts = add_options(region_options_for_select(parent_region.subregions, value, :priority => priority_regions), options, value)
85
166
  content_tag("select", opts, html_options)
86
167
  end
87
168
  end
88
169
 
89
170
  class FormBuilder
171
+ # Generate select and country option tags with the provided name. A
172
+ # common use of this would be to allow users to select a country name inside a
173
+ # web form.
174
+ #
175
+ # See `FormOptionsHelper::country_select` for more information.
90
176
  def country_select(method, options = {}, html_options = {})
91
177
  @template.country_select(@object_name, method,
92
178
  options.merge(:object => @object), html_options)
93
179
  end
94
180
 
181
+ # Generate select and subregion option tags with the provided name. A
182
+ # common use of this would be to allow users to select a state subregion within
183
+ # a given country.
184
+ #
185
+ # See `FormOptionsHelper::subregion_select` for more information.
95
186
  def subregion_select(method, parent_region_or_code, options={}, html_options={})
96
187
  @template.subregion_select(@object_name, method, parent_region_or_code,
97
188
  options.merge(:object => @object), html_options)
@@ -1,5 +1,5 @@
1
1
  module Carmen
2
2
  module Rails
3
- VERSION = "1.0.0.beta2"
3
+ VERSION = "1.0.0.beta3"
4
4
  end
5
5
  end
@@ -18,9 +18,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
18
18
  html = country_select(@object, :country_code)
19
19
  expected = <<-HTML
20
20
  <select id="object_country_code" name="object[country_code]">
21
- <option value="OC">Oceania</option>
22
- <option value="EU">Eurasia</option>
23
21
  <option value="ES">Eastasia</option>
22
+ <option value="EU">Eurasia</option>
23
+ <option value="OC">Oceania</option>
24
24
  </select>
25
25
  HTML
26
26
 
@@ -37,9 +37,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
37
37
  html = country_select_tag('attribute_name', nil)
38
38
  expected = <<-HTML
39
39
  <select id="attribute_name" name="attribute_name">
40
- <option value="OC">Oceania</option>
41
- <option value="EU">Eurasia</option>
42
40
  <option value="ES">Eastasia</option>
41
+ <option value="EU">Eurasia</option>
42
+ <option value="OC">Oceania</option>
43
43
  </select>
44
44
  HTML
45
45
 
@@ -52,14 +52,14 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
52
52
  end
53
53
 
54
54
  def test_priority_country_select
55
- html = country_select(@object, :country_code, {priority: ['ES']})
55
+ html = country_select(@object, :country_code, {:priority => ['ES']})
56
56
  expected = <<-HTML
57
57
  <select id="object_country_code" name="object[country_code]">
58
58
  <option value="ES">Eastasia</option>
59
59
  <option disabled>-------------</option>
60
- <option value="OC">Oceania</option>
61
- <option value="EU">Eurasia</option>
62
60
  <option value="ES">Eastasia</option>
61
+ <option value="EU">Eurasia</option>
62
+ <option value="OC">Oceania</option>
63
63
  </select>
64
64
  HTML
65
65
 
@@ -69,7 +69,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
69
69
  def test_basic_subregion_select
70
70
  oceania = Carmen::Country.coded('OC')
71
71
  expected = <<-HTML
72
- <select id="object_subregion_code" name="object[subregion_code]"><option value="AO">Airstrip One</option></select>
72
+ <select id="object_subregion_code" name="object[subregion_code]">
73
+ <option value="AO">Airstrip One</option>
74
+ </select>
73
75
  HTML
74
76
 
75
77
  html = subregion_select(@object, :subregion_code, oceania)
@@ -79,7 +81,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
79
81
 
80
82
  def test_subregion_select_using_parent_code
81
83
  expected = <<-HTML
82
- <select id="object_subregion_code" name="object[subregion_code]"><option value="AO">Airstrip One</option></select>
84
+ <select id="object_subregion_code" name="object[subregion_code]">
85
+ <option value="AO">Airstrip One</option>
86
+ </select>
83
87
  HTML
84
88
 
85
89
  html = subregion_select(@object, :subregion_code, 'OC')
@@ -89,7 +93,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
89
93
 
90
94
  def test_subregion_select_using_parent_code_array
91
95
  expected = <<-HTML
92
- <select id="object_subregion_code" name="object[subregion_code]"><option value="LO">London</option></select>
96
+ <select id="object_subregion_code" name="object[subregion_code]">
97
+ <option value="LO">London</option>
98
+ </select>
93
99
  HTML
94
100
 
95
101
  html = subregion_select(@object, :subregion_code, ['OC', 'AO'])
@@ -109,7 +115,9 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
109
115
  def test_basic_subregion_select
110
116
  oceania = Carmen::Country.coded('OC')
111
117
  expected = <<-HTML
112
- <select id="subregion_code" name="subregion_code"><option value="AO">Airstrip One</option></select>
118
+ <select id="subregion_code" name="subregion_code">
119
+ <option value="AO">Airstrip One</option>
120
+ </select>
113
121
  HTML
114
122
 
115
123
  html = subregion_select_tag(:subregion_code, nil, oceania)
@@ -117,6 +125,31 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
117
125
  assert_equal_markup(expected, html)
118
126
  end
119
127
 
128
+ def test_region_options_for_select
129
+ regions = Carmen::Country.all
130
+ expected = <<-HTML
131
+ <option value="ES">Eastasia</option>
132
+ <option value="EU">Eurasia</option>
133
+ <option value="OC" selected="selected">Oceania</option>
134
+ HTML
135
+ html = region_options_for_select(regions, 'OC')
136
+
137
+ assert_equal_markup(expected, html)
138
+ end
139
+
140
+ def test_region_options_for_select_with_array_of_regions_and_priority
141
+ regions = [Carmen::Country.coded('ES'), Carmen::Country.coded('EU')]
142
+ expected = <<-HTML
143
+ <option value="ES">Eastasia</option>
144
+ <option disabled>-------------</option>
145
+ <option value="ES">Eastasia</option>
146
+ <option value="EU">Eurasia</option>
147
+ HTML
148
+ html = region_options_for_select(regions, nil, :priority => ['ES'])
149
+
150
+ assert_equal_markup(expected, html)
151
+ end
152
+
120
153
  def method_missing(method, *args)
121
154
  fail "method_missing #{method}"
122
155
  end
@@ -7,13 +7,9 @@ require 'minitest/autorun'
7
7
  require 'action_view/test_case'
8
8
 
9
9
  require 'rails'
10
- require 'carmen'
11
10
  require 'carmen-rails'
12
11
  require 'ostruct'
13
12
 
14
- require 'yaml'
15
- YAML::ENGINE.yamler = 'syck'
16
-
17
13
  MiniTest::Spec.register_spec_type(/.*/, ActionView::TestCase)
18
14
 
19
15
  Carmen.clear_data_paths
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carmen-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta2
4
+ version: 1.0.0.beta3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-09 00:00:00.000000000Z
12
+ date: 2012-05-21 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70301067158160 !ruby/object:Gem::Requirement
16
+ requirement: &70153544790280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70301067158160
24
+ version_requirements: *70153544790280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: carmen
27
- requirement: &70301067142400 !ruby/object:Gem::Requirement
27
+ requirement: &70153544786740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0.beta2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70301067142400
35
+ version_requirements: *70153544786740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70301067141240 !ruby/object:Gem::Requirement
38
+ requirement: &70153544785780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70301067141240
46
+ version_requirements: *70153544785780
47
47
  description: Provides country_select and subregion_select form helpers.
48
48
  email:
49
49
  - jim@autonomousmachine.com