Permalink
Browse files

Add I18n features to the form builder. Tests included.

  • Loading branch information...
1 parent 871bd94 commit 8357be2e19cb4cc8c3e5958ee7bef272fbeb572f @calas committed Jun 2, 2011
@@ -43,8 +43,9 @@ def simple_field(label = nil, content = nil, options = {}, &block)
end
def label_for(field, options={})
- label = options.delete(:label) || object.class.human_attribute_name(field)
- "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe
+ label = options.delete(:label) || object.class.human_attribute_name(field).titleize
+ for_value = options[:id] || "#{object_name}_#{field}"
+ %Q{<label for="#{for_value}">#{label}</label>}.html_safe
end
def submit(value, options = {}, &block)
View
@@ -66,11 +66,31 @@ def assert_exception_raised(exception_class = nil, error_message = nil, &block)
flunk 'Exception was not raised'
end
end
-
+
+ def assert_no_select(selector, value = nil)
+ assert_select(selector, :text => value, :count => 0)
+ end
+
# Small method that allows for better formatting in tests
def rendered_content_formatter(string)
string.gsub(/^[ ]+/, '')
end
+
+ # Example usage:
+ # with_translations(:en, :sections => { :products => "Our Products" }) do
+ # assert_equal I18n.translate('products', :scope => 'sections'), "Our Products"
+ # assert_equal I18n.translate('sections'), { :products => "Our Products" }
+ # end
+ def with_translations(locale, translations, &block)
+ begin
+ I18n.backend.store_translations locale, translations
+ I18n.locale = locale
+ yield
+ ensure
+ I18n.reload!
+ end
+ end
+
end
class ActionController::TestCase
@@ -0,0 +1,92 @@
+require 'test_helper'
+
+class ComfortableMexicanSofa::FormBuilderTest < ActionView::TestCase
+ include ComfortableMexicanSofa::ViewMethods
+
+ setup :setup_cms_page
+
+ def setup_cms_page
+ @cms_page = Cms::Page.new
+ end
+
+ def cms_admin_page_path(*args)
+ '/cms-admin/pages'
+ end
+ alias :cms_admin_pages_path :cms_admin_page_path
+
+ def with_concat_form_for(object, &block)
+ concat cms_form_for(object, :url => cms_admin_page_path, &block)
+ end
+
+ def with_form_for(object, *args, &block)
+ with_concat_form_for(object) do |f|
+ f.text_field(*args) + (block.call(f) if block_given?)
+ end
+ end
+
+ test "labels for inputs with custom id should reference the input correctly" do
+ with_form_for(@cms_page, :label, :id => 'slugify')
+ assert_no_select 'label#slugify'
+ assert_select 'label[for="slugify"]', 'Label'
+ assert_select "input#slugify[name='cms_page[label]']"
+ end
+
+ test "label_for method returns html_safe strings" do
+ cms_form_for @cms_page, :url => cms_admin_pages_path do |f|
+ assert f.label_for(:is_published).html_safe?, "must be html_safe string"
+ end
+ end
+
+ test "the label options are not passed to the input element" do
+ with_form_for(@cms_page, :label)
+ assert_no_select 'input[for="cms_page_label"]'
+ end
+
+ test "the label text is automatically translated" do
+ with_translations :test_lang, {
+ :attributes => { :slug => "Gulsty" },
+ :activerecord => { :attributes => { :'cms/page' => { :label => 'Titlumtimpin' } } }
+ } do
+ with_form_for(@cms_page, :label) do |f|
+ f.text_field(:slug) +
+ f.text_field(:parent_id)
+ end
+ assert_select 'label[for="cms_page_label"]', 'Titlumtimpin', 'using model specific attribute names'
+ assert_select 'label[for="cms_page_slug"]', 'Gulsty', 'using common attribute names'
+ assert_select 'label[for="cms_page_parent_id"]', 'Parent', 'using default humanized attribute name'
+ end
+ end
+
+ test "the label text is titleized" do
+ with_translations :test_lang, :attributes => { :label => "two words" } do
+ with_form_for(@cms_page, :label)
+ assert_select 'label[for="cms_page_label"]', "Two Words", "Label is titleized"
+ end
+ end
+
+ test "the label can be hard coded" do
+ with_form_for(@cms_page, :slug, :label => "Custom Path")
+ assert_select 'label[for="cms_page_slug"]', "Custom Path"
+ end
+
+ test "basic form builder features" do
+ with_form_for(@cms_page, :label) do |f|
+ f.text_field(:slug) +
+ f.select(:parent_id, [['1', 'Parent']])
+ end
+ assert_select 'form' do
+ assert_select 'div.form_element.text_field_element' do
+ assert_select 'label[for="cms_page_label"]', 'Label'
+ assert_select 'input#cms_page_label'
+ end
+ assert_select 'div.form_element.text_field_element' do
+ assert_select 'label[for="cms_page_slug"]', 'Slug'
+ assert_select 'input#cms_page_slug'
+ end
+ assert_select 'div.form_element.select_element' do
+ assert_select 'select#cms_page_parent_id'
+ assert_select 'label', 'Parent'
+ end
+ end
+ end
+end

0 comments on commit 8357be2

Please sign in to comment.