simple_form 2.1.3 → 3.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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -51
  3. data/README.md +195 -161
  4. data/lib/generators/simple_form/install_generator.rb +4 -4
  5. data/lib/generators/simple_form/templates/README +2 -2
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +16 -13
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +16 -16
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +3 -3
  9. data/lib/simple_form/action_view_extensions/builder.rb +1 -320
  10. data/lib/simple_form/action_view_extensions/builder.rb.orig +247 -0
  11. data/lib/simple_form/action_view_extensions/form_helper.rb +2 -9
  12. data/lib/simple_form/components/errors.rb +1 -7
  13. data/lib/simple_form/components/hints.rb +2 -7
  14. data/lib/simple_form/components/html5.rb +5 -2
  15. data/lib/simple_form/components/labels.rb +4 -4
  16. data/lib/simple_form/components/maxlength.rb +1 -8
  17. data/lib/simple_form/components/pattern.rb +2 -2
  18. data/lib/simple_form/components.rb +1 -1
  19. data/lib/simple_form/error_notification.rb +2 -2
  20. data/lib/simple_form/form_builder.rb +154 -50
  21. data/lib/simple_form/form_builder.rb.orig +486 -0
  22. data/lib/simple_form/helpers.rb +1 -1
  23. data/lib/simple_form/inputs/base.rb +7 -10
  24. data/lib/simple_form/inputs/block_input.rb +1 -1
  25. data/lib/simple_form/inputs/boolean_input.rb +6 -5
  26. data/lib/simple_form/inputs/collection_input.rb +7 -7
  27. data/lib/simple_form/inputs/date_time_input.rb +1 -1
  28. data/lib/simple_form/inputs/numeric_input.rb +0 -6
  29. data/lib/simple_form/inputs/password_input.rb +0 -1
  30. data/lib/simple_form/inputs/string_input.rb +0 -1
  31. data/lib/simple_form/railtie.rb +7 -0
  32. data/lib/simple_form/tags.rb +62 -0
  33. data/lib/simple_form/version.rb +1 -1
  34. data/lib/simple_form/version.rb.orig +7 -0
  35. data/lib/simple_form/wrappers/builder.rb +5 -29
  36. data/lib/simple_form/wrappers/many.rb +1 -1
  37. data/lib/simple_form/wrappers/root.rb +1 -1
  38. data/lib/simple_form/wrappers.rb +1 -1
  39. data/lib/simple_form.rb +43 -47
  40. data/test/action_view_extensions/builder_test.rb +78 -99
  41. data/test/action_view_extensions/form_helper_test.rb +25 -16
  42. data/test/components/label_test.rb +46 -46
  43. data/test/form_builder/association_test.rb +47 -29
  44. data/test/form_builder/button_test.rb +4 -4
  45. data/test/form_builder/error_notification_test.rb +8 -8
  46. data/test/form_builder/error_test.rb +18 -65
  47. data/test/form_builder/general_test.rb +62 -63
  48. data/test/form_builder/hint_test.rb +23 -29
  49. data/test/form_builder/input_field_test.rb +29 -12
  50. data/test/form_builder/label_test.rb +7 -17
  51. data/test/form_builder/wrapper_test.rb +21 -21
  52. data/test/inputs/boolean_input_test.rb +24 -24
  53. data/test/inputs/collection_check_boxes_input_test.rb +66 -55
  54. data/test/inputs/collection_radio_buttons_input_test.rb +81 -79
  55. data/test/inputs/collection_select_input_test.rb +76 -51
  56. data/test/inputs/datetime_input_test.rb +17 -11
  57. data/test/inputs/disabled_test.rb +10 -10
  58. data/test/inputs/discovery_test.rb +4 -4
  59. data/test/inputs/file_input_test.rb +1 -1
  60. data/test/inputs/general_test.rb +28 -12
  61. data/test/inputs/grouped_collection_select_input_test.rb +33 -20
  62. data/test/inputs/hidden_input_test.rb +3 -2
  63. data/test/inputs/numeric_input_test.rb +3 -3
  64. data/test/inputs/priority_input_test.rb +9 -3
  65. data/test/inputs/readonly_test.rb +12 -12
  66. data/test/inputs/required_test.rb +5 -5
  67. data/test/inputs/string_input_test.rb +15 -25
  68. data/test/inputs/text_input_test.rb +1 -1
  69. data/test/support/misc_helpers.rb +46 -24
  70. data/test/support/mock_controller.rb +6 -6
  71. data/test/support/models.rb +77 -62
  72. data/test/test_helper.rb +17 -34
  73. metadata +39 -22
  74. data/lib/simple_form/core_ext/hash.rb +0 -16
