Skip to content

Commit

Permalink
Merge pull request heartcombo#455 from krzyzak/master
Browse files Browse the repository at this point in the history
Add ability not to add classes to div/label/input
  • Loading branch information
carlosantoniodasilva committed Feb 17, 2012
2 parents afec50a + e7cac9e commit 2dafe48
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
Expand Up @@ -138,6 +138,9 @@ SimpleForm.setup do |config|
# You can define the class to use on all forms. Default is simple_form.
# config.form_class = :simple_form

# You can define which elements should obtain additional classes
# config.generate_additional_classes_for = [:wrapper, :label, :input]

# Whether attributes are required by default (or not). Default is true.
# config.required_by_default = true

Expand Down
4 changes: 4 additions & 0 deletions lib/simple_form.rb
Expand Up @@ -73,6 +73,10 @@ module SimpleForm
mattr_accessor :form_class
@@form_class = :simple_form

# 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
@@required_by_default = true
Expand Down
4 changes: 3 additions & 1 deletion lib/simple_form/components/labels.rb
Expand Up @@ -38,7 +38,9 @@ def label_target
end

def label_html_options
label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact)
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
end
Expand Down
5 changes: 3 additions & 2 deletions lib/simple_form/inputs/base.rb
Expand Up @@ -59,7 +59,8 @@ 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
@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|
o[:readonly] = true if has_readonly?
Expand Down Expand Up @@ -100,7 +101,7 @@ def html_options_for(namespace, css_classes)
html_options = options[:"#{namespace}_html"] || {}
css_classes << html_options[:class] if html_options.key?(:class)
html_options[:class] = css_classes
html_options
html_options.delete_if{|key, value| value.blank?}
end

# Lookup translations for the given namespace using I18n, based on object name,
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/wrappers/root.rb
Expand Up @@ -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
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
Expand Down
24 changes: 24 additions & 0 deletions test/components/label_test.rb
Expand Up @@ -166,13 +166,37 @@ def @controller.action_name; nil; end
assert_select 'label.datetime'
end

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
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'
with_label_for @validating_user, :status, :string
assert_select 'label.optional'
end

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
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'
Expand Down
8 changes: 8 additions & 0 deletions test/form_builder/general_test.rb
Expand Up @@ -38,6 +38,14 @@ def with_custom_form_for(object, *args, &block)
end
end

test 'builder should allow to skip input_type class' 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"
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
Expand Down

0 comments on commit 2dafe48

Please sign in to comment.