1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

added ability to disable automatically added classes to div/label/input

This commit is contained in:
Michał Krzyżanowski 2012-02-13 23:22:49 +01:00
parent cd3eeb7d1a
commit d959016bbf
6 changed files with 73 additions and 3 deletions

View file

@ -73,6 +73,34 @@ module SimpleForm
mattr_accessor :form_class
@@form_class = :simple_form
# Add input_type class to input. Default is true.
mattr_accessor :add_input_type_class_to_input
@@add_input_type_class_to_input = true
# Add required class to input. Default is true.
mattr_accessor :add_required_class_to_input
@@add_required_class_to_input = true
# Add readonly class to input. Defualt is true.
mattr_accessor :add_readonly_class_to_input
@@add_readonly_class_to_input = true
# Add disabled class to input. Default is true.
mattr_accessor :add_disabled_class_to_input
@@add_disabled_class_to_input = true
# Add input_type class to label. Default is true.
mattr_accessor :add_input_type_to_label
@@add_input_type_to_label = true
# Add required class to label. Default is true.
mattr_accessor :add_required_class_to_label
@@add_required_class_to_label = true
# Add input classes to wrapper. Default is true.
mattr_accessor :add_input_classes_to_wrapper
@@add_input_classes_to_wrapper = true
# Whether attributes are required by default (or not).
mattr_accessor :required_by_default
@@required_by_default = true

View file

@ -38,7 +38,11 @@ module SimpleForm
end
def label_html_options
label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact)
@label_html_classes = [input_type, required_class, SimpleForm.label_class].compact.reject do |klass|
klass == (input_type unless SimpleForm.add_input_type_to_label) || \
klass == (required_class unless SimpleForm.add_required_class_to_label)
end
label_options = html_options_for(:label, @label_html_classes)
label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id)
label_options
end

View file

@ -59,7 +59,13 @@ module SimpleForm
# Notice that html_options_for receives a reference to input_html_classes.
# This means that classes added dynamically to input_html_classes will
# still propagate to input_html_options.
@html_classes = [input_type, required_class, readonly_class, disabled_class].compact
@html_classes = [input_type, required_class, readonly_class, disabled_class].compact.reject do |klass|
klass == (input_type unless SimpleForm.add_input_type_class_to_input) || \
klass == (required_class unless SimpleForm.add_required_class_to_input) || \
klass == (readonly_class unless SimpleForm.add_readonly_class_to_input) || \
klass == (disabled_class unless SimpleForm.add_disabled_class_to_input)
end
@input_html_classes = @html_classes.dup
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
o[:readonly] = true if has_readonly?

View file

@ -24,7 +24,7 @@ module SimpleForm
def html_classes(input, options)
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
css += input.html_classes
css += input.html_classes if SimpleForm.add_input_classes_to_wrapper
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint?
css

View file

@ -166,6 +166,21 @@ class IsolatedLabelTest < ActionView::TestCase
assert_select 'label.datetime'
end
test 'label should not have css class from type if add_input_type_to_label if false' do
swap SimpleForm, :add_input_type_to_label => false do
with_label_for @user, :name, :string
assert_no_select 'label.string'
with_label_for @user, :description, :text
assert_no_select 'label.text'
with_label_for @user, :age, :integer
assert_no_select 'label.integer'
with_label_for @user, :born_at, :date
assert_no_select 'label.date'
with_label_for @user, :created_at, :datetime
assert_no_select 'label.datetime'
end
end
test 'label should obtain required from ActiveModel::Validations when it is included' do
with_label_for @validating_user, :name, :string
assert_select 'label.required'
@ -173,6 +188,15 @@ class IsolatedLabelTest < ActionView::TestCase
assert_select 'label.optional'
end
test 'label should not obtain required from ActiveModel::Validations when add_required_class_to_label is false' do
swap SimpleForm, :add_required_class_to_label => false do
with_label_for @validating_user, :name, :string
assert_no_select 'label.required'
with_label_for @validating_user, :status, :string
assert_no_select 'label.optional'
end
end
test 'label should allow overriding required when ActiveModel::Validations is included' do
with_label_for @validating_user, :name, :string, :required => false
assert_select 'label.optional'

View file

@ -38,6 +38,14 @@ class FormBuilderTest < ActionView::TestCase
end
end
test 'builder should allow to skip input_type class' do
swap SimpleForm, :add_input_type_class_to_input => false do
with_form_for @user, :post_count
assert_no_select "form input#user_post_count.integer"
assert_select "form input#user_post_count"
end
end
test 'builder should allow adding custom input mappings for integer input types' do
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
with_form_for @user, :lock_version