Add I18n features to the form builder. Tests included.
Jorge Calás Lozano
committed Jun 02, 2011
commit 8dae1545fda65c0e13ceb7645d7d6877372571f6
Showing 4
changed files with
144 additions
and 9 deletions
config/locales/en.yml
+26
-4
| @@ | @@ -1,5 +1,27 @@ |
| - | # Sample localization file for English. Add more files in this directory for other locales. |
| - | # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. |
| - | |
| en: | |
| - | hello: "Hello world" |
| + | activerecord: |
| + | models: |
| + | cms/site: 'Site' |
| + | cms/layout: 'Layout' |
| + | cms/page: 'Page' |
| + | cms/snippet: 'Snippet' |
| + | attributes: |
| + | cms/site: |
| + | label: 'Label' |
| + | hostname: 'Hostname' |
| + | path: 'Path' |
| + | locale: 'Locale' |
| + | is_mirrored: 'Mirrored' |
| + | cms/layout: |
| + | label: 'Layout Name' |
| + | app_layout: 'App Layout' |
| + | parent_id: 'Parent Layout' |
| + | css: 'Stylesheets' |
| + | js: 'Javascript' |
| + | cms/page: |
| + | label: 'Label' |
| + | layout_id: 'Layout' |
| + | slug: 'Slug' |
| + | target_page_id: 'Redirect to Page' |
| + | is_published: 'Published' |
| + | |
| \ No newline at end of file | |
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb
+5
-4
| @@ | @@ -42,9 +42,10 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder |
| ).html_safe | |
| end | |
| - | def label_for(field, options) |
| - | label = options.delete(:label) || field.to_s.titleize.capitalize_all |
| - | "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe |
| + | def label_for(field, options={}) |
| + | 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) | |
| @@ | @@ -117,4 +118,4 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder |
| default_tag_field(tag, :content_field_method => :text_area_tag) | |
| end | |
| - | end |
| \ No newline at end of file | |
| + | end |
test/test_helper.rb
+21
-1
| @@ | @@ -62,11 +62,31 @@ class ActiveSupport::TestCase |
| 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 | |
test/unit/comfortable_mexican_sofa/form_builder_test.rb
+92
-0
| @@ | @@ -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 |