@@ -0,0 +1,247 @@
1
+ module SimpleForm
2
+ module ActionViewExtensions
3
+ # A collection of methods required by simple_form but added to rails default form.
4
+ # This means that you can use such methods outside simple_form context.
5
+ module Builder
6
+
7
+ # Wrapper for using SimpleForm inside a default rails form.
8
+ # Example:
9
+ #
10
+ # form_for @user do |f|
11
+ # f.simple_fields_for :posts do |posts_form|
12
+ # # Here you have all simple_form methods available
13
+ # posts_form.input :title
14
+ # end
15
+ # end
16
+ def simple_fields_for(*args, &block)
17
+ options = args.extract_options!
18
+ options[:wrapper] = self.options[:wrapper] if options[:wrapper].nil?
19
+ options[:defaults] ||= self.options[:defaults]
20
+
21
+ if self.class < ActionView::Helpers::FormBuilder
22
+ options[:builder] ||= self.class
23
+ else
24
+ options[:builder] ||= SimpleForm::FormBuilder
25
+ end
26
+ fields_for(*(args << options), &block)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module SimpleForm
33
+ module Tags
34
+ module CollectionExtensions
35
+ private
36
+
37
+ def render_collection
38
+ item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
39
+ item_wrapper_class = @options[:item_wrapper_class]
40
+
41
+ @collection.map do |item|
42
+ value = value_for_collection(item, @value_method)
43
+ text = value_for_collection(item, @text_method)
44
+ default_html_options = default_html_options_for_collection(item, value)
45
+
46
+ rendered_item = yield item, value, text, default_html_options
47
+
48
+ item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
49
+ end.join.html_safe
50
+ end
51
+
52
+ def wrap_rendered_collection(collection)
53
+ wrapper_tag = @options[:collection_wrapper_tag]
54
+
55
+ if wrapper_tag
56
+ wrapper_class = @options[:collection_wrapper_class]
57
+ @template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
58
+ else
59
+ collection
60
+ end
61
+ end
62
+ end
63
+
64
+ class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
65
+ include CollectionExtensions
66
+
67
+ def render
68
+ wrap_rendered_collection(super)
69
+ end
70
+
71
+ private
72
+
73
+ def render_component(builder)
74
+ builder.radio_button + builder.label(:class => "collection_radio_buttons")
75
+ end
76
+ end
77
+
78
+ class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes
79
+ include CollectionExtensions
80
+
81
+ def render
82
+ wrap_rendered_collection(super)
83
+ end
84
+
85
+ private
86
+
87
+ def render_component(builder)
88
+ builder.check_box + builder.label(:class => "collection_check_boxes")
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ module ActionView::Helpers
95
+ class FormBuilder
96
+ include SimpleForm::ActionViewExtensions::Builder
97
+ end
98
+
99
+ <<<<<<< HEAD
100
+ # Create a collection of radio inputs for the attribute. Basically this
101
+ # helper will create a radio input associated with a label for each
102
+ # text/value option in the collection, using value_method and text_method
103
+ # to convert these text/value. You can give a symbol or a proc to both
104
+ # value_method and text_method, that will be evaluated for each item in
105
+ # the collection.
106
+ #
107
+ # == Examples
108
+ #
109
+ # form_for @user do |f|
110
+ # f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
111
+ # end
112
+ #
113
+ # <input id="user_options_true" name="user[options]" type="radio" value="true" />
114
+ # <label class="collection_radio_buttons" for="user_options_true">Yes</label>
115
+ # <input id="user_options_false" name="user[options]" type="radio" value="false" />
116
+ # <label class="collection_radio_buttons" for="user_options_false">No</label>
117
+ #
118
+ # It is also possible to give a block that should generate the radio +
119
+ # label. To wrap the radio with the label, for instance:
120
+ #
121
+ # form_for @user do |f|
122
+ # f.collection_radio_buttons(
123
+ # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
124
+ # ) do |b|
125
+ # b.label { b.radio_button + b.text }
126
+ # end
127
+ # end
128
+ #
129
+ # == Options
130
+ #
131
+ # Collection radio accepts some extra options:
132
+ #
133
+ # * checked => the value that should be checked initially.
134
+ #
135
+ # * disabled => the value or values that should be disabled. Accepts a single
136
+ # item or an array of items.
137
+ #
138
+ # * collection_wrapper_tag => the tag to wrap the entire collection.
139
+ #
140
+ # * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
141
+ #
142
+ # * item_wrapper_tag => the tag to wrap each item in the collection.
143
+ #
144
+ # * item_wrapper_class => the CSS class to use for item_wrapper_tag
145
+ #
146
+ # * a block => to generate the label + radio or any other component.
147
+ def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
148
+ SimpleForm::Tags::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
149
+ =======
150
+ module FormOptionsHelper
151
+ # Override Rails options_from_collection_for_select to handle lambdas/procs in
152
+ # text and value methods, so it works the same way as collection_radio_buttons
153
+ # and collection_check_boxes in SimpleForm. If none of text/value methods is a
154
+ # callable object, then it just delegates back to original collection select.
155
+ # FIXME: remove when support only Rails 4.0 forward
156
+ # https://github.com/rails/rails/commit/9035324367526af0300477a58b6d3efc15d1a5a8
157
+ alias :original_options_from_collection_for_select :options_from_collection_for_select
158
+ def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
159
+ if value_method.respond_to?(:call) || text_method.respond_to?(:call)
160
+ collection = collection.map do |item|
161
+ value = value_for_collection(item, value_method)
162
+ text = value_for_collection(item, text_method)
163
+
164
+ [value, text]
165
+ end
166
+
167
+ value_method, text_method = :first, :last
168
+ selected = extract_selected_and_disabled_and_call_procs selected, collection
169
+ end
170
+
171
+ original_options_from_collection_for_select collection, value_method, text_method, selected
172
+ end
173
+
174
+ private
175
+
176
+ def extract_selected_and_disabled_and_call_procs(selected, collection)
177
+ selected, disabled = extract_selected_and_disabled selected
178
+ selected_disabled = { :selected => selected, :disabled => disabled }
179
+
180
+ selected_disabled.each do |key, check|
181
+ if check.is_a? Proc
182
+ values = collection.map { |option| option.first if check.call(option.first) }
183
+ selected_disabled[key] = values
184
+ end
185
+ end
186
+ end
187
+
188
+ def value_for_collection(item, value) #:nodoc:
189
+ value.respond_to?(:call) ? value.call(item) : item.send(value)
190
+ >>>>>>> master
191
+ end
192
+
193
+ # Creates a collection of check boxes for each item in the collection,
194
+ # associated with a clickable label. Use value_method and text_method to
195
+ # convert items in the collection for use as text/value in check boxes.
196
+ # You can give a symbol or a proc to both value_method and text_method,
197
+ # that will be evaluated for each item in the collection.
198
+ #
199
+ # == Examples
200
+ #
201
+ # form_for @user do |f|
202
+ # f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
203
+ # end
204
+ #
205
+ # <input name="user[options][]" type="hidden" value="" />
206
+ # <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
207
+ # <label class="collection_check_boxes" for="user_options_true">Yes</label>
208
+ # <input name="user[options][]" type="hidden" value="" />
209
+ # <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
210
+ # <label class="collection_check_boxes" for="user_options_false">No</label>
211
+ #
212
+ # It is also possible to give a block that should generate the check box +
213
+ # label. To wrap the check box with the label, for instance:
214
+ #
215
+ # form_for @user do |f|
216
+ # f.collection_check_boxes(
217
+ # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
218
+ # ) do |b|
219
+ # b.label { b.check_box + b.text }
220
+ # end
221
+ # end
222
+ #
223
+ # == Options
224
+ #
225
+ # Collection check box accepts some extra options:
226
+ #
227
+ # * checked => the value or values that should be checked initially. Accepts
228
+ # a single item or an array of items. It overrides existing associations.
229
+ #
230
+ # * disabled => the value or values that should be disabled. Accepts a single
231
+ # item or an array of items.
232
+ #
233
+ # * collection_wrapper_tag => the tag to wrap the entire collection.
234
+ #
235
+ # * collection_wrapper_class => the CSS class to use for collection_wrapper_tag. This option
236
+ # is ignored if the :collection_wrapper_tag option is blank.
237
+ #
238
+ # * item_wrapper_tag => the tag to wrap each item in the collection.
239
+ #
240
+ # * item_wrapper_class => the CSS class to use for item_wrapper_tag
241
+ #
242
+ # * a block => to generate the label + check box or any other component.
243
+ def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
244
+ SimpleForm::Tags::CollectionCheckBoxes.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
245
+ end
246
+ end
247
+ end
@@ -5,17 +5,10 @@ module SimpleForm
5
5
  # Example:
