formtastic 3.1.3 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +1 -0
- data/.github/workflows/test.yml +61 -0
- data/.gitignore +3 -2
- data/CHANGELOG.md +61 -0
- data/Gemfile.lock +140 -0
- data/MIT-LICENSE +1 -1
- data/{README.textile → README.md} +183 -167
- data/RELEASE_PROCESS +3 -1
- data/Rakefile +20 -1
- data/app/assets/stylesheets/formtastic.css +1 -1
- data/bin/appraisal +8 -0
- data/formtastic.gemspec +12 -16
- data/gemfiles/rails_6.0/Gemfile +5 -0
- data/gemfiles/rails_6.1/Gemfile +5 -0
- data/gemfiles/rails_7.0/Gemfile +5 -0
- data/gemfiles/rails_7.1/Gemfile +5 -0
- data/gemfiles/rails_edge/Gemfile +13 -0
- data/lib/formtastic/action_class_finder.rb +1 -0
- data/lib/formtastic/actions/base.rb +1 -0
- data/lib/formtastic/actions/button_action.rb +1 -0
- data/lib/formtastic/actions/buttonish.rb +1 -0
- data/lib/formtastic/actions/input_action.rb +1 -0
- data/lib/formtastic/actions/link_action.rb +1 -0
- data/lib/formtastic/actions.rb +7 -3
- data/lib/formtastic/deprecation.rb +2 -38
- data/lib/formtastic/engine.rb +4 -1
- data/lib/formtastic/form_builder.rb +12 -24
- data/lib/formtastic/helpers/action_helper.rb +2 -48
- data/lib/formtastic/helpers/actions_helper.rb +1 -0
- data/lib/formtastic/helpers/enum.rb +14 -0
- data/lib/formtastic/helpers/errors_helper.rb +3 -2
- data/lib/formtastic/helpers/fieldset_wrapper.rb +14 -9
- data/lib/formtastic/helpers/file_column_detection.rb +1 -0
- data/lib/formtastic/helpers/form_helper.rb +2 -1
- data/lib/formtastic/helpers/input_helper.rb +20 -76
- data/lib/formtastic/helpers/inputs_helper.rb +29 -23
- data/lib/formtastic/helpers/reflection.rb +1 -0
- data/lib/formtastic/helpers.rb +2 -2
- data/lib/formtastic/html_attributes.rb +1 -0
- data/lib/formtastic/i18n.rb +2 -1
- data/lib/formtastic/input_class_finder.rb +1 -0
- data/lib/formtastic/inputs/base/associations.rb +1 -0
- data/lib/formtastic/inputs/base/choices.rb +3 -2
- data/lib/formtastic/inputs/base/collections.rb +46 -10
- data/lib/formtastic/inputs/base/database.rb +5 -7
- data/lib/formtastic/inputs/base/datetime_pickerish.rb +1 -0
- data/lib/formtastic/inputs/base/errors.rb +7 -6
- data/lib/formtastic/inputs/base/fileish.rb +1 -0
- data/lib/formtastic/inputs/base/hints.rb +2 -1
- data/lib/formtastic/inputs/base/html.rb +9 -7
- data/lib/formtastic/inputs/base/labelling.rb +3 -2
- data/lib/formtastic/inputs/base/naming.rb +5 -4
- data/lib/formtastic/inputs/base/numeric.rb +1 -0
- data/lib/formtastic/inputs/base/options.rb +3 -3
- data/lib/formtastic/inputs/base/placeholder.rb +1 -0
- data/lib/formtastic/inputs/base/stringish.rb +1 -0
- data/lib/formtastic/inputs/base/timeish.rb +9 -4
- data/lib/formtastic/inputs/base/validations.rb +39 -12
- data/lib/formtastic/inputs/base/wrapping.rb +1 -0
- data/lib/formtastic/inputs/base.rb +3 -2
- data/lib/formtastic/inputs/boolean_input.rb +2 -1
- data/lib/formtastic/inputs/check_boxes_input.rb +15 -6
- data/lib/formtastic/inputs/color_input.rb +1 -1
- data/lib/formtastic/inputs/country_input.rb +4 -1
- data/lib/formtastic/inputs/datalist_input.rb +1 -0
- data/lib/formtastic/inputs/date_picker_input.rb +1 -0
- data/lib/formtastic/inputs/date_select_input.rb +1 -0
- data/lib/formtastic/inputs/datetime_picker_input.rb +1 -0
- data/lib/formtastic/inputs/datetime_select_input.rb +1 -0
- data/lib/formtastic/inputs/email_input.rb +1 -0
- data/lib/formtastic/inputs/file_input.rb +1 -0
- data/lib/formtastic/inputs/hidden_input.rb +3 -2
- data/lib/formtastic/inputs/number_input.rb +1 -0
- data/lib/formtastic/inputs/password_input.rb +1 -0
- data/lib/formtastic/inputs/phone_input.rb +1 -0
- data/lib/formtastic/inputs/radio_input.rb +21 -0
- data/lib/formtastic/inputs/range_input.rb +1 -0
- data/lib/formtastic/inputs/search_input.rb +1 -0
- data/lib/formtastic/inputs/select_input.rb +30 -1
- data/lib/formtastic/inputs/string_input.rb +1 -0
- data/lib/formtastic/inputs/text_input.rb +1 -0
- data/lib/formtastic/inputs/time_picker_input.rb +1 -0
- data/lib/formtastic/inputs/time_select_input.rb +1 -0
- data/lib/formtastic/inputs/time_zone_input.rb +17 -6
- data/lib/formtastic/inputs/url_input.rb +1 -0
- data/lib/formtastic/inputs.rb +33 -29
- data/lib/formtastic/localized_string.rb +1 -0
- data/lib/formtastic/localizer.rb +21 -22
- data/lib/formtastic/namespaced_class_finder.rb +8 -9
- data/lib/formtastic/version.rb +2 -1
- data/lib/formtastic.rb +10 -11
- data/lib/generators/formtastic/form/form_generator.rb +2 -1
- data/lib/generators/formtastic/input/input_generator.rb +47 -0
- data/lib/generators/formtastic/install/install_generator.rb +1 -0
- data/lib/generators/templates/formtastic.rb +15 -13
- data/lib/generators/templates/input.rb +19 -0
- data/sample/basic_inputs.html +1 -1
- data/script/integration-template.rb +73 -0
- data/script/integration.sh +19 -0
- data/spec/action_class_finder_spec.rb +2 -1
- data/spec/actions/button_action_spec.rb +21 -20
- data/spec/actions/generic_action_spec.rb +134 -133
- data/spec/actions/input_action_spec.rb +20 -19
- data/spec/actions/link_action_spec.rb +30 -29
- data/spec/builder/custom_builder_spec.rb +39 -22
- data/spec/builder/error_proc_spec.rb +6 -5
- data/spec/builder/semantic_fields_for_spec.rb +46 -45
- data/spec/fast_spec_helper.rb +13 -0
- data/spec/generators/formtastic/form/form_generator_spec.rb +33 -32
- data/spec/generators/formtastic/input/input_generator_spec.rb +125 -0
- data/spec/generators/formtastic/install/install_generator_spec.rb +10 -9
- data/spec/helpers/action_helper_spec.rb +329 -10
- data/spec/helpers/actions_helper_spec.rb +43 -42
- data/spec/helpers/form_helper_spec.rb +45 -38
- data/spec/helpers/input_helper_spec.rb +976 -2
- data/spec/helpers/inputs_helper_spec.rb +217 -202
- data/spec/helpers/reflection_helper_spec.rb +7 -6
- data/spec/helpers/semantic_errors_helper_spec.rb +26 -25
- data/spec/i18n_spec.rb +30 -29
- data/spec/input_class_finder_spec.rb +2 -1
- data/spec/inputs/base/collections_spec.rb +78 -0
- data/spec/inputs/base/validations_spec.rb +481 -0
- data/spec/inputs/boolean_input_spec.rb +73 -72
- data/spec/inputs/check_boxes_input_spec.rb +169 -121
- data/spec/inputs/color_input_spec.rb +53 -64
- data/spec/inputs/country_input_spec.rb +23 -22
- data/spec/inputs/custom_input_spec.rb +3 -6
- data/spec/inputs/datalist_input_spec.rb +3 -2
- data/spec/inputs/date_picker_input_spec.rb +114 -113
- data/spec/inputs/date_select_input_spec.rb +76 -61
- data/spec/inputs/datetime_picker_input_spec.rb +123 -122
- data/spec/inputs/datetime_select_input_spec.rb +85 -68
- data/spec/inputs/email_input_spec.rb +17 -16
- data/spec/inputs/file_input_spec.rb +18 -17
- data/spec/inputs/hidden_input_spec.rb +32 -31
- data/spec/inputs/include_blank_spec.rb +10 -9
- data/spec/inputs/label_spec.rb +26 -25
- data/spec/inputs/number_input_spec.rb +212 -211
- data/spec/inputs/password_input_spec.rb +17 -16
- data/spec/inputs/phone_input_spec.rb +17 -16
- data/spec/inputs/placeholder_spec.rb +18 -17
- data/spec/inputs/radio_input_spec.rb +92 -65
- data/spec/inputs/range_input_spec.rb +136 -135
- data/spec/inputs/readonly_spec.rb +51 -0
- data/spec/inputs/search_input_spec.rb +16 -15
- data/spec/inputs/select_input_spec.rb +209 -102
- data/spec/inputs/string_input_spec.rb +51 -50
- data/spec/inputs/text_input_spec.rb +34 -33
- data/spec/inputs/time_picker_input_spec.rb +115 -114
- data/spec/inputs/time_select_input_spec.rb +84 -70
- data/spec/inputs/time_zone_input_spec.rb +58 -31
- data/spec/inputs/url_input_spec.rb +17 -16
- data/spec/inputs/with_options_spec.rb +9 -8
- data/spec/localizer_spec.rb +18 -17
- data/spec/namespaced_class_finder_spec.rb +18 -6
- data/spec/schema.rb +22 -0
- data/spec/spec_helper.rb +172 -260
- data/spec/support/custom_macros.rb +74 -76
- data/spec/support/deprecation.rb +2 -1
- data/spec/support/shared_examples.rb +2 -1233
- data/spec/support/specialized_class_finder_shared_example.rb +1 -0
- data/spec/support/test_environment.rb +24 -9
- metadata +78 -170
- data/.travis.yml +0 -29
- data/Appraisals +0 -29
- data/CHANGELOG +0 -39
- data/DEPRECATIONS +0 -49
- data/gemfiles/rails_3.2.gemfile +0 -7
- data/gemfiles/rails_4.0.4.gemfile +0 -7
- data/gemfiles/rails_4.1.gemfile +0 -7
- data/gemfiles/rails_4.2.gemfile +0 -7
- data/gemfiles/rails_4.gemfile +0 -7
- data/gemfiles/rails_edge.gemfile +0 -10
- data/lib/formtastic/util.rb +0 -57
- data/spec/helpers/namespaced_action_helper_spec.rb +0 -43
- data/spec/helpers/namespaced_input_helper_spec.rb +0 -36
- data/spec/support/deferred_garbage_collection.rb +0 -21
- data/spec/util_spec.rb +0 -66
@@ -1,92 +1,93 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
require 'spec_helper'
|
3
4
|
|
4
|
-
describe 'LinkAction', 'when cancelling' do
|
5
|
+
RSpec.describe 'LinkAction', 'when cancelling' do
|
5
6
|
|
6
7
|
include FormtasticSpecHelper
|
7
8
|
|
8
9
|
before do
|
9
|
-
@output_buffer = ''
|
10
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
10
11
|
mock_everything
|
11
12
|
end
|
12
|
-
|
13
|
+
|
13
14
|
context 'without a :url' do
|
14
15
|
before do
|
15
16
|
concat(semantic_form_for(@new_post) do |builder|
|
16
17
|
concat(builder.action(:cancel, :as => :link))
|
17
18
|
end)
|
18
19
|
end
|
19
|
-
|
20
|
+
|
20
21
|
it 'should render a submit type of input' do
|
21
|
-
output_buffer.
|
22
|
+
expect(output_buffer.to_str).to have_tag('li.action.link_action a[@href="javascript:history.back()"]')
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
end
|
25
|
-
|
26
|
+
|
26
27
|
context 'with a :url as String' do
|
27
|
-
|
28
|
+
|
28
29
|
before do
|
29
30
|
concat(semantic_form_for(@new_post) do |builder|
|
30
31
|
concat(builder.action(:cancel, :as => :link, :url => "http://foo.bah/baz"))
|
31
32
|
end)
|
32
33
|
end
|
33
|
-
|
34
|
+
|
34
35
|
it 'should render a submit type of input' do
|
35
|
-
output_buffer.
|
36
|
+
expect(output_buffer.to_str).to have_tag('li.action.link_action a[@href="http://foo.bah/baz"]')
|
36
37
|
end
|
37
|
-
|
38
|
+
|
38
39
|
end
|
39
40
|
|
40
41
|
context 'with a :url as Hash' do
|
41
|
-
|
42
|
+
|
42
43
|
before do
|
43
44
|
concat(semantic_form_for(@new_post) do |builder|
|
44
45
|
concat(builder.action(:cancel, :as => :link, :url => { :action => "foo" }))
|
45
46
|
end)
|
46
47
|
end
|
47
|
-
|
48
|
+
|
48
49
|
it 'should render a submit type of input' do
|
49
|
-
output_buffer.
|
50
|
+
expect(output_buffer.to_str).to have_tag('li.action.link_action a[@href="/mock/path"]')
|
50
51
|
end
|
51
|
-
|
52
|
+
|
52
53
|
end
|
53
54
|
|
54
55
|
end
|
55
56
|
|
56
|
-
describe 'LinkAction', 'when submitting' do
|
57
|
+
RSpec.describe 'LinkAction', 'when submitting' do
|
57
58
|
|
58
59
|
include FormtasticSpecHelper
|
59
|
-
|
60
|
+
|
60
61
|
before do
|
61
|
-
@output_buffer = ''
|
62
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
62
63
|
mock_everything
|
63
64
|
end
|
64
|
-
|
65
|
+
|
65
66
|
it 'should raise an error' do
|
66
|
-
|
67
|
+
expect {
|
67
68
|
concat(semantic_form_for(@new_post) do |builder|
|
68
69
|
concat(builder.action(:submit, :as => :link))
|
69
70
|
end)
|
70
|
-
}.
|
71
|
+
}.to raise_error(Formtastic::UnsupportedMethodForAction)
|
71
72
|
end
|
72
|
-
|
73
|
+
|
73
74
|
end
|
74
75
|
|
75
|
-
describe 'LinkAction', 'when submitting' do
|
76
|
+
RSpec.describe 'LinkAction', 'when submitting' do
|
76
77
|
|
77
78
|
include FormtasticSpecHelper
|
78
|
-
|
79
|
+
|
79
80
|
before do
|
80
|
-
@output_buffer = ''
|
81
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
81
82
|
mock_everything
|
82
83
|
end
|
83
|
-
|
84
|
+
|
84
85
|
it 'should raise an error' do
|
85
|
-
|
86
|
+
expect {
|
86
87
|
concat(semantic_form_for(@new_post) do |builder|
|
87
88
|
concat(builder.action(:reset, :as => :link))
|
88
89
|
end)
|
89
|
-
}.
|
90
|
+
}.to raise_error(Formtastic::UnsupportedMethodForAction)
|
90
91
|
end
|
91
|
-
|
92
|
+
|
92
93
|
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
require 'spec_helper'
|
3
4
|
|
4
|
-
describe 'Formtastic::Helpers::FormHelper.builder' do
|
5
|
+
RSpec.describe 'Formtastic::Helpers::FormHelper.builder' do
|
5
6
|
|
6
7
|
include FormtasticSpecHelper
|
7
8
|
|
8
9
|
class MyCustomFormBuilder < Formtastic::FormBuilder
|
9
10
|
end
|
10
|
-
|
11
|
+
|
11
12
|
# TODO should be a separate spec for custom inputs
|
12
13
|
class Formtastic::Inputs::AwesomeInput
|
13
14
|
include Formtastic::Inputs::Base
|
@@ -17,37 +18,37 @@ describe 'Formtastic::Helpers::FormHelper.builder' do
|
|
17
18
|
end
|
18
19
|
|
19
20
|
before do
|
20
|
-
@output_buffer = ''
|
21
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
21
22
|
mock_everything
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'is the Formtastic::FormBuilder by default' do
|
25
|
-
Formtastic::Helpers::FormHelper.builder.
|
26
|
+
expect(Formtastic::Helpers::FormHelper.builder).to eq(Formtastic::FormBuilder)
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'can be configured to use your own custom form builder' do
|
29
30
|
# Set it to a custom builder class
|
30
31
|
Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
|
31
|
-
Formtastic::Helpers::FormHelper.builder.
|
32
|
+
expect(Formtastic::Helpers::FormHelper.builder).to eq(MyCustomFormBuilder)
|
32
33
|
|
33
34
|
# Reset it to the default
|
34
35
|
Formtastic::Helpers::FormHelper.builder = Formtastic::FormBuilder
|
35
|
-
Formtastic::Helpers::FormHelper.builder.
|
36
|
+
expect(Formtastic::Helpers::FormHelper.builder).to eq(Formtastic::FormBuilder)
|
36
37
|
end
|
37
38
|
|
38
39
|
it 'should allow custom settings per form builder subclass' do
|
39
40
|
with_config(:all_fields_required_by_default, true) do
|
40
41
|
MyCustomFormBuilder.all_fields_required_by_default = false
|
41
42
|
|
42
|
-
MyCustomFormBuilder.all_fields_required_by_default.
|
43
|
-
Formtastic::FormBuilder.all_fields_required_by_default.
|
43
|
+
expect(MyCustomFormBuilder.all_fields_required_by_default).to be_falsey
|
44
|
+
expect(Formtastic::FormBuilder.all_fields_required_by_default).to be_truthy
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
47
48
|
describe "when using a custom builder" do
|
48
49
|
|
49
50
|
before do
|
50
|
-
@new_post.
|
51
|
+
allow(@new_post).to receive(:title)
|
51
52
|
Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
|
52
53
|
end
|
53
54
|
|
@@ -59,10 +60,10 @@ describe 'Formtastic::Helpers::FormHelper.builder' do
|
|
59
60
|
|
60
61
|
it "should yield an instance of the custom builder" do
|
61
62
|
semantic_form_for(@new_post) do |builder|
|
62
|
-
builder.class.
|
63
|
+
expect(builder.class).to be(MyCustomFormBuilder)
|
63
64
|
end
|
64
65
|
end
|
65
|
-
|
66
|
+
|
66
67
|
# TODO should be a separate spec for custom inputs
|
67
68
|
it "should allow me to call my custom input" do
|
68
69
|
semantic_form_for(@new_post) do |builder|
|
@@ -70,31 +71,47 @@ describe 'Formtastic::Helpers::FormHelper.builder' do
|
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
73
|
-
# See: https://github.com/
|
74
|
+
# See: https://github.com/formtastic/formtastic/issues/657
|
74
75
|
it "should not conflict with navigasmic" do
|
75
|
-
self.class.
|
76
|
-
|
77
|
-
|
76
|
+
allow_any_instance_of(self.class).to receive(:builder).and_return('navigasmic')
|
77
|
+
|
78
|
+
expect { semantic_form_for(@new_post) { |f| } }.not_to raise_error
|
78
79
|
end
|
79
80
|
|
81
|
+
it "should use the custom builder's skipped_columns config for inputs" do
|
82
|
+
class AnotherCustomFormBuilder < Formtastic::FormBuilder
|
83
|
+
configure :skipped_columns, [:title, :created_at]
|
84
|
+
end
|
85
|
+
#AnotherCustomFormBuilder.skipped_columns = [:title, :created_at]
|
86
|
+
|
87
|
+
concat(semantic_form_for(@new_post, builder: AnotherCustomFormBuilder) do |builder|
|
88
|
+
concat(builder.inputs)
|
89
|
+
end)
|
90
|
+
|
91
|
+
expect(output_buffer.to_str).to_not have_tag('input#post_title')
|
92
|
+
expect(output_buffer.to_str).to_not have_tag('li#post_created_at_input')
|
93
|
+
expect(output_buffer.to_str).to have_tag('textarea#post_body')
|
94
|
+
end
|
80
95
|
end
|
81
96
|
|
82
97
|
describe "fields_for" do
|
83
98
|
|
84
99
|
it "should yield an instance of the parent form builder" do
|
85
|
-
@new_post.
|
86
|
-
@new_post.
|
100
|
+
allow(@new_post).to receive(:comment).and_return([@fred])
|
101
|
+
allow(@new_post).to receive(:comment_attributes=)
|
87
102
|
semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
|
88
|
-
builder.class.
|
89
|
-
|
103
|
+
expect(builder.class).to be(MyCustomFormBuilder)
|
104
|
+
|
90
105
|
builder.fields_for(:comment) do |nested_builder|
|
91
|
-
nested_builder.class.
|
106
|
+
expect(nested_builder.class).to be(MyCustomFormBuilder)
|
92
107
|
end
|
93
108
|
end
|
94
109
|
end
|
95
110
|
|
96
111
|
end
|
97
112
|
|
113
|
+
|
114
|
+
|
98
115
|
end
|
99
116
|
|
100
117
|
describe "when using a builder passed to form options" do
|
@@ -102,10 +119,10 @@ describe 'Formtastic::Helpers::FormHelper.builder' do
|
|
102
119
|
describe "fields_for" do
|
103
120
|
|
104
121
|
it "should yield an instance of the parent form builder" do
|
105
|
-
@new_post.
|
122
|
+
allow(@new_post).to receive(:author_attributes=)
|
106
123
|
semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
|
107
124
|
builder.fields_for(:author) do |nested_builder|
|
108
|
-
nested_builder.class.
|
125
|
+
expect(nested_builder.class).to be(MyCustomFormBuilder)
|
109
126
|
end
|
110
127
|
end
|
111
128
|
end
|
@@ -1,12 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
require 'spec_helper'
|
3
4
|
|
4
|
-
describe 'Rails field_error_proc' do
|
5
|
+
RSpec.describe 'Rails field_error_proc' do
|
5
6
|
|
6
7
|
include FormtasticSpecHelper
|
7
8
|
|
8
9
|
before do
|
9
|
-
@output_buffer = ''
|
10
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
10
11
|
mock_everything
|
11
12
|
end
|
12
13
|
|
@@ -14,13 +15,13 @@ describe 'Rails field_error_proc' do
|
|
14
15
|
current_field_error_proc = ::ActionView::Base.field_error_proc
|
15
16
|
|
16
17
|
semantic_form_for(@new_post) do |builder|
|
17
|
-
::ActionView::Base.field_error_proc.
|
18
|
+
expect(::ActionView::Base.field_error_proc).not_to eq(current_field_error_proc)
|
18
19
|
end
|
19
20
|
|
20
|
-
::ActionView::Base.field_error_proc.
|
21
|
+
expect(::ActionView::Base.field_error_proc).to eq(current_field_error_proc)
|
21
22
|
|
22
23
|
form_for(@new_post) do |builder|
|
23
|
-
::ActionView::Base.field_error_proc.
|
24
|
+
expect(::ActionView::Base.field_error_proc).to eq(current_field_error_proc)
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
@@ -1,127 +1,128 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
require 'spec_helper'
|
3
4
|
|
4
|
-
describe 'Formtastic::FormBuilder#fields_for' do
|
5
|
+
RSpec.describe 'Formtastic::FormBuilder#fields_for' do
|
5
6
|
|
6
7
|
include FormtasticSpecHelper
|
7
8
|
|
8
9
|
before do
|
9
|
-
@output_buffer = ''
|
10
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
10
11
|
mock_everything
|
11
|
-
@new_post.
|
12
|
+
allow(@new_post).to receive(:author).and_return(::Author.new)
|
12
13
|
end
|
13
14
|
|
14
15
|
context 'outside a form_for block' do
|
15
16
|
it 'yields an instance of FormHelper.builder' do
|
16
17
|
semantic_fields_for(@new_post) do |nested_builder|
|
17
|
-
nested_builder.class.
|
18
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
18
19
|
end
|
19
20
|
semantic_fields_for(@new_post.author) do |nested_builder|
|
20
|
-
nested_builder.class.
|
21
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
21
22
|
end
|
22
23
|
semantic_fields_for(:author, @new_post.author) do |nested_builder|
|
23
|
-
nested_builder.class.
|
24
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
24
25
|
end
|
25
26
|
semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
26
|
-
nested_builder.class.
|
27
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
27
28
|
end
|
28
29
|
end
|
29
|
-
|
30
|
+
|
30
31
|
it 'should respond to input' do
|
31
32
|
semantic_fields_for(@new_post) do |nested_builder|
|
32
|
-
nested_builder.respond_to?(:input).
|
33
|
+
expect(nested_builder.respond_to?(:input)).to be_truthy
|
33
34
|
end
|
34
35
|
semantic_fields_for(@new_post.author) do |nested_builder|
|
35
|
-
nested_builder.respond_to?(:input).
|
36
|
+
expect(nested_builder.respond_to?(:input)).to be_truthy
|
36
37
|
end
|
37
38
|
semantic_fields_for(:author, @new_post.author) do |nested_builder|
|
38
|
-
nested_builder.respond_to?(:input).
|
39
|
+
expect(nested_builder.respond_to?(:input)).to be_truthy
|
39
40
|
end
|
40
41
|
semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
41
|
-
nested_builder.respond_to?(:input).
|
42
|
+
expect(nested_builder.respond_to?(:input)).to be_truthy
|
42
43
|
end
|
43
44
|
end
|
44
45
|
end
|
45
|
-
|
46
|
+
|
46
47
|
context 'within a form_for block' do
|
47
48
|
it 'yields an instance of FormHelper.builder' do
|
48
49
|
semantic_form_for(@new_post) do |builder|
|
49
50
|
builder.semantic_fields_for(:author) do |nested_builder|
|
50
|
-
nested_builder.class.
|
51
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
51
52
|
end
|
52
53
|
end
|
53
54
|
end
|
54
|
-
|
55
|
+
|
55
56
|
it 'yields an instance of FormHelper.builder with hash-like model' do
|
56
57
|
semantic_form_for(:user) do |builder|
|
57
58
|
builder.semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
|
58
|
-
nested_builder.class.
|
59
|
+
expect(nested_builder.class).to eq(Formtastic::Helpers::FormHelper.builder)
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end
|
62
|
-
|
63
|
+
|
63
64
|
it 'nests the object name' do
|
64
65
|
semantic_form_for(@new_post) do |builder|
|
65
66
|
builder.semantic_fields_for(@bob) do |nested_builder|
|
66
|
-
nested_builder.object_name.
|
67
|
+
expect(nested_builder.object_name).to eq('post[author]')
|
67
68
|
end
|
68
69
|
end
|
69
70
|
end
|
70
|
-
|
71
|
+
|
71
72
|
it 'supports passing collection as second parameter' do
|
72
73
|
semantic_form_for(@new_post) do |builder|
|
73
74
|
builder.semantic_fields_for(:author, [@fred,@bob]) do |nested_builder|
|
74
|
-
nested_builder.object_name.
|
75
|
+
expect(nested_builder.object_name).to match(/post\[author_attributes\]\[\d+\]/)
|
75
76
|
end
|
76
77
|
end
|
77
78
|
end
|
78
|
-
|
79
|
+
|
79
80
|
it 'should sanitize html id for li tag' do
|
80
|
-
@bob.
|
81
|
+
allow(@bob).to receive(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
|
81
82
|
concat(semantic_form_for(@new_post) do |builder|
|
82
83
|
concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
|
83
84
|
concat(nested_builder.inputs(:login))
|
84
85
|
end)
|
85
86
|
end)
|
86
|
-
output_buffer.
|
87
|
+
expect(output_buffer.to_str).to have_tag('form fieldset.inputs #post_author_1_login_input')
|
87
88
|
# Not valid selector, so using good ol' regex
|
88
|
-
output_buffer.
|
89
|
+
expect(output_buffer.to_str).not_to match(/id="post\[author\]_1_login_input"/)
|
89
90
|
# <=> output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
|
90
91
|
end
|
91
|
-
|
92
|
+
|
92
93
|
it 'should use namespace provided in nested fields' do
|
93
|
-
@bob.
|
94
|
+
allow(@bob).to receive(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
|
94
95
|
concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
|
95
96
|
concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
|
96
97
|
concat(nested_builder.inputs(:login))
|
97
98
|
end)
|
98
99
|
end)
|
99
|
-
output_buffer.
|
100
|
+
expect(output_buffer.to_str).to have_tag('form fieldset.inputs #context2_post_author_1_login_input')
|
100
101
|
end
|
101
|
-
|
102
|
+
|
102
103
|
it 'should render errors on the nested inputs' do
|
103
104
|
@errors = double('errors')
|
104
|
-
@errors.
|
105
|
-
@bob.
|
106
|
-
|
105
|
+
allow(@errors).to receive(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
|
106
|
+
allow(@bob).to receive(:errors).and_return(@errors)
|
107
|
+
|
107
108
|
concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
|
108
109
|
concat(builder.semantic_fields_for(@bob) do |nested_builder|
|
109
110
|
concat(nested_builder.inputs(:login))
|
110
111
|
end)
|
111
112
|
end)
|
112
|
-
output_buffer.
|
113
|
+
expect(output_buffer.to_str).to match(/oh noes/)
|
113
114
|
end
|
114
|
-
|
115
|
+
|
115
116
|
end
|
116
|
-
|
117
|
-
context "when I rendered my own hidden id input" do
|
118
|
-
|
117
|
+
|
118
|
+
context "when I rendered my own hidden id input" do
|
119
|
+
|
119
120
|
before do
|
120
|
-
output_buffer.
|
121
|
-
|
122
|
-
@fred.posts.size.
|
123
|
-
@fred.posts.first.
|
124
|
-
@fred.
|
121
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
122
|
+
|
123
|
+
expect(@fred.posts.size).to eq(1)
|
124
|
+
allow(@fred.posts.first).to receive(:persisted?).and_return(true)
|
125
|
+
allow(@fred).to receive(:posts_attributes=)
|
125
126
|
concat(semantic_form_for(@fred) do |builder|
|
126
127
|
concat(builder.semantic_fields_for(:posts) do |nested_builder|
|
127
128
|
concat(nested_builder.input(:id, :as => :hidden))
|
@@ -129,13 +130,13 @@ describe 'Formtastic::FormBuilder#fields_for' do
|
|
129
130
|
end)
|
130
131
|
end)
|
131
132
|
end
|
132
|
-
|
133
|
+
|
133
134
|
it "should only render one hidden input (my one)" do
|
134
|
-
output_buffer.
|
135
|
+
expect(output_buffer.to_str).to have_tag 'input#author_posts_attributes_0_id', :count => 1
|
135
136
|
end
|
136
|
-
|
137
|
+
|
137
138
|
it "should render the hidden input inside an li.hidden" do
|
138
|
-
output_buffer.
|
139
|
+
expect(output_buffer.to_str).to have_tag 'li.hidden input#author_posts_attributes_0_id'
|
139
140
|
end
|
140
141
|
end
|
141
142
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
$LOAD_PATH << 'lib/formtastic'
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'localized_string'
|
6
|
+
require 'inputs'
|
7
|
+
require 'helpers'
|
8
|
+
|
9
|
+
class MyInput
|
10
|
+
include Formtastic::Inputs::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales)
|
@@ -1,9 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
# Generators are not automatically loaded by Rails
|
4
5
|
require 'generators/formtastic/form/form_generator'
|
5
6
|
|
6
|
-
describe Formtastic::FormGenerator do
|
7
|
+
RSpec.describe Formtastic::FormGenerator do
|
7
8
|
|
8
9
|
include FormtasticSpecHelper
|
9
10
|
|
@@ -11,30 +12,30 @@ describe Formtastic::FormGenerator do
|
|
11
12
|
destination File.expand_path("../../../../../tmp", __FILE__)
|
12
13
|
|
13
14
|
before do
|
14
|
-
@output_buffer = ''
|
15
|
+
@output_buffer = ActionView::OutputBuffer.new ''
|
15
16
|
prepare_destination
|
16
17
|
mock_everything
|
17
|
-
::Post.
|
18
|
+
allow(::Post).to receive(:reflect_on_all_associations).with(:belongs_to).and_return([
|
18
19
|
double('reflection', :name => :author, :options => {}, :klass => ::Author, :macro => :belongs_to),
|
19
20
|
double('reflection', :name => :reviewer, :options => {:class_name => 'Author'}, :klass => ::Author, :macro => :belongs_to),
|
20
21
|
double('reflection', :name => :main_post, :options => {}, :klass => ::Post, :macro => :belongs_to),
|
21
22
|
double('reflection', :name => :attachment, :options => {:polymorphic => true}, :macro => :belongs_to),
|
22
23
|
])
|
23
24
|
end
|
24
|
-
|
25
|
+
|
25
26
|
after do
|
26
27
|
FileUtils.rm_rf(File.expand_path("../../../../../tmp", __FILE__))
|
27
28
|
end
|
28
29
|
|
29
30
|
describe 'without model' do
|
30
31
|
it 'should raise Thor::RequiredArgumentMissingError' do
|
31
|
-
|
32
|
+
expect { run_generator }.to raise_error(Thor::RequiredArgumentMissingError)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
describe 'with existing model' do
|
36
37
|
it 'should not raise an exception' do
|
37
|
-
|
38
|
+
expect { run_generator %w(Post) }.not_to raise_error
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
@@ -43,10 +44,10 @@ describe Formtastic::FormGenerator do
|
|
43
44
|
|
44
45
|
describe 'render only the specified attributes' do
|
45
46
|
subject { file('app/views/posts/_form.html.erb') }
|
46
|
-
it {
|
47
|
-
it {
|
48
|
-
it {
|
49
|
-
it {
|
47
|
+
it { is_expected.to exist }
|
48
|
+
it { is_expected.to contain "<%= f.input :title %>" }
|
49
|
+
it { is_expected.to contain "<%= f.input :author %>" }
|
50
|
+
it { is_expected.not_to contain "<%= f.input :main_post %>" }
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
@@ -56,17 +57,17 @@ describe Formtastic::FormGenerator do
|
|
56
57
|
subject { file('app/views/posts/_form.html.erb') }
|
57
58
|
|
58
59
|
describe 'content_columns' do
|
59
|
-
it {
|
60
|
-
it {
|
61
|
-
it {
|
62
|
-
it {
|
60
|
+
it { is_expected.to contain "<%= f.input :title %>" }
|
61
|
+
it { is_expected.to contain "<%= f.input :body %>" }
|
62
|
+
it { is_expected.not_to contain "<%= f.input :created_at %>" }
|
63
|
+
it { is_expected.not_to contain "<%= f.input :updated_at %>" }
|
63
64
|
end
|
64
65
|
|
65
66
|
describe 'reflection_on_association' do
|
66
|
-
it {
|
67
|
-
it {
|
68
|
-
it {
|
69
|
-
it {
|
67
|
+
it { is_expected.to contain "<%= f.input :author %>" }
|
68
|
+
it { is_expected.to contain "<%= f.input :reviewer %>" }
|
69
|
+
it { is_expected.to contain "<%= f.input :main_post %>" }
|
70
|
+
it { is_expected.not_to contain "<%= f.input :attachment %>" }
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
@@ -76,28 +77,28 @@ describe Formtastic::FormGenerator do
|
|
76
77
|
|
77
78
|
describe 'app/views/posts/_form.html.erb' do
|
78
79
|
subject { file('app/views/posts/_form.html.erb') }
|
79
|
-
it {
|
80
|
-
it {
|
80
|
+
it { is_expected.to exist }
|
81
|
+
it { is_expected.to contain "<%= semantic_form_for @post do |f| %>" }
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
85
|
describe 'haml' do
|
85
|
-
|
86
|
+
|
86
87
|
describe 'app/views/posts/_form.html.haml' do
|
87
88
|
before { run_generator %w(Post --template-engine haml) }
|
88
89
|
subject { file('app/views/posts/_form.html.haml') }
|
89
|
-
it {
|
90
|
-
it {
|
90
|
+
it { is_expected.to exist }
|
91
|
+
it { is_expected.to contain "= semantic_form_for @post do |f|" }
|
91
92
|
end
|
92
|
-
|
93
|
+
|
93
94
|
context 'with copy option' do
|
94
95
|
describe 'app/views/posts/_form.html.haml' do
|
95
96
|
before { run_generator %w(Post --copy --template-engine haml) }
|
96
97
|
subject { file('app/views/posts/_form.html.haml') }
|
97
|
-
it {
|
98
|
+
it { is_expected.not_to exist }
|
98
99
|
end
|
99
100
|
end
|
100
|
-
|
101
|
+
|
101
102
|
end
|
102
103
|
|
103
104
|
describe 'slim' do
|
@@ -105,18 +106,18 @@ describe Formtastic::FormGenerator do
|
|
105
106
|
|
106
107
|
describe 'app/views/posts/_form.html.slim' do
|
107
108
|
subject { file('app/views/posts/_form.html.slim') }
|
108
|
-
it {
|
109
|
-
it {
|
109
|
+
it { is_expected.to exist }
|
110
|
+
it { is_expected.to contain "= semantic_form_for @post do |f|" }
|
110
111
|
end
|
111
112
|
end
|
112
113
|
end
|
113
|
-
|
114
|
+
|
114
115
|
describe 'with copy option' do
|
115
116
|
before { run_generator %w(Post --copy) }
|
116
|
-
|
117
|
+
|
117
118
|
describe 'app/views/posts/_form.html.erb' do
|
118
119
|
subject { file('app/views/posts/_form.html.erb') }
|
119
|
-
it {
|
120
|
+
it { is_expected.not_to exist }
|
120
121
|
end
|
121
122
|
end
|
122
123
|
|
@@ -125,7 +126,7 @@ describe Formtastic::FormGenerator do
|
|
125
126
|
|
126
127
|
describe 'app/views/admin/posts/_form.html.erb' do
|
127
128
|
subject { file('app/views/admin/posts/_form.html.erb') }
|
128
|
-
it {
|
129
|
+
it { is_expected.to exist }
|
129
130
|
end
|
130
131
|
end
|
131
132
|
end
|