Allow component default tag to be changed.
This commit is contained in:
parent
2881e775c6
commit
af89e090f2
|
@ -7,4 +7,15 @@ module SimpleForm
|
|||
autoload :I18nCache, 'simple_form/i18n_cache'
|
||||
autoload :MapType, 'simple_form/map_type'
|
||||
autoload :RequiredHelpers, 'simple_form/required_helpers'
|
||||
|
||||
# Default tag used in componenents.
|
||||
mattr_accessor :component_tag
|
||||
@@component_tag = :span
|
||||
|
||||
# Components used by the form builder.
|
||||
mattr_accessor :components
|
||||
@@components = [
|
||||
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
||||
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
||||
]
|
||||
end
|
|
@ -40,7 +40,7 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def component_tag(content)
|
||||
template.content_tag(:span, content, :class => basename)
|
||||
template.content_tag(SimpleForm.component_tag, content, :class => basename)
|
||||
end
|
||||
|
||||
def translate(default='')
|
||||
|
|
|
@ -1,20 +1,12 @@
|
|||
module SimpleForm
|
||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
# Components used by the folder builder.
|
||||
# By default is [:label, :input, :hint, :error].
|
||||
cattr_accessor :components, :instance_writer => false
|
||||
@@components = [
|
||||
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
||||
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
||||
]
|
||||
|
||||
# Make the template accessible for components
|
||||
attr_reader :template
|
||||
|
||||
def input(attribute, options={})
|
||||
input_type = default_input_type(attribute, options)
|
||||
|
||||
pieces = self.components.collect do |klass|
|
||||
pieces = SimpleForm.components.collect do |klass|
|
||||
next if options[klass.basename] == false
|
||||
klass.new(self, attribute, input_type, options).generate
|
||||
end
|
||||
|
@ -22,24 +14,24 @@ module SimpleForm
|
|||
pieces.compact.join
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def default_input_type(attribute, options)
|
||||
return options[:as].to_sym if options[:as]
|
||||
return :select if options[:collection]
|
||||
def default_input_type(attribute, options)
|
||||
return options[:as].to_sym if options[:as]
|
||||
return :select if options[:collection]
|
||||
|
||||
column = @object.column_for_attribute(attribute)
|
||||
input_type = column.type
|
||||
column = @object.column_for_attribute(attribute)
|
||||
input_type = column.type
|
||||
|
||||
case input_type
|
||||
when :timestamp
|
||||
:datetime
|
||||
when :string, nil
|
||||
attribute.to_s =~ /password/ ? :password : :string
|
||||
else
|
||||
input_type
|
||||
end
|
||||
case input_type
|
||||
when :timestamp
|
||||
:datetime
|
||||
when :string, nil
|
||||
attribute.to_s =~ /password/ ? :password : :string
|
||||
else
|
||||
input_type
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,6 +27,13 @@ class ErrorTest < ActionView::TestCase
|
|||
assert_select 'span.hint', 'Use with care...'
|
||||
end
|
||||
|
||||
test 'hint uses the current component tag set' do
|
||||
swap SimpleForm, :component_tag => :p do
|
||||
with_hint_for(:name, :string, :hint => 'Use with care...')
|
||||
assert_select 'p.hint', 'Use with care...'
|
||||
end
|
||||
end
|
||||
|
||||
test 'hint should use i18n with model and attribute to lookup translation' do
|
||||
store_translations(:en, :simple_form => { :hints => { :user => { :name =>
|
||||
'Content of this input will be capitalized...'
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
module I18nHelper
|
||||
def store_translations(locale, translations, &block)
|
||||
begin
|
||||
I18n.backend.store_translations locale, translations
|
||||
yield
|
||||
ensure
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
module MiscHelpers
|
||||
def store_translations(locale, translations, &block)
|
||||
begin
|
||||
I18n.backend.store_translations locale, translations
|
||||
yield
|
||||
ensure
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
def assert_no_select(*args)
|
||||
assert_raise Test::Unit::AssertionFailedError do
|
||||
assert_select(*args)
|
||||
end
|
||||
end
|
||||
|
||||
def swap(object, new_values)
|
||||
old_values = {}
|
||||
new_values.each do |key, value|
|
||||
old_values[key] = object.send key
|
||||
object.send :"#{key}=", value
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
old_values.each do |key, value|
|
||||
object.send :"#{key}=", value
|
||||
end
|
||||
end
|
||||
end
|
|
@ -16,7 +16,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|||
I18n.default_locale = :en
|
||||
|
||||
class ActionView::TestCase
|
||||
include I18nHelper
|
||||
include MiscHelpers
|
||||
|
||||
tests SimpleForm::ActionViewExtensions::FormHelper
|
||||
|
||||
|
@ -48,11 +48,4 @@ class ActionView::TestCase
|
|||
'/users'
|
||||
end
|
||||
alias :super_user_path :user_path
|
||||
|
||||
# Wrapper to assert no select exists
|
||||
def assert_no_select(*args)
|
||||
assert_raise Test::Unit::AssertionFailedError do
|
||||
assert_select(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue