simple_form 1.3.1 → 1.4.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.

data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ script: "git submodule update --init && rake test"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - ree
6
+ - jruby
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,27 @@
1
+ == 1.4.0
2
+
3
+ * enhancements
4
+ * Add label class configuration option (by github.com/reu)
5
+ * Improve i18n lookup (labels/hints/placeholders) for nested models
6
+ * Use the given custom builder with simple_fields_for (by github.com/giniedp)
7
+ * Add slim form generator (by github.com/fagiani)
8
+ * Add form_class configuration option (by github.com/fagiani)
9
+ * Default step of "any" for number input with non integer attributes (by github.com/fedesoria)
10
+ * Add option to disable HTML5 browser validations on all forms (by github.com/coryschires)
11
+ * Add option to disable all HTML5 extensions (by github.com/wolframarnold)
12
+ * Add input_field helper to form builder (by github.com/jeroenhouben)
13
+ * Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic)
14
+ * Add full_error on that shows the error with the attribute name
15
+
16
+ * bug fix
17
+ * Fix for file attributes automatic detection, to work with virtual attributes
18
+ * Fix for numeric fields validation options using symbols and procs
19
+ * Fix password attributes to add size and maxlength options the same way as string (by github.com/fedesoria)
20
+ * Fix bug with custom form builders and new mappings being added to the superclass builder (by github.com/rdvdijk)
21
+ * Fix HTML validation issue with collection_check_boxes
22
+
23
+ == 1.3.1
24
+
1
25
  * enhancements
2
26
  * Add :autofocus HTML5 attribute support (by github.com/jpzwarte)
3
27
  * Add possibility to specify custom builder and inherit mappings (by github.com/rejeep)
data/Gemfile CHANGED
@@ -1,13 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "~> 3.0.0"
3
+ gem "rails", "~> 3.0.7"
4
4
 
5
5
  group :test do
6
- gem "mocha", :require => false
7
-
8
- if RUBY_VERSION < "1.9"
9
- gem "ruby-debug", :require => false
10
- else
11
- gem "test-unit", :require => false
12
- end
6
+ gem "mocha", :require => false
7
+ gem "ruby-debug", :require => false, :platform => :ruby_18
13
8
  end