6
6
  #
7
7
  # simple_form_for @user do |f|
8
- # f.input :name, :hint => 'My hint'
8
+ # f.input :name, hint: 'My hint'
9
9
  # end
10
10
  #
11
11
  module FormHelper
12
- # Override the default ActiveRecordHelper behaviour of wrapping the input.
13
- # This gets taken care of semantically by adding an error class to the wrapper tag
14
- # containing the input.
15
- #
16
- FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
17
- html_tag
18
- end
19
12
 
20
13
  def simple_form_for(record, options={}, &block)
21
14
  options[:builder] ||= SimpleForm::FormBuilder
@@ -44,7 +37,7 @@ module SimpleForm
44
37
  def with_simple_form_field_error_proc
45
38
  default_field_error_proc = ::ActionView::Base.field_error_proc
46
39
  begin
47
- ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
40
+ ::ActionView::Base.field_error_proc = SimpleForm.field_error_proc
48
41
  yield
49
42
  ensure
50
43
  ::ActionView::Base.field_error_proc = default_field_error_proc
@@ -12,9 +12,7 @@ module SimpleForm
12
12
  protected
13
13
 
14
14
  def error_text
15
- text = has_error_in_options? ? options[:error] : errors.send(error_method)
16
-
17
- "#{html_escape(options[:error_prefix])} #{html_escape(text)}".lstrip.html_safe
15
+ "#{options[:error_prefix]} #{errors.send(error_method)}".lstrip.html_safe
18
16
  end
