simple_form 1.5.2 → 2.0.0.rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. data/CHANGELOG.md +224 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +817 -0
  4. data/lib/generators/simple_form/install_generator.rb +15 -1
  5. data/lib/generators/simple_form/templates/README +12 -0
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +173 -0
  7. data/lib/simple_form.rb +109 -43
  8. data/lib/simple_form/action_view_extensions/builder.rb +158 -53
  9. data/lib/simple_form/action_view_extensions/form_helper.rb +29 -22
  10. data/lib/simple_form/components.rb +11 -1
  11. data/lib/simple_form/components/errors.rb +6 -24
  12. data/lib/simple_form/components/hints.rb +7 -21
  13. data/lib/simple_form/components/html5.rb +26 -0
  14. data/lib/simple_form/components/labels.rb +15 -13
  15. data/lib/simple_form/components/maxlength.rb +41 -0
  16. data/lib/simple_form/components/min_max.rb +49 -0
  17. data/lib/simple_form/components/pattern.rb +34 -0
  18. data/lib/simple_form/components/placeholders.rb +5 -17
  19. data/lib/simple_form/components/readonly.rb +22 -0
  20. data/lib/simple_form/core_ext/hash.rb +16 -0
  21. data/lib/simple_form/error_notification.rb +8 -1
  22. data/lib/simple_form/form_builder.rb +86 -22
  23. data/lib/simple_form/helpers.rb +7 -4
  24. data/lib/simple_form/helpers/autofocus.rb +11 -0
  25. data/lib/simple_form/helpers/disabled.rb +15 -0
  26. data/lib/simple_form/helpers/readonly.rb +15 -0
  27. data/lib/simple_form/helpers/required.rb +7 -10
  28. data/lib/simple_form/helpers/validators.rb +4 -4
  29. data/lib/simple_form/inputs.rb +17 -13
  30. data/lib/simple_form/inputs/base.rb +50 -81
  31. data/lib/simple_form/inputs/boolean_input.rb +43 -4
  32. data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
  33. data/lib/simple_form/inputs/collection_input.rb +27 -13
  34. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +69 -0
  35. data/lib/simple_form/inputs/collection_select_input.rb +14 -0
  36. data/lib/simple_form/inputs/date_time_input.rb +2 -2
  37. data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
  38. data/lib/simple_form/inputs/hidden_input.rb +3 -6
  39. data/lib/simple_form/inputs/numeric_input.rb +3 -51
  40. data/lib/simple_form/inputs/password_input.rb +1 -2
  41. data/lib/simple_form/inputs/priority_input.rb +2 -2
  42. data/lib/simple_form/inputs/range_input.rb +1 -3
  43. data/lib/simple_form/inputs/string_input.rb +6 -8
  44. data/lib/simple_form/inputs/text_input.rb +1 -2
  45. data/lib/simple_form/version.rb +1 -1
  46. data/lib/simple_form/wrappers.rb +8 -0
  47. data/lib/simple_form/wrappers/builder.rb +75 -0
  48. data/lib/simple_form/wrappers/many.rb +68 -0
  49. data/lib/simple_form/wrappers/root.rb +34 -0
  50. data/lib/simple_form/wrappers/single.rb +18 -0
  51. data/test/action_view_extensions/builder_test.rb +195 -100
  52. data/test/action_view_extensions/form_helper_test.rb +24 -2
  53. data/test/components/label_test.rb +20 -5
  54. data/test/form_builder/association_test.rb +167 -0
  55. data/test/form_builder/button_test.rb +28 -0
  56. data/test/{error_notification_test.rb → form_builder/error_notification_test.rb} +2 -1
  57. data/test/form_builder/error_test.rb +101 -0
  58. data/test/form_builder/general_test.rb +348 -0
  59. data/test/form_builder/hint_test.rb +115 -0
  60. data/test/form_builder/input_field_test.rb +51 -0
  61. data/test/form_builder/label_test.rb +51 -0
  62. data/test/form_builder/wrapper_test.rb +140 -0
  63. data/test/generators/simple_form_generator_test.rb +32 -0
  64. data/test/inputs/boolean_input_test.rb +91 -0
  65. data/test/inputs/collection_check_boxes_input_test.rb +224 -0
  66. data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
  67. data/test/inputs/collection_select_input_test.rb +241 -0
  68. data/test/inputs/datetime_input_test.rb +85 -0
  69. data/test/inputs/disabled_test.rb +38 -0
  70. data/test/inputs/discovery_test.rb +61 -0
  71. data/test/inputs/file_input_test.rb +16 -0
  72. data/test/inputs/general_test.rb +69 -0
  73. data/test/inputs/grouped_collection_select_input_test.rb +109 -0
  74. data/test/inputs/hidden_input_test.rb +30 -0
  75. data/test/inputs/numeric_input_test.rb +167 -0
  76. data/test/inputs/priority_input_test.rb +43 -0
  77. data/test/inputs/readonly_test.rb +61 -0
  78. data/test/inputs/required_test.rb +113 -0
  79. data/test/inputs/string_input_test.rb +140 -0
  80. data/test/inputs/text_input_test.rb +24 -0
  81. data/test/{discovery_inputs.rb → support/discovery_inputs.rb} +0 -0
  82. data/test/support/misc_helpers.rb +48 -6
  83. data/test/support/mock_controller.rb +2 -2
  84. data/test/support/models.rb +20 -5
  85. data/test/test_helper.rb +5 -8
  86. metadata +123 -98
  87. data/.gitignore +0 -3
  88. data/.gitmodules +0 -3
  89. data/.travis.yml +0 -15
  90. data/CHANGELOG.rdoc +0 -159
  91. data/Gemfile +0 -9
  92. data/README.rdoc +0 -466
  93. data/Rakefile +0 -27
  94. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +0 -93
  95. data/lib/simple_form/components/wrapper.rb +0 -38
  96. data/lib/simple_form/helpers/has_errors.rb +0 -15
  97. data/lib/simple_form/helpers/maxlength.rb +0 -24
  98. data/lib/simple_form/helpers/pattern.rb +0 -28
  99. data/simple_form.gemspec +0 -25
  100. data/test/components/error_test.rb +0 -56
  101. data/test/components/hint_test.rb +0 -74
  102. data/test/components/wrapper_test.rb +0 -63
  103. data/test/custom_components.rb +0 -7
  104. data/test/form_builder_test.rb +0 -930
  105. data/test/inputs_test.rb +0 -995
