Permalink
Browse files

Add possibility to use i18n translatios in submit FormHelper.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
carlosantoniodasilva authored and josevalim committed Jan 12, 2010
1 parent f921ad5 commit 8e0208f650eac23ee4b5c748d2de715344c5c629
@@ -505,7 +505,7 @@ def fields_for(record_or_name_or_array, *args, &block)
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
- # is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
+ # is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
# Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
# onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to
# target labels for radio_button tags (where the value is used in the ID of the input tag).
@@ -1058,7 +1058,7 @@ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
def radio_button(method, tag_value, options = {})
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
end
-
+
def hidden_field(method, options = {})
@emitted_hidden_id = true if method == :id
@template.hidden_field(@object_name, method, objectify_options(options))
@@ -1072,7 +1072,18 @@ def error_messages(options = {})
@template.error_messages_for(@object_name, objectify_options(options))
end
- def submit(value = "Save changes", options = {})
+ def submit(value = nil, options = {})
+ value ||= begin
+ key = @object ? (@object.new_record? ? :create : :update) : :submit
+ model = if @object.class.respond_to?(:model_name)
+ @object.class.model_name.human
+ else
+ @object_name.to_s.humanize
+ end
+
+ I18n.t(:"helpers.submit.#{key}", :model => model, :default => "#{key.to_s.humanize} #{model}")
+ end
+
@template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
end
@@ -9,7 +9,7 @@
delimiter: ","
# Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
precision: 3
-
+
# Used in number_to_currency()
currency:
format:
@@ -20,28 +20,28 @@
separator: "."
delimiter: ","
precision: 2
-
+
# Used in number_to_percentage()
percentage:
format:
# These three are to override number.format and are optional
- # separator:
+ # separator:
delimiter: ""
- # precision:
-
+ # precision:
+
# Used in number_to_precision()
precision:
format:
# These three are to override number.format and are optional
# separator:
delimiter: ""
# precision:
-
+
# Used in number_to_human_size()
human:
format:
# These three are to override number.format and are optional
- # separator:
+ # separator:
delimiter: ""
precision: 1
storage_units:
@@ -112,5 +112,12 @@
helpers:
select:
- # default value for :prompt => true in FormOptionsHelper
+ # Default value for :prompt => true in FormOptionsHelper
prompt: "Please select"
+
+ # Default translation keys for submit FormHelper
+ submit:
+ create: 'Create {{model}}'
+ update: 'Update {{model}}'
+ submit: 'Save {{model}}'
+
@@ -25,6 +25,17 @@ def setup
}
}
+ # Create "submit" locale for testing I18n submit helpers
+ I18n.backend.store_translations 'submit', {
+ :helpers => {
+ :submit => {
+ :create => 'Create {{model}}',
+ :update => 'Confirm {{model}} changes',
+ :submit => 'Save changes'
+ }
+ }
+ }
+
@post = Post.new
@comment = Comment.new
def @post.errors()
@@ -475,6 +486,52 @@ def test_form_for_with_nil_index_option_override
assert_dom_equal expected, output_buffer
end
+ def test_submit_with_object_as_new_record_and_locale_strings
+ old_locale, I18n.locale = I18n.locale, :submit
+
+ def @post.new_record?() true; end
+ form_for(:post, @post) do |f|
+ concat f.submit
+ end
+
+ expected = "<form action='http://www.example.com' method='post'>" +
+ "<input name='commit' id='post_submit' type='submit' value='Create Post' />" +
+ "</form>"
+ assert_dom_equal expected, output_buffer
+ ensure
+ I18n.locale = old_locale
+ end
+
+ def test_submit_with_object_as_existing_record_and_locale_strings
+ old_locale, I18n.locale = I18n.locale, :submit
+
+ form_for(:post, @post) do |f|
+ concat f.submit
+ end
+
+ expected = "<form action='http://www.example.com' method='post'>" +
+ "<input name='commit' id='post_submit' type='submit' value='Confirm Post changes' />" +
+ "</form>"
+ assert_dom_equal expected, output_buffer
+ ensure
+ I18n.locale = old_locale
+ end
+
+ def test_submit_without_object_and_locale_strings
+ old_locale, I18n.locale = I18n.locale, :submit
+
+ form_for(:post) do |f|
+ concat f.submit
+ end
+
+ expected = "<form action='http://www.example.com' method='post'>" +
+ "<input name='commit' id='post_submit' type='submit' value='Save changes' />" +
+ "</form>"
+ assert_dom_equal expected, output_buffer
+ ensure
+ I18n.locale = old_locale
+ end
+
def test_nested_fields_for
form_for(:post, @post) do |f|
f.fields_for(:comment, @post) do |c|
@@ -659,7 +716,7 @@ def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to
assert_dom_equal expected, output_buffer
end
-
+
def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement
@post.author = Author.new(321)
@@ -670,7 +727,7 @@ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_o
concat af.text_field(:name)
end
end
-
+
expected = '<form action="http://www.example.com" method="post">' +
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
@@ -715,7 +772,7 @@ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collecti
end
end
end
-
+
expected = '<form action="http://www.example.com" method="post">' +
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +

0 comments on commit 8e0208f

Please sign in to comment.