simple_form 2.0.0.rc → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of simple_form might be problematic. Click here for more details.

Files changed (45) hide show
  1. data/CHANGELOG.md +16 -6
  2. data/README.md +18 -19
  3. data/lib/generators/simple_form/install_generator.rb +1 -1
  4. data/lib/generators/simple_form/templates/_form.html.erb +2 -2
  5. data/lib/generators/simple_form/templates/_form.html.haml +2 -2
  6. data/lib/generators/simple_form/templates/_form.html.slim +4 -4
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +23 -20
  8. data/lib/simple_form.rb +13 -9
  9. data/lib/simple_form/action_view_extensions/builder.rb +64 -22
  10. data/lib/simple_form/action_view_extensions/form_helper.rb +1 -1
  11. data/lib/simple_form/components/errors.rb +1 -1
  12. data/lib/simple_form/components/labels.rb +8 -2
  13. data/lib/simple_form/error_notification.rb +1 -2
  14. data/lib/simple_form/form_builder.rb +19 -6
  15. data/lib/simple_form/helpers/required.rb +5 -3
  16. data/lib/simple_form/inputs/base.rb +4 -1
  17. data/lib/simple_form/inputs/boolean_input.rb +4 -1
  18. data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -2
  19. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +3 -5
  20. data/lib/simple_form/inputs/date_time_input.rb +8 -4
  21. data/lib/simple_form/inputs/grouped_collection_select_input.rb +1 -1
  22. data/lib/simple_form/version.rb +1 -1
  23. data/lib/simple_form/wrappers/builder.rb +46 -18
  24. data/lib/simple_form/wrappers/many.rb +1 -0
  25. data/lib/simple_form/wrappers/root.rb +1 -1
  26. data/test/action_view_extensions/builder_test.rb +100 -12
  27. data/test/action_view_extensions/form_helper_test.rb +13 -13
  28. data/test/components/label_test.rb +24 -0
  29. data/test/form_builder/association_test.rb +10 -0
  30. data/test/form_builder/button_test.rb +19 -0
  31. data/test/form_builder/error_notification_test.rb +16 -0
  32. data/test/form_builder/error_test.rb +21 -1
  33. data/test/form_builder/general_test.rb +8 -0
  34. data/test/form_builder/hint_test.rb +10 -2
  35. data/test/form_builder/input_field_test.rb +13 -1
  36. data/test/form_builder/label_test.rb +14 -0
  37. data/test/form_builder/wrapper_test.rb +10 -1
  38. data/test/inputs/boolean_input_test.rb +10 -0
  39. data/test/inputs/datetime_input_test.rb +20 -6
  40. data/test/inputs/grouped_collection_select_input_test.rb +9 -0
  41. data/test/inputs/priority_input_test.rb +1 -1
  42. data/test/inputs/string_input_test.rb +1 -1
  43. data/test/support/misc_helpers.rb +11 -12
  44. data/test/test_helper.rb +6 -4
  45. metadata +13 -10
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ## 2.0.0.rc
1
+ ## 2.0.0
2
2
 
3
3
  ### enhancements
