simple_form 1.4.2 → 1.5.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 (51) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +9 -1
  3. data/CHANGELOG.rdoc +20 -0
  4. data/Gemfile +5 -4
  5. data/README.rdoc +2 -2
  6. data/Rakefile +1 -1
  7. data/lib/generators/simple_form/install_generator.rb +2 -6
  8. data/lib/generators/simple_form/templates/{simple_form.rb → config/initializers/simple_form.rb} +0 -0
  9. data/lib/generators/simple_form/templates/{en.yml → config/locales/simple_form.en.yml} +0 -0
  10. data/lib/simple_form.rb +1 -1
  11. data/lib/simple_form/action_view_extensions/builder.rb +12 -5
  12. data/lib/simple_form/action_view_extensions/form_helper.rb +13 -8
  13. data/lib/simple_form/components.rb +1 -1
  14. data/lib/simple_form/components/errors.rb +10 -2
  15. data/lib/simple_form/components/hints.rb +10 -0
  16. data/lib/simple_form/components/label_input.rb +5 -3
  17. data/lib/simple_form/components/labels.rb +10 -4
  18. data/lib/simple_form/components/placeholders.rb +10 -4
  19. data/lib/simple_form/error_notification.rb +1 -1
  20. data/lib/simple_form/form_builder.rb +10 -7
  21. data/lib/simple_form/helpers.rb +9 -0
  22. data/lib/simple_form/helpers/has_errors.rb +15 -0
  23. data/lib/simple_form/helpers/maxlength.rb +24 -0
  24. data/lib/simple_form/helpers/pattern.rb +28 -0
  25. data/lib/simple_form/helpers/required.rb +36 -0
  26. data/lib/simple_form/helpers/validators.rb +44 -0
  27. data/lib/simple_form/inputs.rb +5 -2
  28. data/lib/simple_form/inputs/base.rb +24 -47
  29. data/lib/simple_form/inputs/boolean_input.rb +1 -1
  30. data/lib/simple_form/inputs/collection_input.rb +1 -1
  31. data/lib/simple_form/inputs/date_time_input.rb +1 -1
  32. data/lib/simple_form/inputs/file_input.rb +9 -0
  33. data/lib/simple_form/inputs/hidden_input.rb +1 -1
  34. data/lib/simple_form/inputs/numeric_input.rb +20 -13
  35. data/lib/simple_form/inputs/password_input.rb +13 -0
  36. data/lib/simple_form/inputs/range_input.rb +16 -0
  37. data/lib/simple_form/inputs/string_input.rb +7 -24
  38. data/lib/simple_form/inputs/text_input.rb +12 -0
  39. data/lib/simple_form/version.rb +1 -1
  40. data/test/action_view_extensions/builder_test.rb +59 -0
  41. data/test/action_view_extensions/form_helper_test.rb +21 -0
  42. data/test/discovery_inputs.rb +2 -2
  43. data/test/form_builder_test.rb +54 -0
  44. data/test/inputs_test.rb +185 -20
  45. data/test/support/models.rb +23 -2
  46. data/test/test_helper.rb +1 -0
  47. metadata +17 -11
  48. data/Gemfile.lock +0 -54
  49. data/init.rb +0 -1
  50. data/lib/simple_form/has_errors.rb +0 -14
  51. data/lib/simple_form/inputs/mapping_input.rb +0 -29
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  .bundle/
2
2
  pkg/
3
+ Gemfile.lock
data/.travis.yml CHANGED
@@ -2,6 +2,14 @@ script: "git submodule update --init && bundle exec rake test"
2
2
  rvm:
3
3
  - 1.8.7
4
4
  - 1.9.2
5
+ - 1.9.3
6
+ - ruby-head
5
7
  - ree
6
8
  - jruby
