simple_form 2.0.1 → 2.0.2

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.

data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## 2.0.2
2
+
3
+ ### enhancements
4
+ * Add `:inline_label` option to nested booleans to display text inline with checkbox.
5
+ If the value is `true` it uses the default label text. ([@ehoch](https://github.com/ehoch))
6
+ * Add html support for hints. ([@findrails](https://github.com/findrails))
7
+
8
+ ### bug fix
9
+ * Fix `min_max` component to not output maximum value. ([@julian7](https://github.com/julian7)).
10
+ Closes [#483](https://github.com/plataformatec/simple_form/issues/483)
11
+ * Remove leading and trailing whitespace from `label_text`.
12
+ Closes [#492](https://github.com/plataformatec/simple_form/issues/492)
13
+ * Fix checked radio button issue when value is false. ([@georgehemmings](https://github.com/georgehemmings)).
14
+ * Propagate defaults options to nested forms.
15
+ Closes [#553](https://github.com/plataformatec/simple_form/issues/533).
16
+ ([@nashby](https://github.com/nashby))
17
+ * Fix limit and maxlength with decimal points. ([@shwoodard](https://github.com/shwoodard))
18
+ * Fix issue when html are duplicated.
19
+ Closes [#488](https://github.com/plataformatec/simple_form/issues/488).
20
+ ([@ebonical](https://github.com/ebonical))
21
+
1
22
  ## 2.0.1
2
23
 
3
24
  ### bug fix
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 PlataformaTec http://blog.plataformatec.com.br/
1
+ Copyright (c) 2012 Plataformatec http://plataformatec.com.br/
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -31,13 +31,6 @@ Also, if you want to use the country select, you will need the
31
31
 
32
32
  `gem 'country_select'`
33
33
 
34
- ## Configuration
35
-
36
- **SimpleForm** has several configuration options. You can read and change them in the initializer
37
- created by **SimpleForm**, so if you haven't executed the command below yet, please do:
38
-
39
- `rails generate simple_form:install`
40
-
41
34
  ### Twitter Bootstrap
42
35
 
43
36
  **SimpleForm** 2.0 can be easily integrated to the [Twitter Bootstrap](http://twitter.github.com/bootstrap).
@@ -54,117 +47,6 @@ For more information see the generator output, our
54
47
 
55
48
  **NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0 or higher.
56
49
 
57
- ### The wrappers API
58
-
59
- With **SimpleForm** you can configure how your components will be rendered using the wrappers API.
60
- The syntax looks like this:
61
-
62
- ```ruby
63
- config.wrappers :tag => :div, :class => :input,
64
- :error_class => :field_with_errors do |b|
65
-
66
- # Form extensions
67
- b.use :html5
68
- b.optional :pattern
69
- b.use :maxlength
70
- b.use :placeholder
71
- b.use :readonly
72
-
73
- # Form components
74
- b.use :label_input
75
- b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
76
- b.use :error, :wrap_with => { :tag => :span, :class => :error }
77
- end
78
- ```
79
-
80
- The _Form components_ will generate the form tags like labels, inputs, hints or errors contents.
81
-
82
- The _Form extensions_ are used to generate some attributes or perform some lookups on the model to
83
- add extra information to your components.
84
-
85
- You can create new _Form components_ using the wrappers API as in the following example:
86
-
87
- ```ruby
88
- config.wrappers do |b|
89
- b.use :placeholder
90
- b.use :label_input
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 }
94
- end
95
- end
96
- ```
97
-
98
- this will wrap the hint and error components within a `div` tag using the class `'separator'`.
99
-
100
- If you want to customize the custom _Form components_ on demand you can give it a name like this:
101
-
102
- ```ruby
103
- config.wrappers do |b|
104
- b.use :placeholder
105
- b.use :label_input
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 }
109
- end
110
- end
111
- ```
112
-
113
- and now you can pass options to your `input` calls to customize the `:my_wrapper` _Form component_.
114
-
115
- ```ruby
116
- # Completely turns off the custom wrapper
117
- f.input :name, :my_wrapper => false
118
-
119
- # Configure the html
120
- f.input :name, :my_wrapper_html => { :id => 'special_id' }
121
-
122
- # Configure the tag
123
- f.input :name, :my_wrapper_tag => :p
124
- ```
125
-
126
- You can also define more than one wrapper and pick one to render in a specific form or input.
127
- To define another wrapper you have to give it a name, as the follow:
128
-
129
- ```ruby
130
- config.wrappers :small do |b|
131
- b.use :placeholder
132
- b.use :label_input
133
- end
134
- ```
135
-
136
- and use it in this way:
137
-
138
- ```ruby
139
- # Specifying to whole form
140
- simple_form_for @user, :wrapper => :small do |f|
141
- f.input :name
142
- end
143
-
144
- # Specifying to one input
145
- simple_form_for @user do |f|
146
- f.input :name, :wrapper => :small
147
- end
148
- ```
149
-
150
- **SimpleForm** also allows you to use optional elements. For instance, let's suppose you want to use
151
- hints or placeholders, but you don't want them to be generated automatically. You can set their
152
- default values to `false` or use the `optional` method. Is preferible to use the `optional` syntax:
153
-
154
- ```ruby
155
- config.wrappers :placeholder => false do |b|
156
- b.use :placeholder
157
- b.use :label_input
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 }
161
- end
162
- end
163
- ```
164
-
165
- By setting it as `optional`, a hint will only be generated when `:hint => true` is explicitly used.
166
- The same for placehold.
167
-
168
50
  ## Usage
169
51
 
170
52
  **SimpleForm** was designed to be customized as you need to. Basically it's a stack of components that
@@ -383,26 +265,7 @@ drop down then you may use the `:collection` option:
383
265
  f.input :shipping_country, :priority => [ "Brazil" ], :collection => [ "Australia", "Brazil", "New Zealand"]
384
266
  ```
385
267
 
386
- ### Wrapper
387
-
388
- **SimpleForm** allows you to add a wrapper which contains the label, error, hint and input.
389
- The first step is to configure a wrapper tag:
390
-
391
- ```ruby
392
- SimpleForm.wrapper_tag = :p
393
- ```
394
-
395
- And now, you no longer need to wrap your `f.input` calls anymore:
396
-
397
- ```erb
398
- <%= simple_form_for @user do |f| %>
399
- <%= f.input :username %>
400
- <%= f.input :password %>
401
- <%= f.button :submit %>
402
- <% end %>
403
- ```
404
-
405
- ## Associations
268
+ ### Associations
406
269
 
407
270
  To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or check boxes.
408
271
  Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
@@ -451,7 +314,7 @@ the collection by hand, all together with the prompt:
451
314
  f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
452
315
  ```
453
316
 
454
- ## Buttons
317
+ ### Buttons
455
318
 
456
319
  All web forms need buttons, right? **SimpleForm** wraps them in the DSL, acting like a proxy:
457
320
 
@@ -464,7 +327,7 @@ All web forms need buttons, right? **SimpleForm** wraps them in the DSL, acting
464
327
 
465
328
  The above will simply call submit. You choose to use it or not, it's just a question of taste.
466
329
 
467
- ## Wrapping Rails Form Helpers
330
+ ### Wrapping Rails Form Helpers
468
331
 
469
332
  Say you wanted to use a rails form helper but still wrap it in **SimpleForm** goodness? You can, by
470
333
  calling input with a block like so:
@@ -478,14 +341,15 @@ calling input with a block like so:
478
341
  In the above example, we're taking advantage of Rails 3's select method that allows us to pass in a
479
342
  hash of additional attributes for each option.
480
343
 
481
- ## Extra helpers
344
+ ### Extra helpers
482
345
 
483
346
  **SimpleForm** also comes with some extra helpers you can use inside rails default forms without relying
484
347
  on `simple_form_for` helper. They are listed below.
485
348
 
486
- ### Simple Fields For
349
+ #### Simple Fields For
487
350
 
488
- Wrapper to use SimpleForm inside a default rails form
351
+ Wrapper to use **SimpleForm** inside a default rails form. It works in the same way that the `field_for`
352
+ Rails helper, but change the builder to use the `SimpleForm::FormBuilder`.
489
353
 
490
354
  ```ruby
491
355
  form_for @user do |f|
@@ -496,7 +360,8 @@ form_for @user do |f|
496
360
  end
497
361
  ```
498
362
 
499
- ### Collection Radio Buttons
363
+
364
+ #### Collection Radio Buttons
500
365
 
501
366
  Creates a collection of radio inputs with labels associated (same API as `collection_select`):
502
367
 
@@ -513,7 +378,7 @@ end
513
378
  <label class="collection_radio_buttons" for="user_options_false">No</label>
514
379
  ```
515
380
 
516
- ### Collection Check Boxes
381
+ #### Collection Check Boxes
517
382
 
518
383
  Creates a collection of check boxes with labels associated (same API as `collection_select`):
519
384
 
@@ -544,7 +409,7 @@ end
544
409
 
545
410
  **SimpleForm** comes with a lot of default mappings:
546
411
 
547
- ```
412
+ ```text
548
413
  Mapping Input Column Type
549
414
 
550
415
  boolean check box boolean
@@ -603,6 +468,17 @@ class DateTimeInput < SimpleForm::Inputs::DateTimeInput
603
468
  end
604
469
  ```
605
470
 
471
+ Or if you want to add a class to all the select fields you can do:
472
+
473
+ ```ruby
474
+ # app/inputs/collection_select_input.rb
475
+ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
476
+ def input_html_classes
477
+ super.push('chosen')
478
+ end
479
+ end
480
+ ```
481
+
606
482
  ## Custom form builder
607
483
 
608
484
  You can create a custom form builder that uses **SimpleForm**.
@@ -738,6 +614,132 @@ There are other options that can be configured through I18n API, such as require
738
614
  Be sure to check our locale file or the one copied to your application after you run
739
615
  `rails generate simple_form:install`.
740
616
 
617
+ ## Configuration
618
+
619
+ **SimpleForm** has several configuration options. You can read and change them in the initializer
620
+ created by **SimpleForm**, so if you haven't executed the command below yet, please do:
621
+
622
+ `rails generate simple_form:install`
623
+
624
+ ### The wrappers API
625
+
626
+ With **SimpleForm** you can configure how your components will be rendered using the wrappers API.
627
+ The syntax looks like this:
628
+
629
+ ```ruby
630
+ config.wrappers :tag => :div, :class => :input,
631
+ :error_class => :field_with_errors do |b|
632
+
633
+ # Form extensions
634
+ b.use :html5
635
+ b.optional :pattern
636
+ b.use :maxlength
637
+ b.use :placeholder
638
+ b.use :readonly
639
+
640
+ # Form components
641
+ b.use :label_input
642
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
643
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
644
+ end
645
+ ```
646
+
647
+ The _Form components_ will generate the form tags like labels, inputs, hints or errors contents. The available components are:
648
+
649
+ ```ruby
650
+ :label # The <label> tag alone
651
+ :input # The <input> tag alone
652
+ :label_input # The <label> and the <input> tags
653
+ :hint # The hint for the input
654
+ :error # The error for the input
655
+ ```
656
+
657
+ The _Form extensions_ are used to generate some attributes or perform some lookups on the model to
658
+ add extra information to your components.
659
+
660
+ You can create new _Form components_ using the wrappers API as in the following example:
661
+
662
+ ```ruby
663
+ config.wrappers do |b|
664
+ b.use :placeholder
665
+ b.use :label_input
666
+ b.wrapper :tag => :div, :class => 'separator' do |component|
667
+ component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
668
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
669
+ end
670
+ end
671
+ ```
672
+
673
+ this will wrap the hint and error components within a `div` tag using the class `'separator'`.
674
+
675
+ If you want to customize the custom _Form components_ on demand you can give it a name like this:
676
+
677
+ ```ruby
678
+ config.wrappers do |b|
679
+ b.use :placeholder
680
+ b.use :label_input
681
+ b.wrapper :my_wrapper, :tag => :div, :class => 'separator' do |component|
682
+ component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
683
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
684
+ end
685
+ end
686
+ ```
687
+
688
+ and now you can pass options to your `input` calls to customize the `:my_wrapper` _Form component_.
689
+
690
+ ```ruby
691
+ # Completely turns off the custom wrapper
692
+ f.input :name, :my_wrapper => false
693
+
694
+ # Configure the html
695
+ f.input :name, :my_wrapper_html => { :id => 'special_id' }
696
+
697
+ # Configure the tag
698
+ f.input :name, :my_wrapper_tag => :p
699
+ ```
700
+
701
+ You can also define more than one wrapper and pick one to render in a specific form or input.
702
+ To define another wrapper you have to give it a name, as the follow:
703
+
704
+ ```ruby
705
+ config.wrappers :small do |b|
706
+ b.use :placeholder
707
+ b.use :label_input
708
+ end
709
+ ```
710
+
711
+ and use it in this way:
712
+
713
+ ```ruby
714
+ # Specifying to whole form
715
+ simple_form_for @user, :wrapper => :small do |f|
716
+ f.input :name
717
+ end
718
+
719
+ # Specifying to one input
720
+ simple_form_for @user do |f|
721
+ f.input :name, :wrapper => :small
722
+ end
723
+ ```
724
+
725
+ **SimpleForm** also allows you to use optional elements. For instance, let's suppose you want to use
726
+ hints or placeholders, but you don't want them to be generated automatically. You can set their
727
+ default values to `false` or use the `optional` method. Is preferible to use the `optional` syntax:
728
+
729
+ ```ruby
730
+ config.wrappers :placeholder => false do |b|
731
+ b.use :placeholder
732
+ b.use :label_input
733
+ b.wrapper :tag => :div, :class => 'separator' do |component|
734
+ component.optional :hint, :wrap_with => { :tag => :span, :class => :hint }
735
+ component.use :error, :wrap_with => { :tag => :span, :class => :error }
736
+ end
737
+ end
738
+ ```
739
+
740
+ By setting it as `optional`, a hint will only be generated when `:hint => true` is explicitly used.
741
+ The same for placehold.
742
+
741
743
  ## HTML 5 Notice
742
744
 
743
745
  By default, **SimpleForm** will generate input field types and attributes that are supported in HTML5,
@@ -813,4 +815,4 @@ https://github.com/plataformatec/simple_form/issues
813
815
 
814
816
  ## License
815
817
 
816
- MIT License. Copyright 2012 Plataforma Tecnologia. http://blog.plataformatec.com.br
818
+ MIT License. Copyright 2012 Plataformatec. http://plataformatec.com.br
@@ -100,7 +100,9 @@ SimpleForm.setup do |config|
100
100
  # Default class for buttons
101
101
  config.button_class = 'btn'
102
102
 
103
- # Method used to tidy up errors.
103
+ # Method used to tidy up errors. Specify any Rails Array method.
104
+ # :first lists the first message for each field.
105
+ # Use :to_sentence to list all errors for each field.
104
106
  # config.error_method = :first
105
107
 
106
108
  # Default tag used for error notification helper.
@@ -9,16 +9,18 @@ en:
9
9
  # When using html, text and mark won't be used.
10
10
  # html: '<abbr title="required">*</abbr>'
11
11
  error_notification:
12
- default_message: "Some errors were found, please take a look:"
12
+ default_message: "Please review the problems below:"
13
13
  # Labels and hints examples
14
14
  # labels:
15
- # password: 'Password'
15
+ # defaults:
16
+ # password: 'Password'
16
17
  # user:
17
18
  # new:
18
- # email: 'E-mail para efetuar o sign in.'
19
+ # email: 'E-mail to sign in.'
19
20
  # edit:
20
21
  # email: 'E-mail.'
21
22
  # hints:
22
- # username: 'User name to sign in.'
23
- # password: 'No special characters, please.'
23
+ # defaults:
24
+ # username: 'User name to sign in.'
25
+ # password: 'No special characters, please.'
24
26
 
@@ -193,7 +193,8 @@ module SimpleForm
193
193
  # end
194
194
  def simple_fields_for(*args, &block)
195
195
  options = args.extract_options!
196
- options[:wrapper] ||= self.options[:wrapper]
196
+ options[:wrapper] ||= self.options[:wrapper]
197
+ options[:defaults] ||= self.options[:defaults]
197
198
 
198
199
  if self.class < ActionView::Helpers::FormBuilder
199
200
  options[:builder] ||= self.class
@@ -216,7 +217,8 @@ module SimpleForm
216
217
  html_options = html_options.dup
217
218
 
218
219
  [:checked, :selected, :disabled].each do |option|
219
- next unless current_option = options[option]
220
+ current_option = options[option]
221
+ next if current_option.nil?
220
222
 
221
223
  accept = if current_option.respond_to?(:call)
222
224
  current_option.call(item)
@@ -5,7 +5,8 @@ module SimpleForm
5
5
  def hint
6
6
  @hint ||= begin
7
7
  hint = options[:hint]
8
- hint.is_a?(String) ? hint : translate(:hints)
8
+ hint_content = hint.is_a?(String) ? hint : translate(:hints)
9
+ hint_content.html_safe if hint_content
9
10
  end
10
11
  end
11
12
 
@@ -30,7 +30,7 @@ module SimpleForm
30
30
  end
31
31
 
32
32
  def label_text
33
- SimpleForm.label_text.call(raw_label_text, required_label_text).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
@@ -7,6 +7,7 @@ module SimpleForm
7
7
  input_html_options[:min] ||= minimum_value(validator_options)
8
8
  input_html_options[:max] ||= maximum_value(validator_options)
9
9
  end
10
+ nil
10
11
  end
11
12
 
12
13
  private
@@ -41,7 +41,7 @@ module SimpleForm
41
41
  lookups = []
42
42
  lookups << :"#{object_name}"
43
43
  lookups << :default_message
44
- lookups << "Some errors were found, please take a look:"
44
+ lookups << "Please review the problems below:"
45
45
  I18n.t(lookups.shift, :scope => :"simple_form.error_notification", :default => lookups)
46
46
  end
47
47
  end
@@ -100,7 +100,7 @@ module SimpleForm
100
100
  # == Priority
101
101
  #
102
102
  # Some inputs, as :time_zone and :country accepts a :priority option. If none is
103
- # given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
103
+ # given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectively.
104
104
  #
105
105
  def input(attribute_name, options={}, &block)
106
106
  options = @defaults.deep_dup.deep_merge(options) if @defaults
@@ -48,6 +48,7 @@ module SimpleForm
48
48
  def initialize(builder, attribute_name, column, input_type, options = {})
49
49
  super
50
50
 
51
+ options = options.dup
51
52
  @builder = builder
52
53
  @attribute_name = attribute_name
53
54
  @column = column
@@ -86,7 +87,22 @@ module SimpleForm
86
87
  end
87
88
 
88
89
  def limit
89
- column && column.limit
90
+ if column
91
+ decimal_or_float? ? decimal_limit : column_limit
92
+ end
93
+ end
94
+
95
+ def column_limit
96
+ column.limit
97
+ end
98
+
99
+ # Add one for decimal point
100
+ def decimal_limit
101
+ column_limit && (column_limit + 1)
102
+ end
103
+
104
+ def decimal_or_float?
105
+ column.number? && column.type != :integer
90
106
  end
91
107
 
92
108
  def nested_boolean_style?
@@ -100,7 +116,8 @@ module SimpleForm
100
116
 
101
117
  # Retrieve options for the given namespace from the options hash
102
118
  def html_options_for(namespace, css_classes)
103
- html_options = options[:"#{namespace}_html"] || {}
119
+ html_options = options[:"#{namespace}_html"]
120
+ html_options = html_options ? html_options.dup : {}
104
121
  css_classes << html_options[:class] if html_options.key?(:class)
105
122
  html_options[:class] = css_classes
106
123
  html_options
@@ -5,7 +5,7 @@ module SimpleForm
5
5
  if nested_boolean_style?
6
6
  build_hidden_field_for_checkbox +
7
7
  template.label_tag(nil, :class => "checkbox") {
8
- build_check_box_without_hidden_field
8
+ build_check_box_without_hidden_field + inline_label
9
9
  }
10
10
  else
11
11
  build_check_box
@@ -53,6 +53,11 @@ module SimpleForm
53
53
  :disabled => input_html_options[:disabled])
54
54
  end
55
55
 
56
+ def inline_label
57
+ inline_option = options[:inline_label]
58
+ inline_option == true ? label_text : inline_option
59
+ end
60
+
56
61
  # Booleans are not required by default because in most of the cases
57
62
  # it makes no sense marking them as required. The only exception is
58
63
  # Terms of Use usually presented at most sites sign up screen.
@@ -1,3 +1,3 @@
1
1
  module SimpleForm
2
- VERSION = "2.0.1".freeze
2
+ VERSION = "2.0.2".freeze
3
3
  end
@@ -63,6 +63,12 @@ class BuilderTest < ActionView::TestCase
63
63
  assert_select 'form input[type=radio][value=true][checked=checked]'
64
64
  assert_no_select 'form input[type=radio][value=false][checked=checked]'
65
65
  end
66
+
67
+ test 'collection radio accepts checked item which has a value of false' do
68
+ with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, :checked => false
69
+ assert_no_select 'form input[type=radio][value=true][checked=checked]'
70
+ assert_select 'form input[type=radio][value=false][checked=checked]'
71
+ end
66
72
 
67
73
  test 'collection radio accepts multiple disabled items' do
68
74
  collection = [[1, true], [0, false], [2, 'other']]