19
17
 
20
18
  def error_method
@@ -32,10 +30,6 @@ module SimpleForm
32
30
  def errors_on_association
33
31
  reflection ? object.errors[reflection.name] : []
34
32
  end
35
-
36
- def has_error_in_options?
37
- options[:error] && !options[:error].nil?
38
- end
39
33
  end
40
34
  end
41
35
  end
@@ -5,13 +5,8 @@ module SimpleForm
5
5
  def hint
6
6
  @hint ||= begin
7
7
  hint = options[:hint]
8
-
9
- if hint.is_a?(String)
10
- html_escape(hint)
11
- else
12
- content = translate(:hints)
13
- content.html_safe if content
14
- end
8
+ hint_content = hint.is_a?(String) ? hint : translate(:hints)
9
+ hint_content.html_safe if hint_content
15
10
  end
16
11
  end
17
12
 
@@ -7,7 +7,10 @@ module SimpleForm
7
7
 
8
8
  def html5
9
9
  @html5 = true
10
- input_html_options[:required] = true if has_required?
10
+ if has_required?
11
+ input_html_options[:required] = true
12
+ input_html_options[:'aria-required'] = true
13
+ end
11
14
  nil
12
15
  end
13
16
 
@@ -23,4 +26,4 @@ module SimpleForm
23
26
  end
