formtastic 1.2.4 → 3.1.5
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +46 -0
- data/.yardopts +1 -0
- data/Appraisals +43 -0
- data/CHANGELOG +54 -0
- data/DEPRECATIONS +52 -0
- data/Gemfile +3 -0
- data/README.md +629 -0
- data/RELEASE_PROCESS +6 -0
- data/Rakefile +35 -0
- data/app/assets/stylesheets/formtastic.css +289 -0
- data/app/assets/stylesheets/formtastic_ie6.css +33 -0
- data/app/assets/stylesheets/formtastic_ie7.css +23 -0
- data/formtastic.gemspec +42 -0
- data/gemfiles/rails_3.2.gemfile +9 -0
- data/gemfiles/rails_4.0.4.gemfile +8 -0
- data/gemfiles/rails_4.1.gemfile +8 -0
- data/gemfiles/rails_4.2.gemfile +8 -0
- data/gemfiles/rails_4.gemfile +8 -0
- data/gemfiles/rails_5.0.gemfile +8 -0
- data/gemfiles/rails_edge.gemfile +15 -0
- data/lib/formtastic.rb +40 -1945
- data/lib/formtastic/action_class_finder.rb +18 -0
- data/lib/formtastic/actions.rb +11 -0
- data/lib/formtastic/actions/base.rb +156 -0
- data/lib/formtastic/actions/button_action.rb +67 -0
- data/lib/formtastic/actions/buttonish.rb +17 -0
- data/lib/formtastic/actions/input_action.rb +70 -0
- data/lib/formtastic/actions/link_action.rb +88 -0
- data/lib/formtastic/deprecation.rb +42 -0
- data/lib/formtastic/engine.rb +11 -0
- data/lib/formtastic/form_builder.rb +124 -0
- data/lib/formtastic/helpers.rb +16 -0
- data/lib/formtastic/helpers/action_helper.rb +162 -0
- data/lib/formtastic/helpers/actions_helper.rb +168 -0
- data/lib/formtastic/helpers/enum.rb +13 -0
- data/lib/formtastic/helpers/errors_helper.rb +81 -0
- data/lib/formtastic/helpers/fieldset_wrapper.rb +80 -0
- data/lib/formtastic/helpers/file_column_detection.rb +16 -0
- data/lib/formtastic/helpers/form_helper.rb +203 -0
- data/lib/formtastic/helpers/input_helper.rb +407 -0
- data/lib/formtastic/helpers/inputs_helper.rb +411 -0
- data/lib/formtastic/helpers/reflection.rb +37 -0
- data/lib/formtastic/html_attributes.rb +32 -0
- data/lib/formtastic/i18n.rb +4 -2
- data/lib/formtastic/input_class_finder.rb +18 -0
- data/lib/formtastic/inputs.rb +39 -0
- data/lib/formtastic/inputs/base.rb +76 -0
- data/lib/formtastic/inputs/base/associations.rb +31 -0
- data/lib/formtastic/inputs/base/choices.rb +108 -0
- data/lib/formtastic/inputs/base/collections.rb +159 -0
- data/lib/formtastic/inputs/base/database.rb +22 -0
- data/lib/formtastic/inputs/base/datetime_pickerish.rb +85 -0
- data/lib/formtastic/inputs/base/errors.rb +58 -0
- data/lib/formtastic/inputs/base/fileish.rb +23 -0
- data/lib/formtastic/inputs/base/hints.rb +31 -0
- data/lib/formtastic/inputs/base/html.rb +53 -0
- data/lib/formtastic/inputs/base/labelling.rb +52 -0
- data/lib/formtastic/inputs/base/naming.rb +42 -0
- data/lib/formtastic/inputs/base/numeric.rb +50 -0
- data/lib/formtastic/inputs/base/options.rb +17 -0
- data/lib/formtastic/inputs/base/placeholder.rb +17 -0
- data/lib/formtastic/inputs/base/stringish.rb +38 -0
- data/lib/formtastic/inputs/base/timeish.rb +241 -0
- data/lib/formtastic/inputs/base/validations.rb +215 -0
- data/lib/formtastic/inputs/base/wrapping.rb +50 -0
- data/lib/formtastic/inputs/boolean_input.rb +118 -0
- data/lib/formtastic/inputs/check_boxes_input.rb +197 -0
- data/lib/formtastic/inputs/color_input.rb +42 -0
- data/lib/formtastic/inputs/country_input.rb +86 -0
- data/lib/formtastic/inputs/datalist_input.rb +41 -0
- data/lib/formtastic/inputs/date_picker_input.rb +93 -0
- data/lib/formtastic/inputs/date_select_input.rb +34 -0
- data/lib/formtastic/inputs/datetime_picker_input.rb +103 -0
- data/lib/formtastic/inputs/datetime_select_input.rb +12 -0
- data/lib/formtastic/inputs/email_input.rb +41 -0
- data/lib/formtastic/inputs/file_input.rb +42 -0
- data/lib/formtastic/inputs/hidden_input.rb +62 -0
- data/lib/formtastic/inputs/number_input.rb +88 -0
- data/lib/formtastic/inputs/password_input.rb +41 -0
- data/lib/formtastic/inputs/phone_input.rb +42 -0
- data/lib/formtastic/inputs/radio_input.rb +163 -0
- data/lib/formtastic/inputs/range_input.rb +95 -0
- data/lib/formtastic/inputs/search_input.rb +41 -0
- data/lib/formtastic/inputs/select_input.rb +235 -0
- data/lib/formtastic/inputs/string_input.rb +36 -0
- data/lib/formtastic/inputs/text_input.rb +48 -0
- data/lib/formtastic/inputs/time_picker_input.rb +99 -0
- data/lib/formtastic/inputs/time_select_input.rb +38 -0
- data/lib/formtastic/inputs/time_zone_input.rb +58 -0
- data/lib/formtastic/inputs/url_input.rb +41 -0
- data/lib/formtastic/localized_string.rb +17 -0
- data/lib/formtastic/localizer.rb +152 -0
- data/lib/formtastic/namespaced_class_finder.rb +99 -0
- data/lib/formtastic/util.rb +35 -16
- data/lib/formtastic/version.rb +3 -0
- data/lib/generators/formtastic/form/form_generator.rb +64 -37
- data/lib/generators/formtastic/input/input_generator.rb +46 -0
- data/lib/generators/formtastic/install/install_generator.rb +13 -5
- data/lib/generators/templates/_form.html.erb +10 -4
- data/lib/generators/templates/_form.html.haml +8 -4
- data/lib/generators/templates/_form.html.slim +8 -0
- data/lib/generators/templates/formtastic.rb +77 -44
- data/lib/generators/templates/input.rb +19 -0
- data/lib/locale/en.yml +3 -0
- data/sample/basic_inputs.html +224 -0
- data/sample/config.ru +69 -0
- data/sample/index.html +14 -0
- data/spec/action_class_finder_spec.rb +12 -0
- data/spec/actions/button_action_spec.rb +63 -0
- data/spec/actions/generic_action_spec.rb +521 -0
- data/spec/actions/input_action_spec.rb +59 -0
- data/spec/actions/link_action_spec.rb +92 -0
- data/spec/builder/custom_builder_spec.rb +116 -0
- data/spec/builder/error_proc_spec.rb +27 -0
- data/spec/builder/semantic_fields_for_spec.rb +142 -0
- data/spec/fast_spec_helper.rb +12 -0
- data/spec/generators/formtastic/form/form_generator_spec.rb +131 -0
- data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
- data/spec/generators/formtastic/install/install_generator_spec.rb +47 -0
- data/spec/helpers/action_helper_spec.rb +19 -0
- data/spec/helpers/actions_helper_spec.rb +143 -0
- data/spec/helpers/form_helper_spec.rb +218 -0
- data/spec/helpers/input_helper_spec.rb +6 -0
- data/spec/helpers/inputs_helper_spec.rb +655 -0
- data/spec/helpers/namespaced_action_helper_spec.rb +43 -0
- data/spec/helpers/namespaced_input_helper_spec.rb +36 -0
- data/spec/helpers/reflection_helper_spec.rb +32 -0
- data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
- data/spec/i18n_spec.rb +210 -0
- data/spec/input_class_finder_spec.rb +10 -0
- data/spec/inputs/base/collections_spec.rb +76 -0
- data/spec/inputs/base/validations_spec.rb +342 -0
- data/spec/inputs/boolean_input_spec.rb +254 -0
- data/spec/inputs/check_boxes_input_spec.rb +546 -0
- data/spec/inputs/color_input_spec.rb +97 -0
- data/spec/inputs/country_input_spec.rb +133 -0
- data/spec/inputs/custom_input_spec.rb +55 -0
- data/spec/inputs/datalist_input_spec.rb +61 -0
- data/spec/inputs/date_picker_input_spec.rb +449 -0
- data/spec/inputs/date_select_input_spec.rb +235 -0
- data/spec/inputs/datetime_picker_input_spec.rb +490 -0
- data/spec/inputs/datetime_select_input_spec.rb +193 -0
- data/spec/inputs/email_input_spec.rb +85 -0
- data/spec/inputs/file_input_spec.rb +89 -0
- data/spec/inputs/hidden_input_spec.rb +135 -0
- data/spec/inputs/include_blank_spec.rb +78 -0
- data/spec/inputs/label_spec.rb +149 -0
- data/spec/inputs/number_input_spec.rb +815 -0
- data/spec/inputs/password_input_spec.rb +99 -0
- data/spec/inputs/phone_input_spec.rb +85 -0
- data/spec/inputs/placeholder_spec.rb +71 -0
- data/spec/inputs/radio_input_spec.rb +328 -0
- data/spec/inputs/range_input_spec.rb +505 -0
- data/spec/inputs/readonly_spec.rb +50 -0
- data/spec/inputs/search_input_spec.rb +84 -0
- data/spec/inputs/select_input_spec.rb +615 -0
- data/spec/inputs/string_input_spec.rb +260 -0
- data/spec/inputs/text_input_spec.rb +187 -0
- data/spec/inputs/time_picker_input_spec.rb +455 -0
- data/spec/inputs/time_select_input_spec.rb +248 -0
- data/spec/inputs/time_zone_input_spec.rb +143 -0
- data/spec/inputs/url_input_spec.rb +85 -0
- data/spec/inputs/with_options_spec.rb +43 -0
- data/spec/localizer_spec.rb +130 -0
- data/spec/namespaced_class_finder_spec.rb +79 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +525 -0
- data/spec/support/custom_macros.rb +564 -0
- data/spec/support/deprecation.rb +6 -0
- data/spec/support/shared_examples.rb +1313 -0
- data/spec/support/specialized_class_finder_shared_example.rb +27 -0
- data/spec/support/test_environment.rb +31 -0
- data/spec/util_spec.rb +66 -0
- metadata +434 -161
- data/README.textile +0 -682
- data/generators/form/USAGE +0 -16
- data/generators/form/form_generator.rb +0 -111
- data/generators/formtastic/formtastic_generator.rb +0 -26
- data/init.rb +0 -5
- data/lib/formtastic/layout_helper.rb +0 -12
- data/lib/formtastic/railtie.rb +0 -14
- data/lib/generators/templates/formtastic.css +0 -145
- data/lib/generators/templates/formtastic_changes.css +0 -14
- data/lib/generators/templates/rails2/_form.html.erb +0 -5
- data/lib/generators/templates/rails2/_form.html.haml +0 -4
- data/rails/init.rb +0 -2
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe 'InputAction', 'when submitting' do
|
|
5
|
+
|
|
6
|
+
include FormtasticSpecHelper
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@output_buffer = ''
|
|
10
|
+
mock_everything
|
|
11
|
+
|
|
12
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
13
|
+
concat(builder.action(:submit, :as => :input))
|
|
14
|
+
end)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'should render a submit type of input' do
|
|
18
|
+
output_buffer.should have_tag('li.action.input_action input[@type="submit"]')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
describe 'InputAction', 'when resetting' do
|
|
24
|
+
|
|
25
|
+
include FormtasticSpecHelper
|
|
26
|
+
|
|
27
|
+
before do
|
|
28
|
+
@output_buffer = ''
|
|
29
|
+
mock_everything
|
|
30
|
+
|
|
31
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
32
|
+
concat(builder.action(:reset, :as => :input))
|
|
33
|
+
end)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'should render a reset type of input' do
|
|
37
|
+
output_buffer.should have_tag('li.action.input_action input[@type="reset"]')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe 'InputAction', 'when cancelling' do
|
|
43
|
+
|
|
44
|
+
include FormtasticSpecHelper
|
|
45
|
+
|
|
46
|
+
before do
|
|
47
|
+
@output_buffer = ''
|
|
48
|
+
mock_everything
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'should raise an error' do
|
|
52
|
+
lambda {
|
|
53
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
54
|
+
concat(builder.action(:cancel, :as => :input))
|
|
55
|
+
end)
|
|
56
|
+
}.should raise_error(Formtastic::UnsupportedMethodForAction)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe 'LinkAction', 'when cancelling' do
|
|
5
|
+
|
|
6
|
+
include FormtasticSpecHelper
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@output_buffer = ''
|
|
10
|
+
mock_everything
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context 'without a :url' do
|
|
14
|
+
before do
|
|
15
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
16
|
+
concat(builder.action(:cancel, :as => :link))
|
|
17
|
+
end)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'should render a submit type of input' do
|
|
21
|
+
output_buffer.should have_tag('li.action.link_action a[@href="javascript:history.back()"]')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context 'with a :url as String' do
|
|
27
|
+
|
|
28
|
+
before do
|
|
29
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
30
|
+
concat(builder.action(:cancel, :as => :link, :url => "http://foo.bah/baz"))
|
|
31
|
+
end)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'should render a submit type of input' do
|
|
35
|
+
output_buffer.should have_tag('li.action.link_action a[@href="http://foo.bah/baz"]')
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context 'with a :url as Hash' do
|
|
41
|
+
|
|
42
|
+
before do
|
|
43
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
44
|
+
concat(builder.action(:cancel, :as => :link, :url => { :action => "foo" }))
|
|
45
|
+
end)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'should render a submit type of input' do
|
|
49
|
+
output_buffer.should have_tag('li.action.link_action a[@href="/mock/path"]')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe 'LinkAction', 'when submitting' do
|
|
57
|
+
|
|
58
|
+
include FormtasticSpecHelper
|
|
59
|
+
|
|
60
|
+
before do
|
|
61
|
+
@output_buffer = ''
|
|
62
|
+
mock_everything
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'should raise an error' do
|
|
66
|
+
lambda {
|
|
67
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
68
|
+
concat(builder.action(:submit, :as => :link))
|
|
69
|
+
end)
|
|
70
|
+
}.should raise_error(Formtastic::UnsupportedMethodForAction)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
describe 'LinkAction', 'when submitting' do
|
|
76
|
+
|
|
77
|
+
include FormtasticSpecHelper
|
|
78
|
+
|
|
79
|
+
before do
|
|
80
|
+
@output_buffer = ''
|
|
81
|
+
mock_everything
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it 'should raise an error' do
|
|
85
|
+
lambda {
|
|
86
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
87
|
+
concat(builder.action(:reset, :as => :link))
|
|
88
|
+
end)
|
|
89
|
+
}.should raise_error(Formtastic::UnsupportedMethodForAction)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe 'Formtastic::Helpers::FormHelper.builder' do
|
|
5
|
+
|
|
6
|
+
include FormtasticSpecHelper
|
|
7
|
+
|
|
8
|
+
class MyCustomFormBuilder < Formtastic::FormBuilder
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# TODO should be a separate spec for custom inputs
|
|
12
|
+
class Formtastic::Inputs::AwesomeInput
|
|
13
|
+
include Formtastic::Inputs::Base
|
|
14
|
+
def to_html
|
|
15
|
+
"Awesome!"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
before do
|
|
20
|
+
@output_buffer = ''
|
|
21
|
+
mock_everything
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'is the Formtastic::FormBuilder by default' do
|
|
25
|
+
Formtastic::Helpers::FormHelper.builder.should == Formtastic::FormBuilder
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'can be configured to use your own custom form builder' do
|
|
29
|
+
# Set it to a custom builder class
|
|
30
|
+
Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
|
|
31
|
+
Formtastic::Helpers::FormHelper.builder.should == MyCustomFormBuilder
|
|
32
|
+
|
|
33
|
+
# Reset it to the default
|
|
34
|
+
Formtastic::Helpers::FormHelper.builder = Formtastic::FormBuilder
|
|
35
|
+
Formtastic::Helpers::FormHelper.builder.should == Formtastic::FormBuilder
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'should allow custom settings per form builder subclass' do
|
|
39
|
+
with_config(:all_fields_required_by_default, true) do
|
|
40
|
+
MyCustomFormBuilder.all_fields_required_by_default = false
|
|
41
|
+
|
|
42
|
+
MyCustomFormBuilder.all_fields_required_by_default.should be_falsey
|
|
43
|
+
Formtastic::FormBuilder.all_fields_required_by_default.should be_truthy
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe "when using a custom builder" do
|
|
48
|
+
|
|
49
|
+
before do
|
|
50
|
+
@new_post.stub(:title)
|
|
51
|
+
Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
after do
|
|
55
|
+
Formtastic::Helpers::FormHelper.builder = Formtastic::FormBuilder
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe "semantic_form_for" do
|
|
59
|
+
|
|
60
|
+
it "should yield an instance of the custom builder" do
|
|
61
|
+
semantic_form_for(@new_post) do |builder|
|
|
62
|
+
builder.class.should.kind_of?(MyCustomFormBuilder)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# TODO should be a separate spec for custom inputs
|
|
67
|
+
it "should allow me to call my custom input" do
|
|
68
|
+
semantic_form_for(@new_post) do |builder|
|
|
69
|
+
concat(builder.input(:title, :as => :awesome))
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# See: https://github.com/justinfrench/formtastic/issues/657
|
|
74
|
+
it "should not conflict with navigasmic" do
|
|
75
|
+
self.class.any_instance.stub(:builder).and_return('navigasmic')
|
|
76
|
+
|
|
77
|
+
lambda { semantic_form_for(@new_post) { |f| } }.should_not raise_error
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
describe "fields_for" do
|
|
83
|
+
|
|
84
|
+
it "should yield an instance of the parent form builder" do
|
|
85
|
+
@new_post.stub(:comment).and_return([@fred])
|
|
86
|
+
@new_post.stub(:comment_attributes=)
|
|
87
|
+
semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
|
|
88
|
+
builder.class.should.kind_of?(MyCustomFormBuilder)
|
|
89
|
+
|
|
90
|
+
builder.fields_for(:comment) do |nested_builder|
|
|
91
|
+
nested_builder.class.should.kind_of?(MyCustomFormBuilder)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe "when using a builder passed to form options" do
|
|
101
|
+
|
|
102
|
+
describe "fields_for" do
|
|
103
|
+
|
|
104
|
+
it "should yield an instance of the parent form builder" do
|
|
105
|
+
@new_post.stub(:author_attributes=)
|
|
106
|
+
semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
|
|
107
|
+
builder.fields_for(:author) do |nested_builder|
|
|
108
|
+
nested_builder.class.should.kind_of?(MyCustomFormBuilder)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe 'Rails field_error_proc' do
|
|
5
|
+
|
|
6
|
+
include FormtasticSpecHelper
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@output_buffer = ''
|
|
10
|
+
mock_everything
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should not be overridden globally for all form builders" do
|
|
14
|
+
current_field_error_proc = ::ActionView::Base.field_error_proc
|
|
15
|
+
|
|
16
|
+
semantic_form_for(@new_post) do |builder|
|
|
17
|
+
::ActionView::Base.field_error_proc.should_not == current_field_error_proc
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
::ActionView::Base.field_error_proc.should == current_field_error_proc
|
|
21
|
+
|
|
22
|
+
form_for(@new_post) do |builder|
|
|
23
|
+
::ActionView::Base.field_error_proc.should == current_field_error_proc
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe 'Formtastic::FormBuilder#fields_for' do
|
|
5
|
+
|
|
6
|
+
include FormtasticSpecHelper
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@output_buffer = ''
|
|
10
|
+
mock_everything
|
|
11
|
+
@new_post.stub(:author).and_return(::Author.new)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context 'outside a form_for block' do
|
|
15
|
+
it 'yields an instance of FormHelper.builder' do
|
|
16
|
+
semantic_fields_for(@new_post) do |nested_builder|
|
|
17
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
18
|
+
end
|
|
19
|
+
semantic_fields_for(@new_post.author) do |nested_builder|
|
|
20
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
21
|
+
end
|
|
22
|
+
semantic_fields_for(:author, @new_post.author) do |nested_builder|
|
|
23
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
24
|
+
end
|
|
25
|
+
semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
|
26
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'should respond to input' do
|
|
31
|
+
semantic_fields_for(@new_post) do |nested_builder|
|
|
32
|
+
nested_builder.respond_to?(:input).should be_truthy
|
|
33
|
+
end
|
|
34
|
+
semantic_fields_for(@new_post.author) do |nested_builder|
|
|
35
|
+
nested_builder.respond_to?(:input).should be_truthy
|
|
36
|
+
end
|
|
37
|
+
semantic_fields_for(:author, @new_post.author) do |nested_builder|
|
|
38
|
+
nested_builder.respond_to?(:input).should be_truthy
|
|
39
|
+
end
|
|
40
|
+
semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
|
41
|
+
nested_builder.respond_to?(:input).should be_truthy
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'within a form_for block' do
|
|
47
|
+
it 'yields an instance of FormHelper.builder' do
|
|
48
|
+
semantic_form_for(@new_post) do |builder|
|
|
49
|
+
builder.semantic_fields_for(:author) do |nested_builder|
|
|
50
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'yields an instance of FormHelper.builder with hash-like model' do
|
|
56
|
+
semantic_form_for(:user) do |builder|
|
|
57
|
+
builder.semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
|
58
|
+
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'nests the object name' do
|
|
64
|
+
semantic_form_for(@new_post) do |builder|
|
|
65
|
+
builder.semantic_fields_for(@bob) do |nested_builder|
|
|
66
|
+
nested_builder.object_name.should == 'post[author]'
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it 'supports passing collection as second parameter' do
|
|
72
|
+
semantic_form_for(@new_post) do |builder|
|
|
73
|
+
builder.semantic_fields_for(:author, [@fred,@bob]) do |nested_builder|
|
|
74
|
+
nested_builder.object_name.should =~ /post\[author_attributes\]\[\d+\]/
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'should sanitize html id for li tag' do
|
|
80
|
+
@bob.stub(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
|
|
81
|
+
concat(semantic_form_for(@new_post) do |builder|
|
|
82
|
+
concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
|
|
83
|
+
concat(nested_builder.inputs(:login))
|
|
84
|
+
end)
|
|
85
|
+
end)
|
|
86
|
+
output_buffer.should have_tag('form fieldset.inputs #post_author_1_login_input')
|
|
87
|
+
# Not valid selector, so using good ol' regex
|
|
88
|
+
output_buffer.should_not =~ /id="post\[author\]_1_login_input"/
|
|
89
|
+
# <=> output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'should use namespace provided in nested fields' do
|
|
93
|
+
@bob.stub(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
|
|
94
|
+
concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
|
|
95
|
+
concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
|
|
96
|
+
concat(nested_builder.inputs(:login))
|
|
97
|
+
end)
|
|
98
|
+
end)
|
|
99
|
+
output_buffer.should have_tag('form fieldset.inputs #context2_post_author_1_login_input')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it 'should render errors on the nested inputs' do
|
|
103
|
+
@errors = double('errors')
|
|
104
|
+
@errors.stub(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
|
|
105
|
+
@bob.stub(:errors).and_return(@errors)
|
|
106
|
+
|
|
107
|
+
concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
|
|
108
|
+
concat(builder.semantic_fields_for(@bob) do |nested_builder|
|
|
109
|
+
concat(nested_builder.inputs(:login))
|
|
110
|
+
end)
|
|
111
|
+
end)
|
|
112
|
+
output_buffer.should =~ /oh noes/
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
context "when I rendered my own hidden id input" do
|
|
118
|
+
|
|
119
|
+
before do
|
|
120
|
+
output_buffer.replace ''
|
|
121
|
+
|
|
122
|
+
@fred.posts.size.should == 1
|
|
123
|
+
@fred.posts.first.stub(:persisted?).and_return(true)
|
|
124
|
+
@fred.stub(:posts_attributes=)
|
|
125
|
+
concat(semantic_form_for(@fred) do |builder|
|
|
126
|
+
concat(builder.semantic_fields_for(:posts) do |nested_builder|
|
|
127
|
+
concat(nested_builder.input(:id, :as => :hidden))
|
|
128
|
+
concat(nested_builder.input(:title))
|
|
129
|
+
end)
|
|
130
|
+
end)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it "should only render one hidden input (my one)" do
|
|
134
|
+
output_buffer.should have_tag 'input#author_posts_attributes_0_id', :count => 1
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it "should render the hidden input inside an li.hidden" do
|
|
138
|
+
output_buffer.should have_tag 'li.hidden input#author_posts_attributes_0_id'
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
$LOAD_PATH << 'lib/formtastic'
|
|
3
|
+
require 'active_support/all'
|
|
4
|
+
require 'localized_string'
|
|
5
|
+
require 'inputs'
|
|
6
|
+
require 'helpers'
|
|
7
|
+
|
|
8
|
+
class MyInput
|
|
9
|
+
include Formtastic::Inputs::Base
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales)
|