4
4
  * Add `button_class` configuration to change the class of buttons. ([@sryche](https://github.com/sryche))
@@ -9,9 +9,6 @@
9
9
  * `simple_form_for` allows default options for its inputs `:defaults => {}`.
10
10
  * Add `readonly` as option of input method. ([@Untainted123](https://github.com/Untainted123))
11
11
  * `simple_fields_for` for inherits wrapper option form the form builder. ([@nashby](https://github.com/nashby))
12
- * Deprecated part of the old configuration API in favor of the wrapper API which allows you to customize your inputs
13
- in a more flexible way. See [this guide](https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0)
14
- to know how upgrade.
15
12
  * Use action prefix in the form css class. Closes [#360](https://github.com/plataformatec/simple_form/issues/360).
16
13
  This is not backward compatible with the previous versions of SimpleForm.
17
14
  For more informations see [this comment](https://github.com/plataformatec/simple_form/issues/360#issuecomment-3000780).
@@ -25,20 +22,30 @@
25
22
  * Add `:boolean_style` config to change how check boxes and radios will be displayed.
26
23
  Options are `:inline = input + label` (default) and `:nested = label > input`.
27
24
  * Add possibility to give a block to `collection_radio` and `collection_check_boxes`,
28
- to generate custom label and input structure. It is used internally with the :nested
29
- option for `:boolean_style`, and is useful to allow some more customization if required.
25
+ yielding a custom builder to generate custom label and input structure. It
26
+ is used internally with the :nested option for `:boolean_style`, and is useful
27
+ to allow some more customization if required.
30
28
  * Do not generate hidden check box field when using nested boolean style, as it is considered
31
29
  invalid markup in HTML5. This will work by default in Rails > 3.2.1 (not released at this time),
32
30
  and is backported inside SimpleForm builder extensions.
33
31
  More info in [#215](https://github.com/plataformatec/simple_form/issues/215)
34
32
  * Add `item_wrapper_class` configuration option for collection radio buttons / check boxes inputs.
33
+ * Change default generator templates to use .form-inputs and .form-actions classes in wrapper divs.
34
+ (the latter is the default in bootstrap, so this makes it easier to integrate).
35
+ * Field error now accepts HTML tags ([@edison](https://github.com/edison))
36
+ * Add `generate_additional_classes_for` config option to selectively disable extra
37
+ css classes for components - wrapper, label and input. ([krzyzak](https://github.com/krzyzak))
35
38
 
36
39
  ### deprecation
40
+ * Deprecate part of the old configuration API in favor of the wrapper API which allows you to customize your inputs
41
+ in a more flexible way. See [this guide](https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0)
42
+ to know how upgrade.
37
43
  * Deprecate the `translate` configuration in favor of `translate_labels`
38
44
  * Deprecate the `html5` configuration in favor of a new `html5` component
39
45
  * Deprecate `:radio` input type in favor of `:radio_buttons`
40
46
  * Deprecate `collection_radio` form helper in favor of `collection_radio_buttons`
41
47
  (the label class has changed as well)
48
+ * Remove `error_notification_id` configuration
42
49
 
43
50
  ### bug fix
44
51
  * Fix i18n lookup with attributes with same name of models.
@@ -54,6 +61,9 @@
54
61
  Closes [#341](https://github.com/plataformatec/simple_form/issues/341). ([@nashby](https://github.com/nashby))
55
62
  * Move default attribute translations out of root - use "defaults" key instead
56
63
  Closes [#384](https://github.com/plataformatec/simple_form/issues/384). ([@fringd](https://github.com/fringd))
64
+ * Fix label to datetime inputs to point to first select. ([@georgehemmings](https://github.com/georgehemmings))
65
+ * Fix usage of f.button :button with Rails 3.2.
66
+ Closes [#449](https://github.com/plataformatec/simple_form/issues/449).
57
67
 
58
68
  ## 1.5.2
59
69
 
data/README.md CHANGED
@@ -2,11 +2,15 @@
2
2
  [![Build Status](https://secure.travis-ci.org/plataformatec/simple_form.png)](http://travis-ci.org/plataformatec/simple_form)
3
3
 
4
4
  **SimpleForm** aims to be as flexible as possible while helping you with powerful components to create
5
- your forms. The basic goal of simple form is to not touch your way of defining the layout, letting
5
+ your forms. The basic goal of SimpleForm is to not touch your way of defining the layout, letting
6
6
  you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
7
7
  which we are thankful for and should make you feel right at home.
8
8
 
9
- INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/).
9
+ INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
10
+ and refers to **SimpleForm** 2.0. If you are using **SimpleForm** in the versions 1.x, you should
11
+ check this branch:
12
+
13
+ https://github.com/plataformatec/simple_form/tree/v1.5
10
14
 
11
15
  ## Installation
12
16
 
@@ -27,11 +31,6 @@ Also, if you want to use the country select, you will need the
27
31
 
28
32
  `gem 'country_select'`
29
33
 
30
- And you are ready to go. Since this branch aims Rails 3 support, if you want to use it with
31
- Rails 2.3 you should check this branch:
32
-
33
- `http://github.com/plataformatec/simple_form/tree/v1.0`
34
-
35
34
  ## Configuration
36
35
 
37
36
  **SimpleForm** has several configuration options. You can read and change them in the initializer
@@ -73,8 +72,8 @@ config.wrappers :tag => :div, :class => :input,
73
72
 
74
73
  # Form components
75
74
  b.use :label_input
76
- b.use :hint, :tag => :span, :class => :hint
77
- b.use :error, :tag => :span, :class => :error
75
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
76
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
78
77
  end
79
78
  ```
80
79
 
@@ -89,9 +88,9 @@ You can create new _Form components_ using the wrappers API as in the following
89
88
  config.wrappers do |b|
90
89
  b.use :placeholder
91
90
  b.use :label_input
92
- b.use :tag => :div, :class => 'separator' do |component|
93
- component.use :hint, :tag => :span, :class => :hint
94
- component.use :error, :tag => :span, :class => :error
91
+ b.wrapper :tag => :div, :class => 'separator' do |component|
92
+ component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
93
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
95
94
  end
96
95
  end
97
96
  ```
@@ -104,9 +103,9 @@ If you want to customize the custom _Form components_ on demand you can give it
104
103
  config.wrappers do |b|
105
104
  b.use :placeholder
106
105
  b.use :label_input
107
- b.use :my_wrapper, :tag => :div, :class => 'separator' do |component|
108
- component.use :hint, :tag => :span, :class => :hint
109
- component.use :error, :tag => :span, :class => :error
106
+ b.wrapper :my_wrapper, :tag => :div, :class => 'separator' do |component|
107
+ component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
108
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
110
109
  end
111
110
  end
112
111
  ```
@@ -156,9 +155,9 @@ default values to `false` or use the `optional` method. Is preferible to use the
156
155
  config.wrappers :placeholder => false do |b|
157
156
  b.use :placeholder
158
157
  b.use :label_input
159
- b.use :tag => :div, :class => 'separator' do |component|
160
- component.optional :hint, :tag => :span, :class => :hint
161
- component.use :error, :tag => :span, :class => :error
158
+ b.wrapper :tag => :div, :class => 'separator' do |component|
159
+ component.optional :hint, :wrap_with => { :tag => :span, :class => :hint }
160
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
162
161
  end
163
162
  end
164
163
  ```
@@ -486,7 +485,7 @@ on `simple_form_for` helper. They are listed below.
486
485
 
487
486
  ### Simple Fields For
488
487
 
489
- Wrapper to use simple form inside a default rails form
488
+ Wrapper to use SimpleForm inside a default rails form
490
489
 
491
490
  ```ruby
492
491
  form_for @user do |f|
@@ -8,7 +8,7 @@ module SimpleForm
8
8
 
9
9
  def info_bootstrap
10
10
  return if options.bootstrap?
11
- puts "Simple Form 2 supports Twitter bootstrap. In case you want to " \
11
+ puts "SimpleForm 2 supports Twitter bootstrap. In case you want to " \
12
12
  "generate bootstrap configuration, please re-run this " \
13
13
  "generator passing --bootstrap as option."
14
14
  end
@@ -1,13 +1,13 @@
1
1
  <%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
2
2
  <%%= f.error_notification %>
3
3
 
4
- <div class="inputs">
4
+ <div class="form-inputs">
5
5
  <%- attributes.each do |attribute| -%>
6
6
  <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
7
7
  <%- end -%>
8
8
  </div>
9
9
 
10
- <div class="actions">
10
+ <div class="form-actions">
11
11
  <%%= f.button :submit %>
12
12
  </div>
13
13
  <%% end %>
@@ -1,10 +1,10 @@
1
1
  = simple_form_for(@<%= singular_table_name %>) do |f|
2
2
  = f.error_notification
3
3
 
4
- .inputs
4
+ .form-inputs
5
5
  <%- attributes.each do |attribute| -%>
6
6
  = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
7
7
  <%- end -%>
8
8
 
9
- .actions
9
+ .form-actions
10
10
  = f.button :submit
@@ -1,10 +1,10 @@
1
1
  = simple_form_for(@<%= singular_table_name %>) do |f|
2
2
  = f.error_notification
3
3
 
4
- .inputs
5
- | <%- attributes.each do |attribute| -%>
4
+ .form-inputs
5
+ <%- attributes.each do |attribute| -%>
6
6
  = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
7
- | <%- end -%>
7
+ <%- end -%>
8
8
 
9
- .actions
9
+ .form-actions
10
10
  = f.button :submit
@@ -41,41 +41,41 @@ SimpleForm.setup do |config|
41
41
 
42
42
  ## Inputs
43
43
  b.use :label_input
44
- b.use :hint, :tag => :span, :class => :hint
45
- b.use :error, :tag => :span, :class => :error
44
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
45
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
46
46
  end
47
47
  <% if options.bootstrap? %>
48
48
  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
49
49
  b.use :placeholder
50
- b.use :label, :class => 'control-label'
51
- b.use :tag => 'div', :class => 'controls' do |ba|
50
+ b.use :label
51
+ b.wrapper :tag => 'div', :class => 'controls' do |ba|
52
52
  ba.use :input
53
- ba.use :error, :tag => 'span', :class => 'help-inline'
54
- ba.use :hint, :tag => 'p', :class => 'help-block'
53
+ ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
54
+ ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
55
55
  end
56
56
  end
57
57
 
58
58
  config.wrappers :prepend, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
59
59
  b.use :placeholder
60
- b.use :label, :class => 'control-label'
61
- b.use :hint, :tag => 'span', :class => 'help-block'
62
- b.use :tag => 'div', :class => 'controls' do |input|
63
- input.use :tag => 'div', :class => 'input-prepend' do |prepend|
60
+ b.use :label
61
+ b.wrapper :tag => 'div', :class => 'controls' do |input|
62
+ input.wrapper :tag => 'div', :class => 'input-prepend' do |prepend|
64
63
  prepend.use :input
65
64
  end
66
- input.use :error, :tag => 'span', :class => 'help-inline'
65
+ input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
66
+ input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
67
67
  end
68
68
  end
69
69
 
70
70
  config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
71
71
  b.use :placeholder
72
- b.use :label, :class => 'control-label'
73
- b.use :hint, :tag => 'span', :class => 'help-block'
74
- b.use :tag => 'div', :class => 'controls' do |input|
75
- input.use :tag => 'div', :class => 'input-append' do |append|
72
+ b.use :label
73
+ b.wrapper :tag => 'div', :class => 'controls' do |input|
74
+ input.wrapper :tag => 'div', :class => 'input-append' do |append|
76
75
  append.use :input
77
76
  end
78
- input.use :error, :tag => 'span', :class => 'help-inline'
77
+ input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
78
+ input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
79
79
  end
80
80
  end
81
81
 
@@ -101,10 +101,10 @@ SimpleForm.setup do |config|
101
101
  # config.error_method = :first
102
102
 
103
103
  # Default tag used for error notification helper.
104
- # config.error_notification_tag = :p
104
+ config.error_notification_tag = :div
105
105
 
106
106
  # CSS class to add for error notification helper.
107
- # config.error_notification_class = :error_notification
107
+ config.error_notification_class = 'alert alert-error'
108
108
 
109
109
  # ID to add for error notification helper.
110
110
  # config.error_notification_id = nil
@@ -133,11 +133,14 @@ SimpleForm.setup do |config|
133
133
  # config.label_text = lambda { |label, required| "#{required} #{label}" }
134
134
 
135
135
  # You can define the class to use on all labels. Default is nil.
136
- # config.label_class = nil
136
+ config.label_class = 'control-label'
137
137
 
138
138
  # You can define the class to use on all forms. Default is simple_form.
139
139
  # config.form_class = :simple_form
140
140
 
141
+ # You can define which elements should obtain additional classes
142
+ # config.generate_additional_classes_for = [:wrapper, :label, :input]
143
+
141
144
  # Whether attributes are required by default (or not). Default is true.
142
145
  # config.required_by_default = true
143
146
 
@@ -168,6 +171,6 @@ SimpleForm.setup do |config|
168
171
  # Automatically discover new inputs in Rails' autoload path.
169
172
  # config.inputs_discovery = true
170
173
 
171
- # Cache simple form inputs discovery
174
+ # Cache SimpleForm inputs discovery
172
175
  # config.cache_discovery = !Rails.env.development?
173
176
  end
data/lib/simple_form.rb CHANGED
@@ -29,10 +29,6 @@ module SimpleForm
29
29
  mattr_accessor :error_notification_class
30
30
  @@error_notification_class = :error_notification
31
31
 
32
- # ID to add for error notification helper.
33
- mattr_accessor :error_notification_id
34
- @@error_notification_id = nil
35
-
36
32
  # Series of attemps to detect a default label method for collection.
37
33
  mattr_accessor :collection_label_methods
38
34
  @@collection_label_methods = [ :to_label, :name, :title, :to_s ]
@@ -77,6 +73,10 @@ module SimpleForm
77
73
  mattr_accessor :form_class
78
74
  @@form_class = :simple_form
79
75
 
76
+ # You can define which elements should obtain additional classes
77
+ mattr_accessor :generate_additional_classes_for
78
+ @@generate_additional_classes_for = [:wrapper, :label, :input]
79
+
80
80
  # Whether attributes are required by default (or not).
81
81
  mattr_accessor :required_by_default
82
82
  @@required_by_default = true
@@ -116,7 +116,7 @@ module SimpleForm
116
116
  mattr_accessor :inputs_discovery
117
117
  @@inputs_discovery = true
118
118
 
119
- # Cache simple form inputs discovery
119
+ # Cache SimpleForm inputs discovery
120
120
  mattr_accessor :cache_discovery
121
121
  @@cache_discovery = defined?(Rails) && !Rails.env.development?
122
122
 
@@ -169,13 +169,13 @@ module SimpleForm
169
169
  b.optional :readonly
170
170
 
171
171
  b.use :label_input
172
- b.use :hint, :tag => :span, :class => :hint
173
- b.use :error, :tag => :span, :class => :error
172
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
173
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
174
174
  end
175
175
 
176
176
  ## SETUP
177
177
 
178
- DEPRECATED = %w(hint_tag hint_class error_tag error_class wrapper_tag wrapper_class wrapper_error_class components html5)
178
+ DEPRECATED = %w(hint_tag hint_class error_tag error_class error_notification_id wrapper_tag wrapper_class wrapper_error_class components html5)
179
179
  @@deprecated = []
180
180
 
181
181
  DEPRECATED.each do |method|
@@ -197,13 +197,17 @@ module SimpleForm
197
197
  ActiveSupport::Deprecation.warn "[SIMPLE_FORM] #{message}", caller
198
198
  end
199
199
 
200
+ def self.additional_classes_for(component)
201
+ generate_additional_classes_for.include?(component) ? yield : []
202
+ end
203
+
200
204
  # Default way to setup SimpleForm. Run rails generate simple_form:install
201
205
  # to create a fresh initializer with all configuration values.
202
206
  def self.setup
203
207
  yield self
204
208
 
205
209
  unless @@deprecated.empty?
206
- raise "[SIMPLE FORM] Your simple form initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
210
+ raise "[SIMPLE FORM] Your SimpleForm initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
207
211
  "Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
208
212
  "Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
209
213
  end
@@ -1,9 +1,46 @@
1
1
  module SimpleForm
2
2
  module ActionViewExtensions
3
+ # Base builder to handle each instance of a collection of radio buttons / check boxes.
4
+ # Based on (at this time upcoming) Rails 4 collection builders.
5
+ class BuilderBase #:nodoc:
6
+ attr_reader :object, :text, :value
7
+
8
+ def initialize(template_object, object_name, method_name, object,
9
+ sanitized_attribute_name, text, value, input_html_options)
10
+ @template_object = template_object
11
+ @object_name = object_name
12
+ @method_name = method_name
13
+ @object = object
14
+ @sanitized_attribute_name = sanitized_attribute_name
15
+ @text = text
16
+ @value = value
17
+ @input_html_options = input_html_options
18
+ end
19
+
20
+ def label(label_html_options={}, &block)
21
+ @template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
22
+ end
23
+ end
24
+
25
+ # Handles generating an instance of radio + label for collection_radio_buttons.
26
+ class RadioButtonBuilder < BuilderBase #:nodoc:
27
+ def radio_button(extra_html_options={})
28
+ html_options = extra_html_options.merge(@input_html_options)
29
+ @template_object.radio_button(@object_name, @method_name, @value, html_options)
30
+ end
31
+ end
32
+
33
+ # Handles generating an instance of check box + label for collection_check_boxes.
34
+ class CheckBoxBuilder < BuilderBase #:nodoc:
35
+ def check_box(extra_html_options={})
36
+ html_options = extra_html_options.merge(@input_html_options)
37
+ @template_object.check_box(@object_name, @method_name, html_options, @value, nil)
38
+ end
39
+ end
40
+
3
41
  # A collection of methods required by simple_form but added to rails default form.
4
42
  # This means that you can use such methods outside simple_form context.
5
43
  module Builder
6
-
7
44
  # Create a collection of radio inputs for the attribute. Basically this
8
45
  # helper will create a radio input associated with a label for each
9
46
  # text/value option in the collection, using value_method and text_method
@@ -28,8 +65,8 @@ module SimpleForm
28
65
  # form_for @user do |f|
29
66
  # f.collection_radio_buttons(
30
67
  # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
31
- # ) do |label_for, text, value, html_options|
32
- # f.label(label_for) { f.radio_button(attribute, value, html_options) + text }
68
+ # ) do |b|
69
+ # b.label { b.radio_button + b.text }
33
70
  # end
34
71
  # end
35
72
  #
@@ -55,12 +92,13 @@ module SimpleForm
55
92
  def collection_radio_buttons(attribute, collection, value_method, text_method, options={}, html_options={})
56
93
  rendered_collection = render_collection(
57
94
  collection, value_method, text_method, options, html_options
58
- ) do |value, text, default_html_options|
95
+ ) do |item, value, text, default_html_options|
96
+ builder = instantiate_builder(RadioButtonBuilder, attribute, item, value, text, default_html_options)
97
+
59
98
  if block_given?
60
- yield sanitize_attribute_name(attribute, value), text, value, default_html_options
99
+ yield builder
61
100
  else
62
- radio_button(attribute, value, default_html_options) +
63
- label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio_buttons")
101
+ builder.radio_button + builder.label(:class => "collection_radio_buttons")
64
102
  end
65
103
  end
66
104
 
@@ -99,8 +137,8 @@ module SimpleForm
99
137
  # form_for @user do |f|
100
138
  # f.collection_check_boxes(
101
139
  # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
102
- # ) do |label_for, text, value, html_options|
103
- # f.label(label_for) { f.check_box(attribute, html_options, value, '') + text }
140
+ # ) do |b|
141
+ # b.label { b.check_box + b.text }
104
142
  # end
105
143
  # end
106
144
  #
@@ -127,14 +165,14 @@ module SimpleForm
127
165
  def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
128
166
  rendered_collection = render_collection(
129
167
  collection, value_method, text_method, options, html_options
130
- ) do |value, text, default_html_options|
168
+ ) do |item, value, text, default_html_options|
131
169
  default_html_options[:multiple] = true
170
+ builder = instantiate_builder(CheckBoxBuilder, attribute, item, value, text, default_html_options)
132
171
 
133
172
  if block_given?
134
- yield sanitize_attribute_name(attribute, value), text, value, default_html_options
173
+ yield builder
135
174
  else
136
- check_box(attribute, default_html_options, value, nil) +
137
- label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes")
175
+ builder.check_box + builder.label(:class => "collection_check_boxes")
138
176
  end
139
177
  end
140
178
 
@@ -145,7 +183,7 @@ module SimpleForm
145
183
  wrap_rendered_collection(rendered_collection + hidden, options)
146
184
  end
147
185
 
148
- # Wrapper for using simple form inside a default rails form.
186
+ # Wrapper for using SimpleForm inside a default rails form.
149
187
  # Example:
150
188
  #
151
189
  # form_for @user do |f|
@@ -168,19 +206,23 @@ module SimpleForm
168
206
 
169
207
  private
170
208
 
209
+ def instantiate_builder(builder_class, attribute, item, value, text, html_options)
210
+ builder_class.new(@template, object_name, attribute, item,
211
+ sanitize_attribute_name(attribute, value), text, value, html_options)
212
+ end
213
+
171
214
  # Generate default options for collection helpers, such as :checked and
172
215
  # :disabled.
173
216
  def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
174
217
  html_options = html_options.dup
175
218
 
176
219
  [:checked, :selected, :disabled].each do |option|
177
- next unless options[option]
178
-
220
+ next unless current_option = options[option]
179
221
 
180
- accept = if options[option].respond_to?(:call)
181
- options[option].call(item)
222
+ accept = if current_option.respond_to?(:call)
223
+ current_option.call(item)
182
224
  else
183
- Array(options[option]).include?(value)
225
+ Array(current_option).include?(value)
184
226
  end
185
227
 
186
228
  if accept
@@ -206,7 +248,7 @@ module SimpleForm
206
248
  text = value_for_collection(item, text_method)
207
249
  default_html_options = default_html_options_for_collection(item, value, options, html_options)
208
250
 
209
- rendered_item = yield value, text, default_html_options
251
+ rendered_item = yield item, value, text, default_html_options
210
252
 
211
253
  item_wrapper_tag ? @template.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
212
254
  end.join.html_safe
@@ -288,9 +330,9 @@ module ActionView::Helpers
288
330
  else
289
331
  add_default_name_and_id(options)
290
332
  end
291
- hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : ""
333
+ hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : "".html_safe
292
334
  checkbox = tag("input", options)
293
- (hidden + checkbox).html_safe
335
+ hidden + checkbox
294
336
  end
295
337
  end
296
338
  end