Add i18n cache to speed up form build

This commit is contained in:
José Valim 2009-12-09 16:03:04 -02:00
parent 43c5e7aeed
commit cb6aca650e
6 changed files with 48 additions and 17 deletions

View File

@ -3,6 +3,7 @@ require 'simple_form/input'
require 'simple_form/hint'
require 'simple_form/error'
require 'simple_form/map_type'
require 'simple_form/i18n_cache'
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
@ -12,6 +13,7 @@ module SimpleForm
include SimpleForm::Error
extend SimpleForm::MapType
extend SimpleForm::I18nCache
map_type :boolean, :to => :check_box
map_type :text, :to => :text_area

View File

@ -0,0 +1,18 @@
module SimpleForm
# A lot of configuration valeus are retrived from I18n,
# like boolean collection, required string. This module provides
# caching facility to speed up form construciton.
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
def reset_i18n_cache(key)
instance_variable_set(:"@#{key}", {})
end
end
end

View File

@ -1,6 +1,19 @@
module SimpleForm
module Input
def self.included(base) #:nodoc:
base.extend ClassMethods
end
module ClassMethods
def boolean_collection
i18n_cache :boolean_collection do
[ [I18n.t(:"simple_form.true", :default => 'Yes'), true],
[I18n.t(:"simple_form.false", :default => 'No'), false] ]
end
end
end
private
def generate_input
@ -14,7 +27,7 @@ module SimpleForm
args = [ @attribute ]
if mapping.collection
collection = @options[:collection] || boolean_collection
collection = @options[:collection] || self.class.boolean_collection
detect_collection_methods(collection, @options)
args.push(collection, @options[:value_method], @options[:label_method])
end
@ -25,11 +38,6 @@ module SimpleForm
send(mapping.method, *args)
end
def boolean_collection
[ [I18n.t(:"simple_form.true", :default => 'Yes'), true],
[I18n.t(:"simple_form.false", :default => 'No'), false] ]
end
def detect_collection_methods(collection, options)
sample = collection.first

View File

@ -1,18 +1,17 @@
module SimpleForm
module Label
def self.included(base)
base.class_eval do
extend ClassMethods
@translate_required_string = {}
end
def self.included(base) #:nodoc:
base.extend ClassMethods
end
module ClassMethods
module ClassMethods #:nodoc:
def translate_required_string
@translate_required_string[I18n.locale] ||= I18n.t(:"simple_form.required.string", :default =>
%[<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr> ]
)
i18n_cache :translate_required_string do
I18n.t(:"simple_form.required.string", :default =>
%[<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr> ]
)
end
end
def translate_required_text
@ -42,7 +41,7 @@ module SimpleForm
end
def required_text
attribute_required? ? self.class.translate_required_string : ''
attribute_required? ? self.class.translate_required_string.dup : ''
end
def translate_label

View File

@ -2,6 +2,10 @@ require 'test_helper'
class InputTest < ActionView::TestCase
setup do
SimpleForm::FormBuilder.reset_i18n_cache :boolean_collection
end
test 'input should verify options hash' do
assert_raise ArgumentError do
simple_form_for @user do |f|

View File

@ -3,7 +3,7 @@ require 'test_helper'
class LabelTest < ActionView::TestCase
setup do
SimpleForm::FormBuilder.instance_variable_set(:@translate_required_string, {})
SimpleForm::FormBuilder.reset_i18n_cache :translate_required_string
end
test 'input should generate a label with the text field' do