Skip to content

Commit

Permalink
Generates additional wrapper class based on object + attribute name,
Browse files Browse the repository at this point in the history
like `<div class='input string user_name'>`.

Closes #576
  • Loading branch information
lucasmazza committed Oct 7, 2012
1 parent 58a65e9 commit b5f2ea4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 2.1.0.dev

### enhancements
* Generates additional wrapper class based on object + attribute name.
([@lucasmazza](https://github/lucasmazza))
Closes [#576](https://github.com/plataformatec/simple_form/issues/576).

### bug fix
* Fix escaping issue in `label_input` component
([@allomov](https://github.com/allomov))
Expand Down
6 changes: 5 additions & 1 deletion lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def additional_classes
@additional_classes ||= [input_type, required_class, readonly_class, disabled_class].compact
end

def input_class
"#{object_name}_#{reflection_or_attribute_name}"

This comment has been minimized.

Copy link
@carlosantoniodasilva

carlosantoniodasilva Oct 7, 2012

Member

Remember that object_name may contain [] and index values for nested attributes, we already need to do some cleanup to do i18n lookup.

This comment has been minimized.

Copy link
@lucasmazza

lucasmazza Oct 8, 2012

Author Contributor

💩

end

private

def add_size!
Expand Down Expand Up @@ -113,7 +117,7 @@ def nested_boolean_style?

# Find reflection name when available, otherwise use attribute
def reflection_or_attribute_name
reflection ? reflection.name : attribute_name
@reflection_or_attribute_name ||= reflection ? reflection.name : attribute_name
end

# Retrieve options for the given namespace from the options hash
Expand Down
4 changes: 3 additions & 1 deletion lib/simple_form/wrappers/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def find(name)

def html_classes(input, options)
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
css += SimpleForm.additional_classes_for(:wrapper) { input.additional_classes }
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.compact
Expand Down
11 changes: 11 additions & 0 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class WrapperTest < ActionView::TestCase
assert_no_select 'div.field_with_errors'
end

test 'wrapper should add the attribute name class' do
with_form_for @user, :name
assert_select 'div.user_name'
end

test 'wrapper should add the association name class' do
with_form_for @user, :company
assert_select 'div.user_company'
end

test 'wrapper should add error class for attribute with errors' do
with_form_for @user, :name
assert_select 'div.field_with_errors'
Expand Down Expand Up @@ -72,6 +82,7 @@ class WrapperTest < ActionView::TestCase
assert_select 'form div.wrapper'
assert_no_select 'div.required'
assert_no_select 'div.string'
assert_no_select 'div.user_name'
end
end

Expand Down

0 comments on commit b5f2ea4

Please sign in to comment.