7
- - rubinius
9
+ - rbx
10
+ - rbx-2.0
11
+ notifications:
12
+ recipients:
13
+ - jose.valim@plataformatec.com.br
14
+ - carlos@plataformatec.com.br
15
+ - rafael.franca@plataformatec.com.br
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,23 @@
1
+ == 1.5.0
2
+
3
+ * enhancements
4
+ * Simplified generator by using directory action (by github.com/rupert654)
5
+ * Support for `maxlength` on string inputs inferred from validation (by github.com/srbartlett)
6
+ * Change form css class handling to only add the dom class when one is not given to the form call (by github.com/patrick99e99)
7
+ * Support for required attributes when action validations are present (by github.com/csegura)
8
+ * Don't generate `size` attribute for numeric input (by github.com/jasonmp85)
9
+ * Support for `maxlength` on text area inputs inferred from validation
10
+ * Support for `pattern` on text field inferred from validation when :pattern is true
11
+ * Break Text, Password and File into their own inputs
12
+ * Support easy enabling and disabling of components for specific inputs
13
+ * Add HTML5 range input
14
+
15
+ * bug fix
16
+ * Fix bug when simple_fields_for is used with a hash like models and Rails 3.1
17
+ * Fix bug that doesn't remove the item_wrapper_tag or the collection_wrapper_tag on collection inputs when nil or false value is passed to these options (by gitbub.com/dw2)
18
+ * Fix bug that disable the entire select and wrapper when `disabled` option is a string or array
19
+ * Fix bug when using label/value methods as procs together with disabled/selected options as procs for select inputs
20
+
1
21
  == 1.4.2
2
22
 
3
23
  * enhancements
data/Gemfile CHANGED
@@ -2,7 +2,8 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- group :test do
6
- gem "mocha", :require => false
7
- gem "ruby-debug", :require => false, :platform => :mri_18
8
- end
5
+ gem "rake", '0.9.3.beta1'
6
+ gem "rdoc"
7
+ gem "mocha"
8
+ gem "tzinfo"
9
+ gem "ruby-debug", :platform => :mri_18
data/README.rdoc CHANGED
@@ -158,7 +158,7 @@ Collection inputs accepts two other options beside collections:
158
158
 
159
159
  * value_method => the value method to be applied to the collection to retrieve the value
160
160
 
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
+ Those methods are useful to manipulate the given collection. Both of these options also accept 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:
162
162
 
163
163
  f.input :age, :collection => 18..60, :prompt => "Select your age"
164
164
 
@@ -311,6 +311,7 @@ SimpleForm comes with a lot of default mappings:
311
311
  integer number field integer
312
312
  float number field float
313
313
  decimal number field decimal
314
+ range range field -
314
315
  datetime datetime select datetime/timestamp
315
316
  date date select date
316
317
  time time select time
@@ -456,7 +457,6 @@ SimpleForm has several configuration values. You can read and change them in the
456
457
 
457
458
  == Maintainers
458
459
 
