Permalink
Browse files

Tidy up, add changelog

  • Loading branch information...
carlosantoniodasilva committed Feb 17, 2012
1 parent 2dafe48 commit f1edb15ac8d638e19f831d349b3bd7de50c3a8b3
View
@@ -33,6 +33,8 @@
* Change default generator templates to use .form-inputs and .form-actions classes in wrapper divs.
(the latter is the default in bootstrap, so this makes it easier to integrate).
* Field error now accepts HTML tags ([@edison](https://github.com/edison))
+ * Add `generate_additional_classes_for` config option to selectively disable extra
+ css classes for components - wrapper, label and input. ([krzyzak](https://github.com/krzyzak))
### deprecation
* Deprecate part of the old configuration API in favor of the wrapper API which allows you to customize your inputs
View
@@ -197,6 +197,10 @@ def self.deprecation_warn(message)
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] #{message}", caller
end
+ def self.additional_classes_for(component)
+ generate_additional_classes_for.include?(component) ? yield : []
+ end
+
# Default way to setup SimpleForm. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values.
def self.setup
@@ -38,10 +38,14 @@ def label_target
end
def label_html_options
- label_html_classes = SimpleForm.generate_additional_classes_for.include?(:label) ? [input_type, required_class, SimpleForm.label_class].compact : []
+ label_html_classes = SimpleForm.additional_classes_for(: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)
+ if options.key?(:input_html) && options[:input_html].key?(:id)
+ label_options[:for] = options[:input_html][:id]
+ end
label_options
end
@@ -59,7 +59,9 @@ 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 = SimpleForm.generate_additional_classes_for.include?(:input) ? [input_type, required_class, readonly_class, disabled_class].compact : []
+ @html_classes = SimpleForm.additional_classes_for(: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|
@@ -101,7 +103,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.delete_if{|key, value| value.blank?}
+ html_options
end
# Lookup translations for the given namespace using I18n, based on object name,
@@ -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.generate_additional_classes_for.include?(:wrapper)
+ css += SimpleForm.additional_classes_for(:wrapper) { input.html_classes }
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
@@ -66,6 +66,15 @@ class WrapperTest < ActionView::TestCase
assert_select 'form div.wrapper.required.string'
end
+ test 'wrapper should skip additional classes when configured' do
+ swap SimpleForm, :generate_additional_classes_for => [:input, :label] do
+ with_form_for @user, :name, :wrapper_class => :wrapper
+ assert_select 'form div.wrapper'
+ assert_no_select 'div.required'
+ assert_no_select 'div.string'
+ end
+ end
+
# Custom wrapper test
test 'custom wrappers works' do

0 comments on commit f1edb15

Please sign in to comment.