Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip valid_class check if no class defined #1657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/simple_form/wrappers/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ def html_classes(input, options)
css += SimpleForm.additional_classes_for(:wrapper) do
input.additional_classes + [input.input_class]
end
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 << (options[:wrapper_valid_class] || @defaults[:valid_class]) if input.valid?
css << html_class(:error_class, options) { input.has_errors? }
css << html_class(:hint_class, options) { input.has_hint? }
css << html_class(:valid_class, options) { input.valid? }
css.compact
end

def html_class(key, options)
css = (options[:"wrapper_#{key}"] || @defaults[key])
css if css && yield
end
end
end
end
9 changes: 9 additions & 0 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class WrapperTest < ActionView::TestCase
assert_no_select 'input.is-invalid'
end

test 'wrapper does not determine if valid class is needed when it is set to nil' do
Copy link
Collaborator

@feliperenan feliperenan Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not covering your changes - I put back the deleted code and it still pass. One way I can think right now in how to test it, would be with something like:

  test 'wrapper does not determine if valid class is needed when it is set to nil' do
    @user.instance_eval { undef errors }
    with_form_for @user, :name, wrapper: custom_wrapper_with_input_valid_class(valid_class: nil)

    assert_no_select 'div.field_without_errors'
  end

A better test would checking if input.valid? has not been called at all, but I don't know how to do it 🙈

def @user.name; raise BOOM; end
with_form_for @user, :name, as: :file, wrapper: custom_wrapper_with_input_valid_class(valid_class: nil)
assert_select 'div'
assert_select 'input'
assert_no_select 'div.field_with_errors'
assert_no_select 'input.is-invalid'
end

test 'wrapper adds hint class for attribute with a hint' do
with_form_for @user, :name, hint: 'hint'
assert_select 'div.field_with_hint'
Expand Down
4 changes: 2 additions & 2 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ def custom_wrapper_with_input_error_class
end
end

def custom_wrapper_with_input_valid_class
SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: :field_without_errors do |b|
def custom_wrapper_with_input_valid_class(valid_class: :field_without_errors)
SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: valid_class do |b|
b.use :label
b.use :input, class: 'inline-class', valid_class: 'is-valid'
end
Expand Down