459
- * José Valim (https://github.com/josevalim)
460
460
  * Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
461
461
  * Rafael Mendonça França (https://github.com/rafaelfranca)
462
462
 
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'bundler'
4
4
  Bundler::GemHelper.install_tasks
5
5
 
6
6
  require 'rake/testtask'
7
- require 'rake/rdoctask'
7
+ require 'rdoc/task'
8
8
 
9
9
  desc 'Default: run unit tests.'
10
10
  task :default => :test
@@ -5,12 +5,8 @@ module SimpleForm
5
5
  source_root File.expand_path('../templates', __FILE__)
6
6
  class_option :template_engine
7
7
 
8
- def copy_initializers
9
- copy_file 'simple_form.rb', 'config/initializers/simple_form.rb'
10
- end
11
-
12
- def copy_locale_file
13
- copy_file 'en.yml', 'config/locales/simple_form.en.yml'
8
+ def copy_config
9
+ directory 'config'
14
10
  end
15
11
 
16
12
  def copy_scaffold_template
data/lib/simple_form.rb CHANGED
@@ -6,7 +6,7 @@ module SimpleForm
6
6
  autoload :Components, 'simple_form/components'
7
7
  autoload :ErrorNotification, 'simple_form/error_notification'
8
8
  autoload :FormBuilder, 'simple_form/form_builder'
9
- autoload :HasErrors, 'simple_form/has_errors'
9
+ autoload :Helpers, 'simple_form/helpers'
10
10
  autoload :I18nCache, 'simple_form/i18n_cache'
11
11
  autoload :Inputs, 'simple_form/inputs'
12
12
  autoload :MapType, 'simple_form/map_type'
@@ -114,10 +114,10 @@ module SimpleForm
114
114
  def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
115
115
  html_options = html_options.dup
116
116
 
117
- [:checked, :disabled].each do |option|
117
+ [:checked, :selected, :disabled].each do |option|
118
118
  next unless options[option]
119
119
 
120
- accept = if options[option].is_a?(Proc)
120
+ accept = if options[option].respond_to?(:call)
121
121
  options[option].call(item)
122
122
  else
123
123
  Array(options[option]).include?(value)
@@ -134,8 +134,8 @@ module SimpleForm
134
134
  end
135
135
 
136
136
  def render_collection(attribute, collection, value_method, text_method, options={}, html_options={}) #:nodoc:
137
- collection_wrapper_tag = options[:collection_wrapper_tag] || SimpleForm.collection_wrapper_tag
138
- item_wrapper_tag = options[:item_wrapper_tag] || SimpleForm.item_wrapper_tag
137
+ collection_wrapper_tag = options.has_key?(:collection_wrapper_tag) ? options[:collection_wrapper_tag] : SimpleForm.collection_wrapper_tag
138
+ item_wrapper_tag = options.has_key?(:item_wrapper_tag) ? options[:item_wrapper_tag] : SimpleForm.item_wrapper_tag
139
139
 
140
140
  rendered_collection = collection.map do |item|
141
141
  value = value_for_collection(item, value_method)
@@ -171,8 +171,15 @@ class ActionView::Helpers::FormBuilder
171
171
  collection = collection.map do |item|
172
172
  value = value_for_collection(item, value_method)
173
173
  text = value_for_collection(item, text_method)
174
- [value, text]
174
+
175
+ default_html_options = default_html_options_for_collection(item, value, options, html_options)
176
+ disabled = value if default_html_options[:disabled]
177
+ selected = value if default_html_options[:selected]
178
+
179
+ [value, text, selected, disabled]
175
180
  end
181
+ options[:disabled] = collection.map(&:pop).compact
182
+ options[:selected] = collection.map(&:pop).compact
176
183
  value_method, text_method = :first, :last
177
184
  end
178
185
 
@@ -31,24 +31,29 @@ module SimpleForm
31
31
 
32
32
  def simple_form_for(record, options={}, &block)
33
33
  options[:builder] ||= SimpleForm::FormBuilder
34
- css_class = case record
35
- when String, Symbol then record.to_s
36
- when Array then dom_class(record.last)
37
- else dom_class(record)
38
- end
39
34
  options[:html] ||= {}
40
35
  unless options[:html].key?(:novalidate)
41
36
  options[:html][:novalidate] = !SimpleForm.browser_validations
42
37
  end
43
- options[:html][:class] = "#{SimpleForm.form_class} #{css_class} #{options[:html][:class]}".strip
44
-
38
+ options[:html][:class] = [SimpleForm.form_class, css_class(record, options[:html])].compact.join(" ")
45
39
  with_custom_field_error_proc do
46
40
  form_for(record, options, &block)
47
41
  end
48
42
  end
49
43
 
44
+ def css_class(record, html_options)
45
+ if html_options.key?(:class)
46
+ html_options[:class]
47
+ elsif record.is_a?(String) || record.is_a?(Symbol)
48
+ record
49
+ else
50
+ record = record.last if record.is_a?(Array)
51
+ dom_class(record)
52
+ end
53
+ end
54
+
50
55
  def simple_fields_for(record_name, record_object = nil, options = {}, &block)
51
- options, record_object = record_object, nil if record_object.is_a?(Hash)
56
+ options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
52
57
  options[:builder] ||= SimpleForm::FormBuilder
53
58
 
54
59
  with_custom_field_error_proc do
@@ -7,4 +7,4 @@ module SimpleForm
7
7
  autoload :Placeholders, 'simple_form/components/placeholders'
8
8
  autoload :Wrapper, 'simple_form/components/wrapper'
9
9
  end
10
- end
10
+ end
@@ -1,10 +1,10 @@
1
1
  module SimpleForm
2
2
  module Components
3
3
  module Errors
4
- include SimpleForm::HasErrors
4
+ include SimpleForm::Helpers::HasErrors
5
5
 
6
6
  def error
7
- template.content_tag(error_tag, error_text, error_html_options) if has_errors?
7
+ enabled_error
8
8
  end
9
9
 
10
10
  def error_tag
@@ -29,6 +29,14 @@ module SimpleForm
29
29
 
30
30
  protected
31
31
 
32
+ def enabled_error
33
+ template.content_tag(error_tag, error_text, error_html_options) if has_errors?
34
+ end
35
+
36
+ def disabled_error
37
+ nil
38
+ end
39
+
32
40
  def errors
33
41
  @errors ||= (errors_on_attribute + errors_on_association).compact
34
42
  end
@@ -2,9 +2,19 @@ module SimpleForm
2
2
  module Components
3
3
  module Hints
4
4
  def hint
5
+ enabled_hint
6
+ end
7
+
8
+ private
9
+
10
+ def enabled_hint
5
11
  template.content_tag(hint_tag, hint_text, hint_html_options) unless hint_text.blank?
6
12
  end
7
13
 
14
+ def disabled_hint
15
+ nil
16
+ end
17
+
8
18
  def hint_tag
9
19
  options[:hint_tag] || SimpleForm.hint_tag
10
20
  end
@@ -1,8 +1,10 @@
1
1
  module SimpleForm
2
2
  module Components
3
3
  module LabelInput
4
- def self.included(base)
5
- base.send :include, SimpleForm::Components::Labels
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ include SimpleForm::Components::Labels
6
8
  end
7
9
 
8
10
  def label_input
@@ -10,4 +12,4 @@ module SimpleForm
10
12
  end
11
13
  end
12
14
  end
13
- end
15
+ end
@@ -1,9 +1,7 @@
1
1
  module SimpleForm
2
2
  module Components
3
3
  module Labels
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
4
+ extend ActiveSupport::Concern
7
5
 
8
6
  module ClassMethods #:nodoc:
9
7
  def translate_required_html
@@ -24,7 +22,7 @@ module SimpleForm
24
22
  end
25
23
 
26
24
  def label
27
- @builder.label(label_target, label_text, label_html_options)
25
+ enabled_label
28
26
  end
29
27
 
30
28
  def label_text
@@ -43,6 +41,14 @@ module SimpleForm
43
41
 
44
42
  protected
45
43
 
44
+ def enabled_label
45
+ @builder.label(label_target, label_text, label_html_options)
46
+ end
47
+
48
+ def disabled_label
49
+ ""
50
+ end
51
+
46
52
  def raw_label_text #:nodoc:
47
53
  options[:label] || label_translation
48
54
  end
@@ -2,12 +2,18 @@ module SimpleForm
2
2
  module Components
3
3
  module Placeholders
4
4
  def placeholder
5
- input_html_options[:placeholder] ||= placeholder_text if has_placeholder?
5
+ disabled_placeholder
6
+ end
7
+
8
+ private
9
+
10
+ def enabled_placeholder
11
+ input_html_options[:placeholder] ||= placeholder_text if placeholder_present?
6
12
  nil
7
13
  end
8
14
 
9
- def has_placeholder?
10
- false
15
+ def disabled_placeholder
16
+ nil
11
17
  end
12
18
 
13
19
  def placeholder_present?
@@ -15,7 +21,7 @@ module SimpleForm
15
21
  end
16
22
 
17
23
  def placeholder_text
18
- @placeholder ||= options[:placeholder] || translate(:placeholders)
24
+ @placeholder_text ||= options[:placeholder] || translate(:placeholders)
19
25
  end
20
26
  end
21
27
  end
@@ -1,7 +1,7 @@
1
1
  module SimpleForm
2
2
  class ErrorNotification
3
3
  delegate :object, :object_name, :template, :to => :@builder
4
- include SimpleForm::HasErrors
4
+ include SimpleForm::Helpers::HasErrors
5
5
 
6
6
  def initialize(builder, options)
7
7
  @builder = builder
@@ -5,13 +5,16 @@ module SimpleForm
5
5
  extend MapType
6
6
  include SimpleForm::Inputs
7
7
 
8
- map_type :text, :file, :to => SimpleForm::Inputs::MappingInput
9
- map_type :string, :password, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
10
- map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
11
- map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
12
- map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
13
- map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
14
- map_type :boolean, :to => SimpleForm::Inputs::BooleanInput
8
+ map_type :text, :to => SimpleForm::Inputs::TextInput
9
+ map_type :file, :to => SimpleForm::Inputs::FileInput
10
+ map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
11
+ map_type :password, :to => SimpleForm::Inputs::PasswordInput
12
+ map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
13
+ map_type :range, :to => SimpleForm::Inputs::RangeInput
14
+ map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
15
+ map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
16
+ map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
17
+ map_type :boolean, :to => SimpleForm::Inputs::BooleanInput
15
18
 
16
19
  def self.discovery_cache
17
20
  @discovery_cache ||= {}
@@ -0,0 +1,9 @@
1
+ module SimpleForm
2
+ module Helpers
3
+ autoload :HasErrors, 'simple_form/helpers/has_errors'
4
+ autoload :Maxlength, 'simple_form/helpers/maxlength'
5
+ autoload :Pattern, 'simple_form/helpers/pattern'
6
+ autoload :Validators, 'simple_form/helpers/validators'
7
+ autoload :Required, 'simple_form/helpers/required'
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module SimpleForm
2
+ module Helpers
3
+ module HasErrors
4
+ private
5
+
6
+ def errors
7
+ object.errors
8
+ end
9
+
10
+ def has_errors?
11
+ object && object.respond_to?(:errors) && errors.present?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module SimpleForm
2
+ module Helpers
3
+ # Helper methods for maxlength.
4
+ module Maxlength #:nodoc:
5
+
6
+ private
7
+
8
+ def add_maxlength!
9
+ input_html_options[:maxlength] ||= maximum_length_from_validation || limit if SimpleForm.html5
10
+ end
11
+
12
+ def maximum_length_from_validation
13
+ return unless has_validators?
14
+
15
+ length_validator = find_length_validator or return
16
+ length_validator.options[:maximum]
17
+ end
18
+
19
+ def find_length_validator
20
+ find_validator(ActiveModel::Validations::LengthValidator)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module SimpleForm
2
+ module Helpers
3
+ # Helper methods for pattern.
4
+ module Pattern #:nodoc:
5
+ private
6
+
7
+ def add_pattern!
8
+ input_html_options[:pattern] ||= pattern_source if options[:pattern]
9
+ end
10
+
11
+ def pattern_source
12
+ if options[:pattern] == true
13
+ if has_validators? && pattern_validator
14
+ pattern_validator.options[:with].source
15
+ else
16
+ raise "Could not find format validator for #{attribute_name}"
17
+ end
18
+ else
19
+ options[:pattern]
20
+ end
21
+ end
22
+
23
+ def pattern_validator
24
+ @pattern_validator ||= find_validator(ActiveModel::Validations::FormatValidator)
25
+ end
26
+ end
27
+ end
28
+ end