From 057df0c6323fa8bd15bb66cdf1a91cc922588b94 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 9 Dec 2009 16:20:17 -0200 Subject: [PATCH] Cache other required options and fix typo --- lib/simple_form/i18n_cache.rb | 8 ++++---- lib/simple_form/label.rb | 8 ++++++-- test/label_test.rb | 4 +++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/simple_form/i18n_cache.rb b/lib/simple_form/i18n_cache.rb index bacef6ce..fc8e567d 100644 --- a/lib/simple_form/i18n_cache.rb +++ b/lib/simple_form/i18n_cache.rb @@ -1,12 +1,12 @@ module SimpleForm - # A lot of configuration valeus are retrived from I18n, + # A lot of configuration values are retrived from I18n, # like boolean collection, required string. This module provides - # caching facility to speed up form construciton. + # caching facility to speed up form construction. module I18nCache def i18n_cache(key) get_i18n_cache(key)[I18n.locale] ||= yield.freeze end - + def get_i18n_cache(key) instance_variable_get(:"@#{key}") || reset_i18n_cache(key) end @@ -15,4 +15,4 @@ module SimpleForm instance_variable_set(:"@#{key}", {}) end end -end \ No newline at end of file +end diff --git a/lib/simple_form/label.rb b/lib/simple_form/label.rb index 42d5d5a2..0597f81a 100644 --- a/lib/simple_form/label.rb +++ b/lib/simple_form/label.rb @@ -15,11 +15,15 @@ module SimpleForm end def translate_required_text - I18n.t(:"simple_form.required.text", :default => 'required') + i18n_cache :translate_required_text do + I18n.t(:"simple_form.required.text", :default => 'required') + end end def translate_required_mark - I18n.t(:"simple_form.required.mark", :default => '*') + i18n_cache :translate_required_mark do + I18n.t(:"simple_form.required.mark", :default => '*') + end end end diff --git a/test/label_test.rb b/test/label_test.rb index 926b78f6..9c96e9f8 100644 --- a/test/label_test.rb +++ b/test/label_test.rb @@ -3,7 +3,9 @@ require 'test_helper' class LabelTest < ActionView::TestCase setup do - SimpleForm::FormBuilder.reset_i18n_cache :translate_required_string + [:string, :text, :mark].each do |cache| + SimpleForm::FormBuilder.reset_i18n_cache :"translate_required_#{cache}" + end end test 'input should generate a label with the text field' do