data/Gemfile.lock CHANGED
@@ -2,40 +2,40 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  abstract (1.0.0)
5
- actionmailer (3.0.3)
6
- actionpack (= 3.0.3)
7
- mail (~> 2.2.9)
8
- actionpack (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
11
  builder (~> 2.1.2)
12
12
  erubis (~> 2.6.6)
13
- i18n (~> 0.4)
13
+ i18n (~> 0.5.0)
14
14
  rack (~> 1.2.1)
15
- rack-mount (~> 0.6.13)
16
- rack-test (~> 0.5.6)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
17
  tzinfo (~> 0.3.23)
18
- activemodel (3.0.3)
19
- activesupport (= 3.0.3)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
20
  builder (~> 2.1.2)
21
- i18n (~> 0.4)
22
- activerecord (3.0.3)
23
- activemodel (= 3.0.3)
24
- activesupport (= 3.0.3)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
25
  arel (~> 2.0.2)
26
26
  tzinfo (~> 0.3.23)
27
- activeresource (3.0.3)
28
- activemodel (= 3.0.3)
29
- activesupport (= 3.0.3)
30
- activesupport (3.0.3)
31
- arel (2.0.6)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ arel (2.0.9)
32
32
  builder (2.1.2)
33
33
  columnize (0.3.2)
34
34
  erubis (2.6.6)
35
35
  abstract (>= 1.0.0)
36
36
  i18n (0.5.0)
37
37
  linecache (0.43)
38
- mail (2.2.12)
38
+ mail (2.2.18)
39
39
  activesupport (>= 2.3.6)
40
40
  i18n (>= 0.4.0)
41
41
  mime-types (~> 1.16)
@@ -44,22 +44,22 @@ GEM
44
44
  mocha (0.9.10)
45
45
  rake
46
46
  polyglot (0.3.1)
47
- rack (1.2.1)
48
- rack-mount (0.6.13)
47
+ rack (1.2.2)
48
+ rack-mount (0.6.14)
49
49
  rack (>= 1.0.0)
50
- rack-test (0.5.6)
50
+ rack-test (0.5.7)
51
51
  rack (>= 1.0)
52
- rails (3.0.3)
53
- actionmailer (= 3.0.3)
54
- actionpack (= 3.0.3)
55
- activerecord (= 3.0.3)
56
- activeresource (= 3.0.3)
57
- activesupport (= 3.0.3)
52
+ rails (3.0.7)
53
+ actionmailer (= 3.0.7)
54
+ actionpack (= 3.0.7)
55
+ activerecord (= 3.0.7)
56
+ activeresource (= 3.0.7)
57
+ activesupport (= 3.0.7)
58
58
  bundler (~> 1.0)
59
- railties (= 3.0.3)
60
- railties (3.0.3)
61
- actionpack (= 3.0.3)
62
- activesupport (= 3.0.3)
59
+ railties (= 3.0.7)
60
+ railties (3.0.7)
61
+ actionpack (= 3.0.7)
62
+ activesupport (= 3.0.7)
63
63
  rake (>= 0.8.7)
64
64
  thor (~> 0.14.4)
65
65
  rake (0.8.7)
@@ -71,12 +71,12 @@ GEM
71
71
  thor (0.14.6)
72
72
  treetop (1.4.9)
73
73
  polyglot (>= 0.3.1)
74
- tzinfo (0.3.23)
74
+ tzinfo (0.3.27)
75
75
 
76
76
  PLATFORMS
77
77
  ruby
78
78
 
79
79
  DEPENDENCIES
80
80
  mocha
81
- rails (~> 3.0.0)
81
+ rails (~> 3.0.7)
82
82
  ruby-debug
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 PlataformaTec http://blog.plataformatec.com.br/
1
+ Copyright (c) 2011 PlataformaTec http://blog.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.rdoc CHANGED
@@ -127,17 +127,18 @@ SimpleForm accepts same options as their corresponding input type helper in Rail
127
127
  <%= f.button :submit %>
128
128
  <% end %>
129
129
 
130
- SimpleForm also allows you using label, hint and error helpers it provides:
130
+ SimpleForm also allows you to use label, hint, input_field, error and full_error helpers it provides (please take a look at the rdocs for each method for more info):
131
131
 
132
132
  <%= simple_form_for @user do |f| %>
133
133
  <%= f.label :username %>
134
- <%= f.text_field :username %>
135
- <%= f.error :username, :id => 'user_name_error' %>
134
+ <%= f.input_field :username %>
136
135
  <%= f.hint 'No special characters, please!' %>
136
+ <%= f.error :username, :id => 'user_name_error' %>
137
+ <%= f.full_error :token %>
137
138
  <%= f.submit 'Save' %>
138
139
  <% end %>
139
140
 
140
- Any extra option passed to label, hint or error will be rendered as html option.
141
+ Any extra option passed to these methods will be rendered as html option.
141
142
 
142
143
  === Collections
143
144
 
@@ -157,7 +158,7 @@ Collection inputs accepts two other options beside collections:
157
158
 
158
159
  * value_method => the value method to be applied to the collection to retrieve the value
159
160
 
160
- Those methods are useful to manipulate the given collection. Both of these options also except lambda/procs incase you want to calculate the value or label in a special way eg. custom translation. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
161
+ Those methods are useful to manipulate the given collection. Both of these options also except lambda/procs in case you want to calculate the value or label in a special way eg. custom translation. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
161
162
 
162
163
  f.input :age, :collection => 18..60, :prompt => "Select your age"
163
164
 
@@ -302,6 +303,50 @@ SimpleForm comes with a lot of default mappings:
302
303
  country country select string with name matching "country"
303
304
  time_zone time zone select string with name matching "time_zone"
304
305
 
306
+ == Custom inputs
307
+
308
+ It is very easy to add custom inputs to SimpleForm. For instance, if you want to add a custom input that extends the string one, you just need to add this file:
309
+
310
+ # app/inputs/currency_input.rb
311
+ class CurrencyInput < SimpleForm::Inputs::StringInput
312
+ def input
313
+ "$ #{super}".html_safe
314
+ end
315
+ end
316
+
317
+ And use it in your views:
318
+
319
+ f.input :money, :as => :currency
320
+
321
+ You can also redefine existing SimpleForm inputs by creating a new class with the same name. For instance, if you want to wrap date/time/datetime in a div, you can do:
322
+
323
+ # app/inputs/date_time_input.rb
324
+ class DateTimeInput < SimpleForm::Inputs::DateTimeInput
325
+ def input
326
+ "<div>#{super}</div>".html_safe
327
+ end
328
+ end
329
+
330
+ == Custom form builder
331
+
332
+ You can create a custom form builder that uses SimpleForm.
333
+
334
+ Create a helper method that calls simple_form_for with a custom builder:
335
+
336
+ def custom_form_for(object, *args, &block)
337
+ options = args.extract_options!
338
+ simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
339
+ end
340
+
341
+ Create a form builder class that inherits from SimpleForm::FormBuilder.
342
+
343
+ class CustomFormBuilder < SimpleForm::FormBuilder
344
+ def input(attribute_name, options = {}, &block)
345
+ options[:input_html].merge! :class => 'custom'
346
+ super
347
+ end
348
+ end
349
+
305
350
  == I18n
306
351
 
307
352
  SimpleForm uses all power of I18n API to lookup labels, hints and placeholders. To customize your forms you can create a locale file like this:
@@ -364,35 +409,37 @@ It's also possible to translate buttons, using Rails' built-in I18n support:
364
409
 
365
410
  There are other options that can be configured through I18n API, such as required text and boolean. Be sure to check our locale file or the one copied to your application after you run "rails generate simple_form:install".
366
411
 
367
- == Configuration
412
+ == HTML 5 Notice
368
413
 
369
- SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
414
+ By default, SimpleForm will generate input field types and attributes that are supported in HTML5, but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5 extensions include the new field types such as email, number, search, url, tel, and the new attributes such as required, autofocus, maxlength, min, max, step.
370
415
 
371
- rails generate simple_form:install
416
+ Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the required attribute to force a value into an input and will prevent form submission without it. Depending on the design of the application this may or may not be desired. In many cases it can break existing UI's.
372
417
 
373
- == Custom form builder
418
+ It is possible to disable all HTML 5 extensions in SimpleForm with the following configuration:
374
419
 
375
- You can create a custom form builder that uses SimpleForm.
420
+ SimpleForm.html5 = false # default is true
376
421
 
377
- Create a helper method that calls simple_form_for with a custom builder:
422
+ If you want to have all other HTML 5 features, such as the new field types, you can disable only the browser validation:
378
423
 
379
- def custom_form_for(object, *args, &block)
380
- simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
381
- end
424
+ SimpleForm.browser_validations = false # default is true
382
425
 
383
- Create a form builder class that inherits from SimpleForm::FormBuilder.
426
+ This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5 validation. The inputs will still be generated with the required and other attributes, that might help you to use some generic javascript validation.
384
427
 
385
- class CustomFormBuilder < SimpleForm::FormBuilder
386
- def input(attribute_name, *args, &block)
387
- super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block)
388
- end
389
- end
428
+ Please notice that any of the configurations above will disable the `placeholder` component, which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine, and if they aren't, any plugin you use would take of using the placeholder attribute to do it. However, you can disable it if you want, by removing the placeholder component from the components list in SimpleForm configuration file.
429
+
430
+ == Configuration
431
+
432
+ SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
433
+
434
+ rails generate simple_form:install
390
435
 