data/Rakefile DELETED
@@ -1,27 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'bundler'
4
- Bundler::GemHelper.install_tasks
5
-
6
- require 'rake/testtask'
7
- require 'rdoc/task'
8
-
9
- desc 'Default: run unit tests.'
10
- task :default => :test
11
-
12
- desc 'Test the simple_form plugin.'
13
- Rake::TestTask.new(:test) do |t|
14
- t.libs << 'lib'
15
- t.libs << 'test'
16
- t.pattern = 'test/**/*_test.rb'
17
- t.verbose = true
18
- end
19
-
20
- desc 'Generate documentation for the simple_form plugin.'
21
- Rake::RDocTask.new(:rdoc) do |rdoc|
22
- rdoc.rdoc_dir = 'rdoc'
23
- rdoc.title = 'SimpleForm'
24
- rdoc.options << '--line-numbers' << '--inline-source'
25
- rdoc.rdoc_files.include('README.rdoc')
26
- rdoc.rdoc_files.include('lib/**/*.rb')
27
- end
@@ -1,93 +0,0 @@
1
- # Use this setup block to configure all options available in SimpleForm.
2
- SimpleForm.setup do |config|
3
- # Components used by the form builder to generate a complete input. You can remove
4
- # any of them, change the order, or even add your own components to the stack.
5
- # config.components = [ :placeholder, :label_input, :hint, :error ]
6
-
7
- # Default tag used on hints.
8
- # config.hint_tag = :span
9
-
10
- # CSS class to add to all hint tags.
11
- # config.hint_class = :hint
12
-
13
- # CSS class used on errors.
14
- # config.error_class = :error
15
-
16
- # Default tag used on errors.
17
- # config.error_tag = :span
18
-
19
- # Method used to tidy up errors.
20
- # config.error_method = :first
21
-
22
- # Default tag used for error notification helper.
23
- # config.error_notification_tag = :p
24
-
25
- # CSS class to add for error notification helper.
26
- # config.error_notification_class = :error_notification
27
-
28
- # ID to add for error notification helper.
29
- # config.error_notification_id = nil
30
-
31
- # You can wrap all inputs in a pre-defined tag.
32
- # config.wrapper_tag = :div
33
-
34
- # CSS class to add to all wrapper tags.
35
- # config.wrapper_class = :input
36
-
37
- # CSS class to add to the wrapper if the field has errors.
38
- # config.wrapper_error_class = :field_with_errors
39
-
40
- # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
41
- # config.collection_wrapper_tag = nil
42
-
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
-
46
- # Series of attempts to detect a default label method for collection.
47
- # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
48
-
49
- # Series of attempts to detect a default value method for collection.
50
- # config.collection_value_methods = [ :id, :to_s ]
51
-
52
- # How the label text should be generated altogether with the required text.
53
- # config.label_text = lambda { |label, required| "#{required} #{label}" }
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
-
61
- # Whether attributes are required by default (or not). Default is true.
62
- # config.required_by_default = true
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
-
74
- # Custom mappings for input types. This should be a hash containing a regexp
75
- # to match as key, and the input type that will be used when the field name
76
- # matches the regexp as value.
77
- # config.input_mappings = { /count/ => :integer }
78
-
79
- # Collection of methods to detect if a file type was given.
80
- # config.file_methods = [ :mounted_as, :file?, :public_filename ]
81
-
82
- # Default priority for time_zone inputs.
83
- # config.time_zone_priority = nil
84
-
85
- # Default priority for country inputs.
86
- # config.country_priority = nil
87
-
88
- # Default size for text inputs.
89
- # config.default_input_size = 50
90
-
91
- # When false, do not use translations for labels, hints or placeholders.
92
- # config.translate = true
93
- end
@@ -1,38 +0,0 @@
1
- module SimpleForm
2
- module Components
3
- module Wrapper
4
- def wrap(content)
5
- if wrapper_tag && options[:wrapper] != false
6
- template.content_tag(wrapper_tag, content, wrapper_html_options)
7
- else
8
- content
9
- end
10
- end
11
-
12
- def wrapper_tag
13
- options[:wrapper_tag] || SimpleForm.wrapper_tag
14
- end
15
-
16
- def wrapper_class
17
- options[:wrapper_class] || SimpleForm.wrapper_class
18
- end
19
-
20
- def wrapper_error_class
21
- options[:wrapper_error_class] || SimpleForm.wrapper_error_class
22
- end
23
-
24
- def wrapper_html_options
25
- css_classes = input_html_classes.unshift(wrapper_class)
26
- css_classes << wrapper_error_class if has_errors?
27
- css_classes << disabled_class if disabled?
28
- html_options_for(:wrapper, css_classes)
29
- end
30
-
31
- private
32
-
33
- def disabled_class
34
- 'disabled'
35
- end
36
- end
37
- end
38
- end
@@ -1,15 +0,0 @@
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
@@ -1,24 +0,0 @@
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
@@ -1,28 +0,0 @@
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
data/simple_form.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "simple_form/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "simple_form"
7
- s.version = SimpleForm::VERSION.dup
8
- s.platform = Gem::Platform::RUBY
9
- s.summary = "Forms made easy!"
10
- s.email = "contact@plataformatec.com.br"
11
- s.homepage = "http://github.com/plataformatec/simple_form"
12
- s.description = "Forms made easy!"
13
- s.authors = ['José Valim', 'Carlos Antônio']
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.test_files -= Dir["test/support/country_select/**/*"]
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
20
-
21
- s.rubyforge_project = "simple_form"
22
-
23
- s.add_dependency('activemodel', '~> 3.0')
24
- s.add_dependency('actionpack', '~> 3.0')
25
- end
@@ -1,56 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ErrorTest < ActionView::TestCase
4
-
5
- def with_error_for(object, attribute_name, type, options={}, &block)
6
- with_concat_form_for(object) do |f|
7
- options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
8
- SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).error.to_s
9
- end
10
- end
11
-
12
- test 'error should not generate content for attribute without errors' do
13
- with_error_for @user, :active, :boolean
14
- assert_no_select 'span.error'
15
- end
16
-
17
- test 'error should not generate messages when object is not present' do
18
- with_error_for :project, :name, :string
19
- assert_no_select 'span.error'
20
- end
21
-
22
- test "error should not generate messages when object doesn't respond to errors method" do
23
- @user.instance_eval { undef errors }
24
- with_error_for @user, :name, :string
25
- assert_no_select 'span.error'
26
- end
27
-
28
- test 'error should generate messages for attribute with single error' do
29
- with_error_for @user, :name, :string
30
- assert_select 'span.error', "can't be blank"
31
- end
32
-
33
- test 'error should generate messages for attribute with one error when using first' do
34
- swap SimpleForm, :error_method => :first do
35
- with_error_for @user, :age, :numeric
36
- assert_select 'span.error', 'is not a number'
37
- end
38
- end
39
-
40
- test 'error should generate messages for attribute with several errors when using to_sentence' do
41
- swap SimpleForm, :error_method => :to_sentence do
42
- with_error_for @user, :age, :numeric
43
- assert_select 'span.error', 'is not a number and must be greater than 18'
44
- end
45
- end
46
-
47
- test 'error should be able to pass html options' do
48
- with_error_for @user, :name, :string, :error_html => { :id => 'error', :class => 'yay' }
49
- assert_select 'span#error.error.yay'
50
- end
51
-
52
- test 'error should find errors on attribute and association' do
53
- with_error_for @user, :company_id, :select, :setup_association => true, :error_method => :to_sentence
54
- assert_select 'span.error', 'must be valid and company must be present'
55
- end
56
- end
@@ -1,74 +0,0 @@
1
- require 'test_helper'
2
-
3
- class HintTest < ActionView::TestCase
4
-
5
- def with_hint_for(object, attribute_name, type, options={}, &block)
6
- with_concat_form_for(object) do |f|
7
- options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
8
- SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).hint.to_s
9
- end
10
- end
11
-
12
- test 'hint should not be generated by default' do
13
- with_hint_for @user, :name, :string
14
- assert_no_select 'span.hint'
15
- end
16
-
17
- test 'hint should be generated with input text' do
18
- with_hint_for @user, :name, :string, :hint => 'Use with care...'
19
- assert_select 'span.hint', 'Use with care...'
20
- end
21
-
22
- test 'hint uses the current component tag set' do
23
- swap SimpleForm, :hint_tag => :p do
24
- with_hint_for @user, :name, :string, :hint => 'Use with care...'
25
- assert_select 'p.hint', 'Use with care...'
26
- end
27
- end
28
-
29
- test 'hint should use i18n based on model, action, and attribute to lookup translation' do
30
- store_translations(:en, :simple_form => { :hints => { :user => {
31
- :edit => { :name => 'Content of this input will be truncated...' }
32
- } } }) do
33
- with_hint_for @user, :name, :string
34
- assert_select 'span.hint', 'Content of this input will be truncated...'
35
- end
36
- end
37
-
38
- test 'hint should use i18n with model and attribute to lookup translation' do
39
- store_translations(:en, :simple_form => { :hints => { :user => {
40
- :name => 'Content of this input will be capitalized...'
41
- } } }) do
42
- with_hint_for @user, :name, :string
43
- assert_select 'span.hint', 'Content of this input will be capitalized...'
44
- end
45
- end
46
-
47
- test 'hint should use i18n just with attribute to lookup translation' do
48
- store_translations(:en, :simple_form => { :hints => {
49
- :name => 'Content of this input will be downcased...'
50
- } }) do
51
- with_hint_for @user, :name, :string
52
- assert_select 'span.hint', 'Content of this input will be downcased...'
53
- end
54
- end
55
-
56
- test 'hint should use i18n with lookup for association name' do
57
- store_translations(:en, :simple_form => { :hints => {
58
- :user => { :company => 'My company!' }
59
- } } ) do
60
- with_hint_for @user, :company_id, :string, :setup_association => true
61
- assert_select 'span.hint', /My company!/
62
- end
63
- end
64
-
65
- test 'hint should generate properly when object is not present' do
66
- with_hint_for :project, :name, :string, :hint => 'Test without object'
67
- assert_select 'span.hint', 'Test without object'
68
- end
69
-
70
- test 'hint should be able to pass html options' do
71
- with_hint_for @user, :name, :string, :hint => 'Yay!', :hint_html => { :id => 'hint', :class => 'yay' }
72
- assert_select 'span#hint.hint.yay'
73
- end
74
- end
@@ -1,63 +0,0 @@
1
- require 'test_helper'
2
-
3
- class WrapperTest < ActionView::TestCase
4
- def with_error_for(object, attribute_name, &block)
5
- with_concat_form_for(object) do |f|
6
- f.input attribute_name
7
- end
8
- end
9
-
10
- def with_form_for(object, *args, &block)
11
- with_concat_form_for(object) do |f|
12
- f.input(*args, &block)
13
- end
14
- end
15
-
16
- test 'wrapper should not have error class for attribute without errors' do
17
- with_error_for @user, :active
18
- assert_no_select 'div.field_with_errors'
19
- end
20
-
21
- test 'wrapper should not have error class when object is not present' do
22
- with_error_for :project, :name
23
- assert_no_select 'div.field_with_errors'
24
- end
25
-
26
- test 'wrapper should add error class for attribute with errors' do
27
- with_error_for @user, :name
28
- assert_select 'div.field_with_errors'
29
- end
30
-
31
- test 'wrapper should add chosen error class for attribute with errors' do
32
- swap SimpleForm, :wrapper_error_class => "omgError" do
33
- with_error_for @user, :name
34
- assert_select 'div.omgError'
35
- end
36
- end
37
-
38
- test 'wrapper should add chosen wrapper class' do
39
- swap SimpleForm, :wrapper_class => "wrapper" do
40
- with_form_for @user, :active
41
- assert_select 'div.wrapper'
42
- assert_no_select 'div.input'
43
-
44
- with_form_for @user, :name
45
- assert_select 'div.wrapper'
46
- assert_no_select 'div.input'
47
-
48
- with_form_for :project, :name
49
- assert_select 'div.wrapper'
50
- assert_no_select 'div.input'
51
- end
52
- end
53
-
54
- test 'wrapper should not have disabled class by default' do
55
- with_form_for @user, :active
56
- assert_no_select 'div.disabled'
57
- end
58
-
59
- test 'wrapper should add disabled class when the input is disabled' do
60
- with_form_for @user, :active, :disabled => true
61
- assert_select 'div.disabled'
62
- end
63
- end