Skip to content
Browse files

simplify configuration

  • Loading branch information...
1 parent 21b1bb0 commit 436a59c6c3b9452d6c61bd2c4a3a80efdb8b3e91 @krzyzak krzyzak committed Feb 16, 2012
View
30 lib/simple_form.rb
@@ -73,33 +73,9 @@ 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
+ # You can define which elements should obtain additional classes
+ mattr_accessor :generate_additional_classes_for
+ @@generate_additional_classes_for = [:wrapper, :label, :input]
# Whether attributes are required by default (or not).
mattr_accessor :required_by_default
View
6 lib/simple_form/components/labels.rb
@@ -38,10 +38,8 @@ def label_target
end
def label_html_options
- @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_html_classes = SimpleForm.generate_additional_classes_for.include?(:label) ? [input_type, required_class, SimpleForm.label_class].compact : []
+
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
View
7 lib/simple_form/inputs/base.rb
@@ -59,12 +59,7 @@ def initialize(builder, attribute_name, column, input_type, options = {})
# 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.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
+ @html_classes = SimpleForm.generate_additional_classes_for.include?(:input) ? [input_type, required_class, readonly_class, disabled_class].compact : []
@input_html_classes = @html_classes.dup
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
View
2 lib/simple_form/wrappers/root.rb
@@ -24,7 +24,7 @@ def find(name)
def html_classes(input, options)
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
- css += input.html_classes if SimpleForm.add_input_classes_to_wrapper
+ css += input.html_classes if SimpleForm.generate_additional_classes_for.include?(: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
8 test/components/label_test.rb
@@ -166,8 +166,8 @@ def @controller.action_name; nil; end
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
+ test 'label should not have css class from type when generate_additional_classes_for does not include :label' do
+ swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
with_label_for @user, :name, :string
assert_no_select 'label.string'
with_label_for @user, :description, :text
@@ -188,8 +188,8 @@ def @controller.action_name; nil; end
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
+ test 'label should not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
+ swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do
with_label_for @validating_user, :name, :string
assert_no_select 'label.required'
with_label_for @validating_user, :status, :string
View
2 test/form_builder/general_test.rb
@@ -39,7 +39,7 @@ def with_custom_form_for(object, *args, &block)
end
test 'builder should allow to skip input_type class' do
- swap SimpleForm, :add_input_type_class_to_input => false do
+ swap SimpleForm, :generate_additional_classes_for => [:label, :wrapper] do
with_form_for @user, :post_count
assert_no_select "form input#user_post_count.integer"
assert_select "form input#user_post_count"

0 comments on commit 436a59c

Please sign in to comment.
Something went wrong with that request. Please try again.