formtastic 2.1.0.beta1 → 2.1.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/Appraisals +1 -1
- data/CHANGELOG +38 -1
- data/README.textile +20 -4
- data/gemfiles/rails-3.2.gemfile +1 -1
- data/lib/formtastic/actions/button_action.rb +2 -2
- data/lib/formtastic/actions/input_action.rb +2 -2
- data/lib/formtastic/actions/link_action.rb +2 -2
- data/lib/formtastic/form_builder.rb +1 -0
- data/lib/formtastic/helpers/action_helper.rb +3 -3
- data/lib/formtastic/helpers/actions_helper.rb +4 -4
- data/lib/formtastic/helpers/input_helper.rb +26 -10
- data/lib/formtastic/helpers/inputs_helper.rb +9 -2
- data/lib/formtastic/inputs/country_input.rb +4 -3
- data/lib/formtastic/localizer.rb +46 -6
- data/lib/formtastic/version.rb +1 -1
- data/lib/generators/templates/_form.html.erb +1 -1
- data/lib/generators/templates/formtastic.rb +4 -0
- data/spec/actions/generic_action_spec.rb +1 -0
- data/spec/helpers/inputs_helper_spec.rb +38 -2
- data/spec/localizer_spec.rb +108 -0
- data/spec/spec_helper.rb +5 -1
- metadata +6 -5
data/.travis.yml
CHANGED
data/Appraisals
CHANGED
data/CHANGELOG
CHANGED
@@ -1,4 +1,41 @@
|
|
1
|
-
|
1
|
+
2.1.0.rc
|
2
|
+
|
3
|
+
* documentation improvements
|
4
|
+
* improved performance by caching of Formtastic's i18n lookups
|
5
|
+
* improved performance in production by checking for defined constants instead of catching the exception
|
6
|
+
* fixed arguments passed into fields_for being flattened one level too deep
|
7
|
+
* added an optional `index` that an inputs() block can access for use in labels and other HTML attributes
|
8
|
+
|
9
|
+
2.1.0.beta
|
10
|
+
|
11
|
+
* Added Rails 3.2 compatibility
|
12
|
+
* Added a new Actions DSL (f.actions, f.action) — see below for deprecation of the Buttons DSL
|
13
|
+
* Added new i18n_localizer configuration, allowing you to use your own localiser class instead of Formtastic::Localizer
|
14
|
+
* Added a hidden input before mutli-selects, to allow full clearing of the select (like we do for checkboxes)
|
15
|
+
* Added support for integers in a :collection for radio and check_boxes inputs
|
16
|
+
* Added support for time inputs with no current value to default render blank inputs rather than pre-selecting the current time
|
17
|
+
* Brought back the Form Generator from 1.2.x versions of Formtastic
|
18
|
+
* Added support for placeholder text on textareas (text inputs)
|
19
|
+
* Deprecated the Buttons DSL (f.buttons, f.commit_button) in favor of the new Actions DSL — see above
|
20
|
+
* Removed the previously deprecated :label_method, :value_method & :group_label_method options
|
21
|
+
* Removed the previously deprecated :as => :numeric
|
22
|
+
* Removed the previously deprecated inline_errors_for and related methods
|
23
|
+
* Removed the previously deprecated SemanticFormHelper and SemanticFormBuilder
|
24
|
+
* Fixed the behavior of :include_blank and :prompt options to be inline with Rails’
|
25
|
+
* Fixed that :input_html => { :multiple => true } did not force a single choice select into a multi choice
|
26
|
+
* Fixed date, time and datetime legend labels to correspond to the first visible input, rather than the first input (which may be hidden)
|
27
|
+
* Fixed that DateInput should treat fragments excluded from :order option as discarded
|
28
|
+
* Fixed that the :wrapper_html options could not be reused in the view (like in a with_options block) because they were modified by Formtastic
|
29
|
+
* Fixed numerous Mongoid and MongoMapper compatibility issues
|
30
|
+
* Fixed that we should be calculating the length of integer columns as bytes
|
31
|
+
* Fixed many inputs (date, datetime, time, checkboxes, select & boolean) that did not correctly use the :index option in fields_for
|
32
|
+
* Fixed Haml and Slim template indentation
|
33
|
+
* Fixed invalid html output for nested inputs with multiple siblings
|
34
|
+
* Fixed i18n keys with nested objects
|
35
|
+
* Many documentation fixes and improvements
|
36
|
+
* A few performance improvements
|
37
|
+
|
38
|
+
2.0.2
|
2
39
|
|
3
40
|
* Fixed that MongoMapper does not use `associations(method)`. `.associations` is an accessor.
|
4
41
|
|
data/README.textile
CHANGED
@@ -7,7 +7,8 @@ Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far e
|
|
7
7
|
|
8
8
|
h2. Compatibility
|
9
9
|
|
10
|
-
* Formtastic 2.
|
10
|
+
* Formtastic 2.1.x is Rails 3.x compatible
|
11
|
+
* Formtastic 2.0.x is Rails 3.0.x and 3.1.x compatible only
|
11
12
|
* Formtastic 1.x is compatible with both Rails 2 and 3, and is being maintained for bug fixes in the the "1.2-stable branch":https://github.com/justinfrench/formtastic/tree/1.2-stable. View the README in that branch for installation instructions, etc.
|
12
13
|
* Formtastic, much like Rails, is very ActiveRecord-centric. Many are successfully using other ActiveModel-like ORMs and objects (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing full compatibility at this stage. Patches are welcome!
|
13
14
|
|
@@ -194,7 +195,7 @@ If you want to customize the label text, or render some hint text below the fiel
|
|
194
195
|
<%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
|
195
196
|
<% end %>
|
196
197
|
<%= f.actions do %>
|
197
|
-
<%= f.action
|
198
|
+
<%= f.action :submit %>
|
198
199
|
<% end %>
|
199
200
|
<% end %>
|
200
201
|
</pre>
|
@@ -237,6 +238,21 @@ When working in has many association, you can even supply @"%i"@ in your fieldse
|
|
237
238
|
<% end %>
|
238
239
|
</pre>
|
239
240
|
|
241
|
+
Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
|
242
|
+
|
243
|
+
<pre>
|
244
|
+
<%= semantic_form_for @post do |f| %>
|
245
|
+
<%= f.inputs :for => :categories do |category, i| %>
|
246
|
+
<% if i <= 2 %>
|
247
|
+
<%= f.inputs :name => "Category ##{i}" %>
|
248
|
+
<% else %>
|
249
|
+
<%= f.inputs :name => "Category ##{i} (optional)" %>
|
250
|
+
<% end %>
|
251
|
+
<% end %>
|
252
|
+
<%= f.actions %>
|
253
|
+
<% end %>
|
254
|
+
</pre>
|
255
|
+
|
240
256
|
If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
|
241
257
|
|
242
258
|
<pre>
|
@@ -426,7 +442,7 @@ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the
|
|
426
442
|
<%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
|
427
443
|
<% end %>
|
428
444
|
<%= f.actions do %>
|
429
|
-
<%= f.action
|
445
|
+
<%= f.action :submit %> # => "Create my %{model}"
|
430
446
|
<% end %>
|
431
447
|
<% end %>
|
432
448
|
</pre>
|
@@ -454,7 +470,7 @@ _Note: Slightly different because Formtastic can't guess how you group fields in
|
|
454
470
|
<%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
|
455
471
|
<% end %>
|
456
472
|
<%= f.actions do %>
|
457
|
-
<%= f.
|
473
|
+
<%= f.action :submit, :label => :dummie %> # => "Launch!"
|
458
474
|
<% end %>
|
459
475
|
<% end %>
|
460
476
|
</pre>
|
data/gemfiles/rails-3.2.gemfile
CHANGED
@@ -14,10 +14,10 @@
|
|
14
14
|
# <form...>
|
15
15
|
# <fieldset class="actions">
|
16
16
|
# <ol>
|
17
|
-
# <li class="action button_action" id="
|
17
|
+
# <li class="action button_action" id="post_reset_action">
|
18
18
|
# <button type="reset" value="Reset">
|
19
19
|
# </li>
|
20
|
-
# <li class="action button_action" id="
|
20
|
+
# <li class="action button_action" id="post_submit_action">
|
21
21
|
# <button type="submit" value="Create Post">
|
22
22
|
# </li>
|
23
23
|
# </ol>
|
@@ -18,10 +18,10 @@
|
|
18
18
|
# <form...>
|
19
19
|
# <fieldset class="actions">
|
20
20
|
# <ol>
|
21
|
-
# <li class="action input_action" id="
|
21
|
+
# <li class="action input_action" id="post_reset_action">
|
22
22
|
# <input type="reset" value="Reset">
|
23
23
|
# </li>
|
24
|
-
# <li class="action input_action" id="
|
24
|
+
# <li class="action input_action" id="post_submit_action">
|
25
25
|
# <input type="submit" value="Create Post">
|
26
26
|
# </li>
|
27
27
|
# </ol>
|
@@ -19,9 +19,9 @@
|
|
19
19
|
# <fieldset class="actions">
|
20
20
|
# <ol>
|
21
21
|
# <li class="action input_action" id="post_submit_action">
|
22
|
-
# <input type="
|
22
|
+
# <input type="submit" value="Create Post">
|
23
23
|
# </li>
|
24
|
-
# <li class="action link_action" id="
|
24
|
+
# <li class="action link_action" id="post_cancel_action">
|
25
25
|
# <a href="javascript:history.back()">Cancel</a>
|
26
26
|
# </li>
|
27
27
|
# </ol>
|
@@ -23,6 +23,7 @@ module Formtastic
|
|
23
23
|
configure :file_metadata_suffixes, ['content_type', 'file_name', 'file_size']
|
24
24
|
configure :priority_countries, ["Australia", "Canada", "United Kingdom", "United States"]
|
25
25
|
configure :i18n_lookups_by_default, true
|
26
|
+
configure :i18n_cache_lookups, true
|
26
27
|
configure :i18n_localizer, Formtastic::Localizer
|
27
28
|
configure :escape_html_entities_in_hints_and_labels, true
|
28
29
|
configure :default_commit_button_accesskey
|
@@ -17,9 +17,9 @@ module Formtastic
|
|
17
17
|
# <%= semantic_form_for @post do |f| %>
|
18
18
|
# ...
|
19
19
|
# <%= f.actions do %>
|
20
|
-
# <%= f.action
|
21
|
-
# <%= f.action
|
22
|
-
# <%= f.action
|
20
|
+
# <%= f.action :submit %>
|
21
|
+
# <%= f.action :reset %>
|
22
|
+
# <%= f.action :cancel %>
|
23
23
|
# <% end %>
|
24
24
|
# <% end %>
|
25
25
|
#
|
@@ -10,8 +10,8 @@ module Formtastic
|
|
10
10
|
# <%= semantic_form_for @post do |f| %>
|
11
11
|
# ...
|
12
12
|
# <%= f.actions do %>
|
13
|
-
# <%= f.action
|
14
|
-
# <%= f.action
|
13
|
+
# <%= f.action :submit
|
14
|
+
# <%= f.action :cancel
|
15
15
|
# <% end %>
|
16
16
|
# <% end %>
|
17
17
|
#
|
@@ -62,8 +62,8 @@ module Formtastic
|
|
62
62
|
# <% semantic_form_for @post do |f| %>
|
63
63
|
# ...
|
64
64
|
# <% f.actions do %>
|
65
|
-
# <%= f.action
|
66
|
-
# <%= f.action
|
65
|
+
# <%= f.action :submit %>
|
66
|
+
# <%= f.action :cancel %>
|
67
67
|
# <% end %>
|
68
68
|
# <% end %>
|
69
69
|
#
|
@@ -330,20 +330,36 @@ module Formtastic
|
|
330
330
|
#
|
331
331
|
# @example When a top-level class is found
|
332
332
|
# input_class(:string) #=> StringInput
|
333
|
-
# input_class(:awesome) #=> AwesomeInput
|
333
|
+
# input_class(:awesome) #=> AwesomeInput
|
334
334
|
def input_class(as)
|
335
335
|
@input_classes_cache ||= {}
|
336
336
|
@input_classes_cache[as] ||= begin
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
337
|
+
Rails.application.config.cache_classes ? input_class_with_const_defined(as) : input_class_by_trying(as)
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
# prevent exceptions in production environment for better performance
|
342
|
+
def input_class_with_const_defined(as)
|
343
|
+
input_class_name = custom_input_class_name(as)
|
344
|
+
|
345
|
+
if ::Object.const_defined?(input_class_name)
|
346
|
+
input_class_name.constantize
|
347
|
+
elsif Formtastic::Inputs.const_defined?(input_class_name)
|
348
|
+
standard_input_class_name(as).constantize
|
349
|
+
else
|
350
|
+
raise Formtastic::UnknownInputError
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# use auto-loading in development environment
|
355
|
+
def input_class_by_trying(as)
|
356
|
+
begin
|
357
|
+
custom_input_class_name(as).constantize
|
358
|
+
rescue NameError
|
359
|
+
standard_input_class_name(as).constantize
|
346
360
|
end
|
361
|
+
rescue NameError
|
362
|
+
raise Formtastic::UnknownInputError
|
347
363
|
end
|
348
364
|
|
349
365
|
# :as => :string # => StringInput
|
@@ -370,7 +370,14 @@ module Formtastic
|
|
370
370
|
raise ArgumentError, 'You gave :for option with a block to inputs method, ' <<
|
371
371
|
'but the block does not accept any argument.' if block.arity <= 0
|
372
372
|
lambda do |f|
|
373
|
-
contents = f.inputs(*args)
|
373
|
+
contents = f.inputs(*args) do
|
374
|
+
if block.arity == 1 # for backwards compatibility with REE & Ruby 1.8.x
|
375
|
+
block.call(f)
|
376
|
+
else
|
377
|
+
index = parent_child_index(options[:parent]) if options[:parent]
|
378
|
+
block.call(f, index)
|
379
|
+
end
|
380
|
+
end
|
374
381
|
template.concat(contents)
|
375
382
|
end
|
376
383
|
else
|
@@ -380,7 +387,7 @@ module Formtastic
|
|
380
387
|
end
|
381
388
|
end
|
382
389
|
|
383
|
-
fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten
|
390
|
+
fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten(1)
|
384
391
|
fields_for(*fields_for_args, &fields_for_block)
|
385
392
|
end
|
386
393
|
|
@@ -4,7 +4,8 @@ module Formtastic
|
|
4
4
|
# Rails doesn't come with a `country_select` helper by default any more, so you'll need to do
|
5
5
|
# one of the following:
|
6
6
|
#
|
7
|
-
# * install
|
7
|
+
# * install the [country-select](https://github.com/jamesds/country-select) gem
|
8
|
+
# * install the no longer maintained [official Rails plugin](http://github.com/rails/iso-3166-country-select)
|
8
9
|
# * install any other country_select plugin that behaves in a similar way
|
9
10
|
# * roll your own `country_select` helper with the same args and options as the Rails one
|
10
11
|
#
|
@@ -50,7 +51,7 @@ module Formtastic
|
|
50
51
|
include Base
|
51
52
|
|
52
53
|
def to_html
|
53
|
-
raise "To use the :country input, please install a country_select plugin, like this one:
|
54
|
+
raise "To use the :country input, please install a country_select plugin, like this one: https://github.com/jamesds/country-select" unless builder.respond_to?(:country_select)
|
54
55
|
input_wrapping do
|
55
56
|
label_html <<
|
56
57
|
builder.country_select(method, priority_countries, input_options, input_html_options)
|
@@ -63,4 +64,4 @@ module Formtastic
|
|
63
64
|
|
64
65
|
end
|
65
66
|
end
|
66
|
-
end
|
67
|
+
end
|
data/lib/formtastic/localizer.rb
CHANGED
@@ -24,27 +24,59 @@ module Formtastic
|
|
24
24
|
# 'formtastic.labels.post.title'
|
25
25
|
# 'formtastic.labels.title'
|
26
26
|
class Localizer
|
27
|
-
|
27
|
+
class Cache
|
28
|
+
def get(key)
|
29
|
+
cache[key]
|
30
|
+
end
|
31
|
+
|
32
|
+
def has_key?(key)
|
33
|
+
cache.has_key?(key)
|
34
|
+
end
|
35
|
+
|
36
|
+
def set(key, result)
|
37
|
+
cache[key] = result
|
38
|
+
end
|
39
|
+
|
40
|
+
def cache
|
41
|
+
@cache ||= {}
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear!
|
45
|
+
cache.clear
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
28
49
|
attr_accessor :builder
|
29
|
-
|
50
|
+
|
51
|
+
def self.cache
|
52
|
+
@cache ||= Cache.new
|
53
|
+
end
|
54
|
+
|
30
55
|
def initialize(current_builder)
|
31
56
|
self.builder = current_builder
|
32
57
|
end
|
33
58
|
|
34
59
|
def localize(key, value, type, options = {}) #:nodoc:
|
35
60
|
key = value if value.is_a?(::Symbol)
|
36
|
-
|
61
|
+
|
37
62
|
if value.is_a?(::String)
|
38
63
|
escape_html_entities(value)
|
39
64
|
else
|
40
65
|
use_i18n = value.nil? ? i18n_lookups_by_default : (value != false)
|
41
|
-
|
66
|
+
use_cache = i18n_cache_lookups
|
67
|
+
cache = self.class.cache
|
68
|
+
|
42
69
|
if use_i18n
|
43
70
|
model_name, nested_model_name = normalize_model_name(builder.model_name.underscore)
|
44
71
|
|
45
72
|
action_name = builder.template.params[:action].to_s rescue ''
|
46
73
|
attribute_name = key.to_s
|
47
|
-
|
74
|
+
|
75
|
+
# look in the cache first
|
76
|
+
if use_cache
|
77
|
+
cache_key = [::I18n.locale, action_name, model_name, nested_model_name, attribute_name, key, value, type, options]
|
78
|
+
return cache.get(cache_key) if cache.has_key?(cache_key)
|
79
|
+
end
|
48
80
|
|
49
81
|
defaults = Formtastic::I18n::SCOPES.reject do |i18n_scope|
|
50
82
|
nested_model_name.nil? && i18n_scope.match(/nested_model/)
|
@@ -71,7 +103,11 @@ module Formtastic
|
|
71
103
|
options[:default] = defaults
|
72
104
|
i18n_value = ::I18n.t(default_key, options)
|
73
105
|
end
|
74
|
-
|
106
|
+
|
107
|
+
# save the result to the cache
|
108
|
+
result = (i18n_value.is_a?(::String) && i18n_value.present?) ? escape_html_entities(i18n_value) : nil
|
109
|
+
cache.set(cache_key, result) if use_cache
|
110
|
+
result
|
75
111
|
end
|
76
112
|
end
|
77
113
|
end
|
@@ -105,6 +141,10 @@ module Formtastic
|
|
105
141
|
def i18n_lookups_by_default
|
106
142
|
builder.i18n_lookups_by_default
|
107
143
|
end
|
144
|
+
|
145
|
+
def i18n_cache_lookups
|
146
|
+
builder.i18n_cache_lookups
|
147
|
+
end
|
108
148
|
|
109
149
|
end
|
110
150
|
end
|
data/lib/formtastic/version.rb
CHANGED
@@ -68,6 +68,10 @@
|
|
68
68
|
# i.e. :label => true, or :hint => true (or opposite depending on initialized value)
|
69
69
|
# Formtastic::FormBuilder.i18n_lookups_by_default = false
|
70
70
|
|
71
|
+
# Specifies if I18n lookups of the default I18n Localizer should be cached to improve performance.
|
72
|
+
# Defaults to false.
|
73
|
+
# Formtastic::FormBuilder.i18n_cache_lookups = true
|
74
|
+
|
71
75
|
# Specifies the class to use for localization lookups. You can create your own
|
72
76
|
# class and use it instead by subclassing Formtastic::Localizer (which is the default).
|
73
77
|
# Formtastic::FormBuilder.i18n_localizer = MyOwnLocalizer
|
@@ -122,6 +122,18 @@ describe 'Formtastic::FormBuilder#inputs' do
|
|
122
122
|
output_buffer.should have_tag("form input[@name='post[authors_attributes][1][login]']")
|
123
123
|
output_buffer.should_not have_tag('form fieldset[@name]')
|
124
124
|
end
|
125
|
+
|
126
|
+
it 'should include an indexed :label input for each item' do
|
127
|
+
concat(semantic_form_for(@new_post) do |post|
|
128
|
+
post.inputs :for => :authors do |author, index|
|
129
|
+
concat(author.input(:login, :label => "#{index}", :required => false))
|
130
|
+
end
|
131
|
+
end)
|
132
|
+
|
133
|
+
output_buffer.should have_tag("form fieldset.inputs label", "1", :count => 1)
|
134
|
+
output_buffer.should have_tag("form fieldset.inputs label", "2", :count => 1)
|
135
|
+
output_buffer.should_not have_tag('form fieldset legend')
|
136
|
+
end
|
125
137
|
end
|
126
138
|
|
127
139
|
describe 'as an array containing the a symbole for the association name and the associated object' do
|
@@ -187,7 +199,7 @@ describe 'Formtastic::FormBuilder#inputs' do
|
|
187
199
|
output_buffer.should_not have_tag('fieldset[@builder="Formtastic::Helpers::FormHelper"]')
|
188
200
|
end
|
189
201
|
|
190
|
-
it 'should send parent_builder as an option to allow child index interpolation' do
|
202
|
+
it 'should send parent_builder as an option to allow child index interpolation for legends' do
|
191
203
|
concat(semantic_form_for(@new_post) do |builder|
|
192
204
|
builder.instance_variable_set('@nested_child_index', 0)
|
193
205
|
inputs = builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
|
@@ -199,7 +211,7 @@ describe 'Formtastic::FormBuilder#inputs' do
|
|
199
211
|
output_buffer.should have_tag('fieldset legend', 'Author #1')
|
200
212
|
end
|
201
213
|
|
202
|
-
it 'should also provide child index interpolation when nested child index is a hash' do
|
214
|
+
it 'should also provide child index interpolation for legends when nested child index is a hash' do
|
203
215
|
concat(semantic_form_for(@new_post) do |builder|
|
204
216
|
builder.instance_variable_set('@nested_child_index', :author => 10)
|
205
217
|
inputs = builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
|
@@ -210,6 +222,30 @@ describe 'Formtastic::FormBuilder#inputs' do
|
|
210
222
|
|
211
223
|
output_buffer.should have_tag('fieldset legend', 'Author #11')
|
212
224
|
end
|
225
|
+
|
226
|
+
it 'should send parent_builder as an option to allow child index interpolation for labels' do
|
227
|
+
concat(semantic_form_for(@new_post) do |builder|
|
228
|
+
builder.instance_variable_set('@nested_child_index', 'post[author_attributes]' => 0)
|
229
|
+
inputs = builder.inputs :for => [:author, @bob] do |bob_builder, index|
|
230
|
+
concat(bob_builder.input(:name, :label => "Author ##{index}", :required => false))
|
231
|
+
end
|
232
|
+
concat(inputs)
|
233
|
+
end)
|
234
|
+
|
235
|
+
output_buffer.should have_tag('fieldset label', 'Author #1')
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'should also provide child index interpolation for labels when nested child index is a hash' do
|
239
|
+
concat(semantic_form_for(@new_post) do |builder|
|
240
|
+
builder.instance_variable_set('@nested_child_index', 'post[author_attributes]' => 10)
|
241
|
+
inputs = builder.inputs :for => [:author, @bob] do |bob_builder, index|
|
242
|
+
concat(bob_builder.input(:name, :label => "Author ##{index}", :required => false))
|
243
|
+
end
|
244
|
+
concat(inputs)
|
245
|
+
end)
|
246
|
+
|
247
|
+
output_buffer.should have_tag('fieldset label', 'Author #11')
|
248
|
+
end
|
213
249
|
end
|
214
250
|
|
215
251
|
describe 'when a :name or :title option is provided' do
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Formtastic::Localizer' do
|
5
|
+
describe "Cache" do
|
6
|
+
before do
|
7
|
+
@cache = Formtastic::Localizer::Cache.new
|
8
|
+
@key = ['model', 'name']
|
9
|
+
@undefined_key = ['model', 'undefined']
|
10
|
+
@cache.set(@key, 'value')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should get value" do
|
14
|
+
@cache.get(@key).should == 'value'
|
15
|
+
@cache.get(@undefined_key).should be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should check if key exists?" do
|
19
|
+
@cache.has_key?(@key).should be_true
|
20
|
+
@cache.has_key?(@undefined_key).should be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should set a key" do
|
24
|
+
@cache.set(['model', 'name2'], 'value2')
|
25
|
+
@cache.get(['model', 'name2']).should == 'value2'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return hash" do
|
29
|
+
@cache.cache.should be_an_instance_of(Hash)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should clear the cache" do
|
33
|
+
@cache.clear!
|
34
|
+
@cache.get(@key).should be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "Localizer" do
|
39
|
+
include FormtasticSpecHelper
|
40
|
+
|
41
|
+
before do
|
42
|
+
mock_everything
|
43
|
+
|
44
|
+
with_config :i18n_lookups_by_default, true do
|
45
|
+
semantic_form_for(@new_post) do |builder|
|
46
|
+
@localizer = Formtastic::Localizer.new(builder)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
after do
|
52
|
+
::I18n.backend.reload!
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be defined" do
|
56
|
+
lambda { Formtastic::Localizer }.should_not raise_error(::NameError)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have a cache" do
|
60
|
+
Formtastic::Localizer.cache.should be_an_instance_of(Formtastic::Localizer::Cache)
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "localize" do
|
64
|
+
def store_post_translations(value)
|
65
|
+
::I18n.backend.store_translations :en, {:formtastic => {
|
66
|
+
:labels => {
|
67
|
+
:post => { :name => value }
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
before do
|
74
|
+
store_post_translations('POST.NAME')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should translate key with i18n" do
|
78
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NAME'
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "with caching" do
|
82
|
+
it "should not update translation when stored translations change" do
|
83
|
+
with_config :i18n_cache_lookups, true do
|
84
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NAME'
|
85
|
+
store_post_translations('POST.NEW_NAME')
|
86
|
+
|
87
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NAME'
|
88
|
+
|
89
|
+
Formtastic::Localizer.cache.clear!
|
90
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NEW_NAME'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "without caching" do
|
96
|
+
it "should update translation when stored translations change" do
|
97
|
+
with_config :i18n_cache_lookups, false do
|
98
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NAME'
|
99
|
+
store_post_translations('POST.NEW_NAME')
|
100
|
+
@localizer.localize(:name, :name, :label).should == 'POST.NEW_NAME'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -419,10 +419,14 @@ end
|
|
419
419
|
::ActiveSupport::Deprecation.silenced = false
|
420
420
|
|
421
421
|
RSpec.configure do |config|
|
422
|
+
config.before(:each) do
|
423
|
+
Formtastic::Localizer.cache.clear!
|
424
|
+
end
|
425
|
+
|
422
426
|
config.before(:all) do
|
423
427
|
DeferredGarbageCollection.start unless ENV["DEFER_GC"] == "false"
|
424
428
|
end
|
425
429
|
config.after(:all) do
|
426
|
-
DeferredGarbageCollection.reconsider unless ENV["DEFER_GC"] == "false"
|
430
|
+
DeferredGarbageCollection.reconsider unless ENV["DEFER_GC"] == "false"
|
427
431
|
end
|
428
432
|
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formtastic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7712050
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 1
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
|
12
|
-
version: 2.1.0.beta1
|
10
|
+
- rc
|
11
|
+
version: 2.1.0.rc
|
13
12
|
platform: ruby
|
14
13
|
authors:
|
15
14
|
- Justin French
|
@@ -17,7 +16,7 @@ autorequire:
|
|
17
16
|
bindir: bin
|
18
17
|
cert_chain: []
|
19
18
|
|
20
|
-
date: 2012-
|
19
|
+
date: 2012-02-16 00:00:00 +11:00
|
21
20
|
default_executable:
|
22
21
|
dependencies:
|
23
22
|
- !ruby/object:Gem::Dependency
|
@@ -349,6 +348,7 @@ files:
|
|
349
348
|
- spec/inputs/time_zone_input_spec.rb
|
350
349
|
- spec/inputs/url_input_spec.rb
|
351
350
|
- spec/inputs/with_options_spec.rb
|
351
|
+
- spec/localizer_spec.rb
|
352
352
|
- spec/spec.opts
|
353
353
|
- spec/spec_helper.rb
|
354
354
|
- spec/support/custom_macros.rb
|
@@ -434,6 +434,7 @@ test_files:
|
|
434
434
|
- spec/inputs/time_zone_input_spec.rb
|
435
435
|
- spec/inputs/url_input_spec.rb
|
436
436
|
- spec/inputs/with_options_spec.rb
|
437
|
+
- spec/localizer_spec.rb
|
437
438
|
- spec/spec.opts
|
438
439
|
- spec/spec_helper.rb
|
439
440
|
- spec/support/custom_macros.rb
|