391
436
  == Maintainers
392
437
 
393
- * José Valim (http://github.com/josevalim)
394
- * Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
438
+ * José Valim (https://github.com/josevalim)
439
+ * Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
440
+ * Rafael Mendonça França (https://github.com/rafaelfranca)
395
441
 
396
442
  == License
397
443
 
398
- MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
444
+ MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
445
+
@@ -1,4 +1,4 @@
1
- <%%= simple_form_for(@<%= singular_name %>) do |f| %>
1
+ <%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
2
2
  <%%= f.error_notification %>
3
3
 
4
4
  <div class="inputs">
@@ -1,13 +1,5 @@
1
- = simple_form_for(@<%= singular_name %>) do |f|
2
- - if @<%= singular_name %>.errors.any?
3
- #error_explanation
4
- %h2
5
- = pluralize(@<%= singular_name %>.errors.count, "error")
6
- prohibited this <%= singular_name %> from being saved:
7
-
8
- %ul
9
- - @<%= singular_name %>.errors.full_messages.each do |msg|
10
- %li= msg
1
+ = simple_form_for(@<%= singular_table_name %>) do |f|
2
+ = f.error_notification
11
3
 
12
4
  .inputs
13
5
  <%- attributes.each do |attribute| -%>
@@ -0,0 +1,10 @@
1
+ = simple_form_for(@<%= singular_table_name %>) do |f|
2
+ = f.error_notification
3
+
4
+ .inputs
5
+ | <%- attributes.each do |attribute| -%>
6
+ = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
7
+ | <%- end -%>
8
+
9
+ .actions
10
+ = f.button :submit
@@ -10,7 +10,7 @@ SimpleForm.setup do |config|
10
10
  # CSS class to add to all hint tags.
11
11
  # config.hint_class = :hint
12
12
 
13
- # Default tag used on errors.
13
+ # CSS class used on errors.
14
14
  # config.error_class = :error
15
15
 
16
16
  # Default tag used on errors.
@@ -40,8 +40,8 @@ SimpleForm.setup do |config|
40
40
  # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
41
41
  # config.collection_wrapper_tag = nil
42
42
 
43
- # You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
44
- # config.item_wrapper_tag = nil
43
+ # You can wrap each item in a collection of radio/check boxes with a tag, defaulting to span.
44
+ # config.item_wrapper_tag = :span
45
45
 
46
46
  # Series of attemps to detect a default label method for collection.
47
47
  # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
@@ -52,16 +52,32 @@ SimpleForm.setup do |config|
52
52
  # How the label text should be generated altogether with the required text.
53
53
  # config.label_text = lambda { |label, required| "#{required} #{label}" }
54
54
 
55
+ # You can define the class to use on all labels. Default is nil.
56
+ # config.label_class = nil
57
+
58
+ # You can define the class to use on all forms. Default is simple_form.
59
+ # config.form_class = :simple_form
60
+
55
61
  # Whether attributes are required by default (or not). Default is true.
56
62
  # config.required_by_default = true
57
63
 
64
+ # Tell browsers whether to use default HTML5 validations (novalidate option).
65
+ # Default is enabled.
66
+ # config.browser_validations = true
67
+
68
+ # Determines whether HTML5 types (:email, :url, :search, :tel) and attributes
69
+ # (e.g. required) are used or not. True by default.
70
+ # Having this on in non-HTML5 compliant sites can cause odd behavior in
71
+ # HTML5-aware browsers such as Chrome.
72
+ # config.html5 = true
73
+
58
74
  # Custom mappings for input types. This should be a hash containing a regexp
59
75
  # to match as key, and the input type that will be used when the field name
60
76
  # matches the regexp as value.
61
77
  # config.input_mappings = { /count/ => :integer }
62
78
 
63
79
  # Collection of methods to detect if a file type was given.
64
- # config.file_methods = [ :file?, :public_filename ]
80
+ # config.file_methods = [ :mounted_as, :file?, :public_filename ]
65
81
 
66
82
  # Default priority for time_zone inputs.
67
83
  # config.time_zone_priority = nil
data/lib/simple_form.rb CHANGED
@@ -61,7 +61,7 @@ module SimpleForm
61
61
 
62
62
  # You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
63
63
  mattr_accessor :item_wrapper_tag
64
- @@item_wrapper_tag = nil
64
+ @@item_wrapper_tag = :span
65
65
 
66
66
  # You can wrap all inputs in a pre-defined tag. Default is a div.
67
67
  mattr_accessor :wrapper_tag
@@ -79,10 +79,29 @@ module SimpleForm
79
79
  mattr_accessor :label_text
80
80
  @@label_text = lambda { |label, required| "#{required} #{label}" }
81
81
 
82
+ # You can define the class to use on all labels. Default is nil.
83
+ mattr_accessor :label_class
84
+ @@label_class = nil
85
+
86
+ # You can define the class to use on all forms. Default is simple_form.
87
+ mattr_accessor :form_class
88
+ @@form_class = :simple_form
89
+
82
90
  # Whether attributes are required by default (or not).
83
91
  mattr_accessor :required_by_default
84
92
  @@required_by_default = true
85
93
 
94
+ # Tell browsers whether to use default HTML5 validations (novalidate option).
95
+ mattr_accessor :browser_validations
96
+ @@browser_validations = true
97
+
98
+ # Determines whether HTML5 types (:email, :url, :search, :tel) and attributes
99
+ # (e.g. required) are used or not. True by default.
100
+ # Having this on in non-HTML5 compliant sites can cause odd behavior in
101
+ # HTML5-aware browsers such as Chrome.
102
+ mattr_accessor :html5
103
+ @@html5 = true
104
+
86
105
  # Collection of methods to detect if a file type was given.
87
106
  mattr_accessor :file_methods
88
107
  @@file_methods = [ :mounted_as, :file?, :public_filename ]
@@ -110,6 +129,14 @@ module SimpleForm
110
129
  mattr_accessor :translate
111
130
  @@translate = true
112
131
 
132
+ # Automatically discover new inputs in Rails' autoload path.
133
+ mattr_accessor :inputs_discovery
134
+ @@inputs_discovery = true
135
+
136
+ # Cache simple form inputs discovery
137
+ mattr_accessor :cache_discovery
138
+ @@cache_discovery = !Rails.env.development?
139
+
113
140
  # Default way to setup SimpleForm. Run rails generate simple_form:install
114
141
  # to create a fresh initializer with all configuration values.
115
142
  def self.setup