carmen-rails 1.0.0.beta3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -50,3 +50,13 @@ carmen-rails [have been thoroughly TomDoc'ed](https://github.com/jim/carmen-rail
50
50
 
51
51
  There is a [live demo app](http://carmen-rails-demo.herokuapp.com) that shows
52
52
  carmen-rails in action, and includes a [step-by-step setup guide](https://github.com/jim/carmen-demo-app#readme).
53
+
54
+ ## Configuration
55
+
56
+ Using this library will automatically set Carmen to use [Rails' built-in I18n functionality](http://guides.rubyonrails.org/i18n.html). This means that changing
57
+ some configuration should be done through Rails and not Carmen. For example, adding paths for additional locale files
58
+ should be done inside `config/application.rb`:
59
+
60
+ ``` ruby
61
+ config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
62
+ ```
@@ -45,9 +45,24 @@ module ActionView
45
45
  #
46
46
  # country_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
47
47
  #
48
+ # Note that in order to preserve compatibility with various existing
49
+ # libraries, an alternative API is supported but not recommended:
50
+ #
51
+ # country_select(@object, :region, ['US', 'CA'], class: region)
52
+ #
48
53
  # 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))
54
+ def country_select(object, method, *args)
55
+
56
+ # These contortions are to provide API-compatibility
57
+ priority_countries = args.shift if args.first.is_a?(Array)
58
+ options, html_options = args
59
+
60
+ options ||= {}
61
+ options[:priority] = priority_countries if priority_countries
62
+
63
+ html_options ||= {}
64
+
65
+ tag = InstanceTag.new(object, method, self)
51
66
  tag.to_region_select_tag(Carmen::World.instance, options, html_options)
52
67
  end
53
68
 
@@ -85,7 +100,9 @@ module ActionView
85
100
  end
86
101
 
87
102
  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}
103
+ main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s}
104
+ main_options.unshift [options['prompt'], ''] if options['prompt']
105
+
89
106
  region_options += options_for_select(main_options, selected)
90
107
  region_options.html_safe
91
108
  end
@@ -133,10 +150,9 @@ module ActionView
133
150
  def subregion_select_tag(name, value, parent_region_or_code, options = {}, html_options = {})
134
151
  options.stringify_keys!
135
152
  parent_region = determine_parent(parent_region_or_code)
136
- priority_regions = options.delete(:priority) || []
137
- opts = region_options_for_select(parent_region.subregions, value, :priority => priority_regions)
153
+ opts = region_options_for_select(parent_region.subregions, value, options)
138
154
  html_options = {"name" => name,
139
- "id" => sanitize_to_id(name)}.update(options.stringify_keys)
155
+ "id" => sanitize_to_id(name)}.update(html_options.stringify_keys)
140
156
  content_tag(:select, opts, html_options)
141
157
  end
142
158
 
@@ -157,7 +173,7 @@ module ActionView
157
173
  end
158
174
 
159
175
  class InstanceTag
160
- def to_region_select_tag(parent_region, options, html_options)
176
+ def to_region_select_tag(parent_region, options = {}, html_options = {})
161
177
  html_options = html_options.stringify_keys
162
178
  add_default_name_and_id(html_options)
163
179
  priority_regions = options[:priority] || []
@@ -173,9 +189,8 @@ module ActionView
173
189
  # web form.
174
190
  #
175
191
  # See `FormOptionsHelper::country_select` for more information.
176
- def country_select(method, options = {}, html_options = {})
177
- @template.country_select(@object_name, method,
178
- options.merge(:object => @object), html_options)
192
+ def country_select(method, *args)
193
+ @template.country_select(@object_name, method, *args)
179
194
  end
180
195
 
181
196
  # Generate select and subregion option tags with the provided name. A
@@ -183,9 +198,8 @@ module ActionView
183
198
  # a given country.
184
199
  #
185
200
  # See `FormOptionsHelper::subregion_select` for more information.
186
- def subregion_select(method, parent_region_or_code, options={}, html_options={})
187
- @template.subregion_select(@object_name, method, parent_region_or_code,
188
- options.merge(:object => @object), html_options)
201
+ def subregion_select(method, parent_region_or_code, *args)
202
+ @template.subregion_select(@object_name, method, parent_region_or_code, *args)
189
203
  end
190
204
  end
191
205
 
@@ -1,5 +1,5 @@
1
1
  module Carmen
2
2
  module Rails
3
- VERSION = "1.0.0.beta3"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -46,13 +46,41 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
46
46
  assert_equal_markup(expected, html)
47
47
  end
48
48
 
49
+ def test_country_select_tag_with_prompt
50
+ html = country_select_tag('attribute_name', nil, :prompt => 'Please Select')
51
+ expected = <<-HTML
52
+ <select id="attribute_name" name="attribute_name">
53
+ <option value="">Please Select</option>
54
+ <option value="ES">Eastasia</option>
55
+ <option value="EU">Eurasia</option>
56
+ <option value="OC">Oceania</option>
57
+ </select>
58
+ HTML
59
+
60
+ assert_equal_markup(expected, html)
61
+ end
49
62
  def test_country_tag_selected_value
50
63
  @html = country_select_tag(:country_code, 'OC')
51
64
  assert_select('option[selected="selected"][value="OC"]')
52
65
  end
53
66
 
54
67
  def test_priority_country_select
55
- html = country_select(@object, :country_code, {:priority => ['ES']})
68
+ html = country_select(@object, :country_code, :priority => ['ES'])
69
+ expected = <<-HTML
70
+ <select id="object_country_code" name="object[country_code]">
71
+ <option value="ES">Eastasia</option>
72
+ <option disabled>-------------</option>
73
+ <option value="ES">Eastasia</option>
74
+ <option value="EU">Eurasia</option>
75
+ <option value="OC">Oceania</option>
76
+ </select>
77
+ HTML
78
+
79
+ assert_equal_markup(expected, html)
80
+ end
81
+
82
+ def test_priority_country_select_deprecated_api
83
+ html = country_select(@object, :country_code, ['ES'], {})
56
84
  expected = <<-HTML
57
85
  <select id="object_country_code" name="object[country_code]">
58
86
  <option value="ES">Eastasia</option>
@@ -112,7 +140,7 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
112
140
  assert_select('option[selected="selected"][value="AO"]')
113
141
  end
114
142
 
115
- def test_basic_subregion_select
143
+ def test_basic_subregion_select_tag
116
144
  oceania = Carmen::Country.coded('OC')
117
145
  expected = <<-HTML
118
146
  <select id="subregion_code" name="subregion_code">
@@ -125,6 +153,35 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
125
153
  assert_equal_markup(expected, html)
126
154
  end
127
155
 
156
+ def test_subregion_select_tag_with_priority
157
+ oceania = Carmen::Country.coded('OC')
158
+ expected = <<-HTML
159
+ <select id="subregion_code" name="subregion_code">
160
+ <option value="AO">Airstrip One</option>
161
+ <option disabled>-------------</option>
162
+ <option value="AO">Airstrip One</option>
163
+ </select>
164
+ HTML
165
+
166
+ html = subregion_select_tag(:subregion_code, nil, oceania, :priority => ['AO'])
167
+
168
+ assert_equal_markup(expected, html)
169
+ end
170
+
171
+ def test_subregion_select_tag_with_prompt
172
+ oceania = Carmen::Country.coded('OC')
173
+ expected = <<-HTML
174
+ <select id="subregion_code" name="subregion_code">
175
+ <option value="">Please select</option>
176
+ <option value="AO">Airstrip One</option>
177
+ </select>
178
+ HTML
179
+
180
+ html = subregion_select_tag(:subregion_code, nil, oceania, :prompt => 'Please select')
181
+
182
+ assert_equal_markup(expected, html)
183
+ end
184
+
128
185
  def test_region_options_for_select
129
186
  regions = Carmen::Country.all
130
187
  expected = <<-HTML
@@ -150,7 +207,35 @@ class CarmenViewHelperTest < MiniTest::Unit::TestCase
150
207
  assert_equal_markup(expected, html)
151
208
  end
152
209
 
153
- def method_missing(method, *args)
154
- fail "method_missing #{method}"
210
+ def test_form_builder_country_select
211
+ form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{})
212
+
213
+ html = form.country_select('attribute_name')
214
+ expected = <<-HTML
215
+ <select id="object_attribute_name" name="object[attribute_name]">
216
+ <option value="ES">Eastasia</option>
217
+ <option value="EU">Eurasia</option>
218
+ <option value="OC">Oceania</option>
219
+ </select>
220
+ HTML
221
+
222
+ assert_equal_markup(expected, html)
223
+ end
224
+
225
+ def test_form_builder_country_select_deprecated_api
226
+ form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{})
227
+
228
+ html = form.country_select('attribute_name', ['ES'])
229
+ expected = <<-HTML
230
+ <select id="object_attribute_name" name="object[attribute_name]">
231
+ <option value="ES">Eastasia</option>
232
+ <option disabled>-------------</option>
233
+ <option value="ES">Eastasia</option>
234
+ <option value="EU">Eurasia</option>
235
+ <option value="OC">Oceania</option>
236
+ </select>
237
+ HTML
238
+
239
+ assert_equal_markup(expected, html)
155
240
  end
156
241
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carmen-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta3
5
- prerelease: 6
4
+ version: 1.0.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jim Benton
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-21 00:00:00.000000000Z
12
+ date: 2013-04-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70153544790280 !ruby/object:Gem::Requirement
16
+ requirement: &2160706040 !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: *70153544790280
24
+ version_requirements: *2160706040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: carmen
27
- requirement: &70153544786740 !ruby/object:Gem::Requirement
27
+ requirement: &2160704840 !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: *70153544786740
35
+ version_requirements: *2160704840
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70153544785780 !ruby/object:Gem::Requirement
38
+ requirement: &2160677320 !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: *70153544785780
46
+ version_requirements: *2160677320
47
47
  description: Provides country_select and subregion_select form helpers.
48
48
  email:
49
49
  - jim@autonomousmachine.com
@@ -75,12 +75,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
75
  required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  none: false
77
77
  requirements:
78
- - - ! '>'
78
+ - - ! '>='
79
79
  - !ruby/object:Gem::Version
80
- version: 1.3.1
80
+ version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 1.8.10
83
+ rubygems_version: 1.8.15
84
84
  signing_key:
85
85
  specification_version: 3
86
86
  summary: Rails adapter for Carmen