Deprecate translate in favor of disabling the automatic lookup in hints and placeholders.

This commit is contained in:
José Valim 2011-12-04 15:49:57 +01:00
parent 94dab68cd7
commit 50b24b5cbe
9 changed files with 29 additions and 27 deletions

View File

@ -99,10 +99,10 @@ module SimpleForm
mattr_accessor :default_input_size
@@default_input_size = 50
# When off, do not use translations in hint, labels and placeholders.
# It is a small performance improvement if you are not using such features.
mattr_accessor :translate
@@translate = true
# When off, do not use translations in labels. Disabling translation in
# hints and placeholders can be done manually in the wrapper API.
mattr_accessor :translate_labels
@@translate_labels = true
# Automatically discover new inputs in Rails' autoload path.
mattr_accessor :inputs_discovery
@ -165,7 +165,17 @@ module SimpleForm
@@deprecated = false
DEPRECATED.each do |method|
class_eval "def #{method}; @@deprecated = true; end"
class_eval "def self.#{method}; @@deprecated = true; end"
end
def self.translate=(value)
ActiveSupport::Deprecation.warn "SimpleForm.translate= is disabled in favor of translate_labels="
self.translate_labels = value
end
def self.translate
ActiveSupport::Deprecation.warn "SimpleForm.translate is disabled in favor of translate_labels"
self.translate_labels
end
# Default way to setup SimpleForm. Run rails generate simple_form:install

View File

@ -1,17 +0,0 @@
module SimpleForm
module Components
module Disabled
def disabled
if has_disabled?
input_html_classes << 'disabled'
input_html_options[:disabled] = true
end
nil
end
def has_disabled?
options[:disabled] == true
end
end
end
end

View File

@ -1,8 +1,13 @@
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Hints
def hint
(options.delete(:hint) || translate(:hints)).presence
if options[:hint] == true
translate(:hints).presence
else
options[:hint]
end
end
end
end

View File

@ -56,7 +56,7 @@ module SimpleForm
# First check labels translation and then human attribute name.
def label_translation #:nodoc:
translate(:labels) || if object.class.respond_to?(:human_attribute_name)
(SimpleForm.translate_labels && translate(:labels)) || if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
else
attribute_name.to_s.humanize

View File

@ -1,5 +1,6 @@
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Maxlength
def maxlength
input_html_options[:maxlength] ||= maximum_length_from_validation || limit

View File

@ -1,5 +1,6 @@
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Pattern
def pattern
input_html_options[:pattern] ||= pattern_source

View File

@ -1,5 +1,6 @@
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Placeholders
def placeholder
input_html_options[:placeholder] ||= placeholder_text

View File

@ -39,6 +39,9 @@ module SimpleForm
self.default_options = options
end
# Always enabled.
enable :hint
# Usually disabled, needs to be enabled explicitly passing true as option.
disable :maxlength, :placeholder, :pattern
@ -126,8 +129,6 @@ module SimpleForm
#
# Take a look at our locale example file.
def translate(namespace, default='')
return nil unless SimpleForm.translate
model_names = lookup_model_names.dup
lookups = []

View File

@ -80,7 +80,7 @@ class IsolatedLabelTest < ActionView::TestCase
end
test 'input should not use i18n label if translate is false' do
swap SimpleForm, :translate => false do
swap SimpleForm, :translate_labels => false do
store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do
with_label_for @user, :age, :integer
assert_select 'label[for=user_age]', /Age/