24
27
  end
25
28
  end
26
- end
29
+ end
@@ -6,18 +6,18 @@ module SimpleForm
6
6
  module ClassMethods #:nodoc:
7
7
  def translate_required_html
8
8
  i18n_cache :translate_required_html do
9
- I18n.t(:"simple_form.required.html", :default =>
9
+ I18n.t(:"simple_form.required.html", default:
10
10
  %[<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr>]
11
11
  )
12
12
  end
13
13
  end
14
14
 
15
15
  def translate_required_text
16
- I18n.t(:"simple_form.required.text", :default => 'required')
16
+ I18n.t(:"simple_form.required.text", default: 'required')
17
17
  end
18
18
 
19
19
  def translate_required_mark
20
- I18n.t(:"simple_form.required.mark", :default => '*')
20
+ I18n.t(:"simple_form.required.mark", default: '*')
21
21
  end
22
22
  end
23
23
 
@@ -30,7 +30,7 @@ module SimpleForm
30
30
  end
31
31
 
32
32
  def label_text
33
- SimpleForm.label_text.call(html_escape(raw_label_text), required_label_text).strip.html_safe
33
+ SimpleForm.label_text.call(raw_label_text, required_label_text).strip.html_safe
34
34
  end
35
35
 
36
36
  def label_target
@@ -27,14 +27,7 @@ module SimpleForm
27
27
  end
28
28
 
29
29
  def has_tokenizer?(length_validator)
30
- tokenizer = length_validator.options[:tokenizer]
31
-
32
- # TODO: Remove this check when we drop Rails 3.0 support
33
- if ActiveModel::Validations::LengthValidator.const_defined?(:DEFAULT_TOKENIZER)
34
- tokenizer && tokenizer != ActiveModel::Validations::LengthValidator::DEFAULT_TOKENIZER
35
- else
36
- tokenizer
37
- end
30
+ length_validator.options[:tokenizer]
38
31
  end
39
32
  end
40
33
  end
@@ -13,8 +13,8 @@ module SimpleForm
13
13
  pattern = options[:pattern]
14
14
  if pattern.is_a?(String)
15
15
  pattern
16
- elsif pattern_validator = find_pattern_validator
17
- evaluate_format_validator_option(pattern_validator.options[:with]).source
16
+ elsif (pattern_validator = find_pattern_validator) && (with = pattern_validator.options[:with])
17
+ evaluate_format_validator_option(with).source
18
18
  end
19
19
  end
20
20
 
@@ -2,7 +2,7 @@ module SimpleForm
2
2
  # Components are a special type of helpers that can work on their own.
3
3
  # For example, by using a component, it will automatically change the
4
4
  # output under given circumstances without user input. For example,
5
- # the disabled helper always need a :disabled => true option given
5
+ # the disabled helper always need a disabled: true option given
6
6
  # to the input in order to be enabled. On the other hand, things like
7
7
  # hints can generate output automatically by doing I18n lookups.
8
8
  module Components
@@ -1,6 +1,6 @@
1
1
  module SimpleForm
2
2
  class ErrorNotification
3
- delegate :object, :object_name, :template, :to => :@builder
3
+ delegate :object, :object_name, :template, to: :@builder
4
4
 
5
5
  def initialize(builder, options)
6
6
  @builder = builder
@@ -42,7 +42,7 @@ module SimpleForm
42
42
  lookups << :"#{object_name}"
43
43
  lookups << :default_message
44
44
  lookups << "Please review the problems below:"
45
- I18n.t(lookups.shift, :scope => :"simple_form.error_notification", :default => lookups)
45
+ I18n.t(lookups.shift, scope: :"simple_form.error_notification", default: lookups)
46
46
  end
47
47
  end
48
48
  end