formtastic 3.0.0 → 5.0.0

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 (175) hide show
  1. checksums.yaml +5 -13
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/test.yml +61 -0
  4. data/.gitignore +3 -2
  5. data/CHANGELOG.md +61 -0
  6. data/Gemfile.lock +140 -0
  7. data/MIT-LICENSE +1 -1
  8. data/{README.textile → README.md} +191 -168
  9. data/RELEASE_PROCESS +3 -1
  10. data/Rakefile +24 -8
  11. data/app/assets/stylesheets/formtastic.css +1 -1
  12. data/bin/appraisal +8 -0
  13. data/formtastic.gemspec +13 -17
  14. data/gemfiles/rails_6.0/Gemfile +5 -0
  15. data/gemfiles/rails_6.1/Gemfile +5 -0
  16. data/gemfiles/rails_7.0/Gemfile +5 -0
  17. data/gemfiles/rails_7.1/Gemfile +5 -0
  18. data/gemfiles/rails_edge/Gemfile +13 -0
  19. data/lib/formtastic/action_class_finder.rb +19 -0
  20. data/lib/formtastic/actions/base.rb +1 -0
  21. data/lib/formtastic/actions/button_action.rb +56 -53
  22. data/lib/formtastic/actions/buttonish.rb +1 -0
  23. data/lib/formtastic/actions/input_action.rb +60 -57
  24. data/lib/formtastic/actions/link_action.rb +69 -67
  25. data/lib/formtastic/actions.rb +7 -3
  26. data/lib/formtastic/deprecation.rb +6 -0
  27. data/lib/formtastic/engine.rb +4 -1
  28. data/lib/formtastic/form_builder.rb +32 -25
  29. data/lib/formtastic/helpers/action_helper.rb +22 -31
  30. data/lib/formtastic/helpers/actions_helper.rb +1 -0
  31. data/lib/formtastic/helpers/enum.rb +14 -0
  32. data/lib/formtastic/helpers/errors_helper.rb +3 -2
  33. data/lib/formtastic/helpers/fieldset_wrapper.rb +16 -11
  34. data/lib/formtastic/helpers/file_column_detection.rb +1 -0
  35. data/lib/formtastic/helpers/form_helper.rb +4 -3
  36. data/lib/formtastic/helpers/input_helper.rb +59 -80
  37. data/lib/formtastic/helpers/inputs_helper.rb +33 -27
  38. data/lib/formtastic/helpers/reflection.rb +5 -4
  39. data/lib/formtastic/helpers.rb +2 -2
  40. data/lib/formtastic/html_attributes.rb +13 -1
  41. data/lib/formtastic/i18n.rb +2 -1
  42. data/lib/formtastic/input_class_finder.rb +19 -0
  43. data/lib/formtastic/inputs/base/associations.rb +1 -0
  44. data/lib/formtastic/inputs/base/choices.rb +4 -3
  45. data/lib/formtastic/inputs/base/collections.rb +47 -11
  46. data/lib/formtastic/inputs/base/database.rb +8 -5
  47. data/lib/formtastic/inputs/base/datetime_pickerish.rb +1 -0
  48. data/lib/formtastic/inputs/base/errors.rb +7 -6
  49. data/lib/formtastic/inputs/base/fileish.rb +1 -0
  50. data/lib/formtastic/inputs/base/hints.rb +2 -1
  51. data/lib/formtastic/inputs/base/html.rb +12 -10
  52. data/lib/formtastic/inputs/base/labelling.rb +3 -2
  53. data/lib/formtastic/inputs/base/naming.rb +5 -4
  54. data/lib/formtastic/inputs/base/numeric.rb +1 -0
  55. data/lib/formtastic/inputs/base/options.rb +3 -3
  56. data/lib/formtastic/inputs/base/placeholder.rb +1 -0
  57. data/lib/formtastic/inputs/base/stringish.rb +1 -0
  58. data/lib/formtastic/inputs/base/timeish.rb +9 -4
  59. data/lib/formtastic/inputs/base/validations.rb +39 -12
  60. data/lib/formtastic/inputs/base/wrapping.rb +2 -3
  61. data/lib/formtastic/inputs/base.rb +17 -12
  62. data/lib/formtastic/inputs/boolean_input.rb +2 -1
  63. data/lib/formtastic/inputs/check_boxes_input.rb +16 -24
  64. data/lib/formtastic/inputs/color_input.rb +1 -1
  65. data/lib/formtastic/inputs/country_input.rb +4 -1
  66. data/lib/formtastic/inputs/datalist_input.rb +42 -0
  67. data/lib/formtastic/inputs/date_picker_input.rb +1 -0
  68. data/lib/formtastic/inputs/date_select_input.rb +1 -0
  69. data/lib/formtastic/inputs/datetime_picker_input.rb +1 -0
  70. data/lib/formtastic/inputs/datetime_select_input.rb +1 -0
  71. data/lib/formtastic/inputs/email_input.rb +1 -0
  72. data/lib/formtastic/inputs/file_input.rb +3 -2
  73. data/lib/formtastic/inputs/hidden_input.rb +3 -2
  74. data/lib/formtastic/inputs/number_input.rb +1 -0
  75. data/lib/formtastic/inputs/password_input.rb +1 -0
  76. data/lib/formtastic/inputs/phone_input.rb +1 -0
  77. data/lib/formtastic/inputs/radio_input.rb +26 -21
  78. data/lib/formtastic/inputs/range_input.rb +1 -0
  79. data/lib/formtastic/inputs/search_input.rb +1 -0
  80. data/lib/formtastic/inputs/select_input.rb +32 -10
  81. data/lib/formtastic/inputs/string_input.rb +1 -0
  82. data/lib/formtastic/inputs/text_input.rb +1 -0
  83. data/lib/formtastic/inputs/time_picker_input.rb +1 -0
  84. data/lib/formtastic/inputs/time_select_input.rb +1 -0
  85. data/lib/formtastic/inputs/time_zone_input.rb +17 -6
  86. data/lib/formtastic/inputs/url_input.rb +1 -0
  87. data/lib/formtastic/inputs.rb +33 -28
  88. data/lib/formtastic/localized_string.rb +2 -1
  89. data/lib/formtastic/localizer.rb +23 -24
  90. data/lib/formtastic/namespaced_class_finder.rb +98 -0
  91. data/lib/formtastic/version.rb +2 -1
  92. data/lib/formtastic.rb +19 -14
  93. data/lib/generators/formtastic/form/form_generator.rb +8 -2
  94. data/lib/generators/formtastic/input/input_generator.rb +47 -0
  95. data/lib/generators/formtastic/install/install_generator.rb +2 -0
  96. data/lib/generators/templates/formtastic.rb +29 -7
  97. data/lib/generators/templates/input.rb +19 -0
  98. data/sample/basic_inputs.html +1 -1
  99. data/script/integration-template.rb +73 -0
  100. data/script/integration.sh +19 -0
  101. data/spec/action_class_finder_spec.rb +13 -0
  102. data/spec/actions/button_action_spec.rb +21 -20
  103. data/spec/actions/generic_action_spec.rb +134 -133
  104. data/spec/actions/input_action_spec.rb +20 -19
  105. data/spec/actions/link_action_spec.rb +30 -29
  106. data/spec/builder/custom_builder_spec.rb +39 -22
  107. data/spec/builder/error_proc_spec.rb +6 -5
  108. data/spec/builder/semantic_fields_for_spec.rb +46 -45
  109. data/spec/fast_spec_helper.rb +13 -0
  110. data/spec/generators/formtastic/form/form_generator_spec.rb +33 -32
  111. data/spec/generators/formtastic/input/input_generator_spec.rb +125 -0
  112. data/spec/generators/formtastic/install/install_generator_spec.rb +10 -9
  113. data/spec/helpers/action_helper_spec.rb +70 -97
  114. data/spec/helpers/actions_helper_spec.rb +43 -42
  115. data/spec/helpers/form_helper_spec.rb +56 -39
  116. data/spec/helpers/input_helper_spec.rb +314 -255
  117. data/spec/helpers/inputs_helper_spec.rb +217 -202
  118. data/spec/helpers/reflection_helper_spec.rb +7 -6
  119. data/spec/helpers/semantic_errors_helper_spec.rb +26 -25
  120. data/spec/i18n_spec.rb +30 -29
  121. data/spec/input_class_finder_spec.rb +11 -0
  122. data/spec/inputs/base/collections_spec.rb +78 -0
  123. data/spec/inputs/base/validations_spec.rb +481 -0
  124. data/spec/inputs/boolean_input_spec.rb +73 -72
  125. data/spec/inputs/check_boxes_input_spec.rb +174 -123
  126. data/spec/inputs/color_input_spec.rb +53 -64
  127. data/spec/inputs/country_input_spec.rb +23 -22
  128. data/spec/inputs/custom_input_spec.rb +3 -6
  129. data/spec/inputs/datalist_input_spec.rb +62 -0
  130. data/spec/inputs/date_picker_input_spec.rb +114 -113
  131. data/spec/inputs/date_select_input_spec.rb +76 -61
  132. data/spec/inputs/datetime_picker_input_spec.rb +123 -122
  133. data/spec/inputs/datetime_select_input_spec.rb +85 -68
  134. data/spec/inputs/email_input_spec.rb +17 -16
  135. data/spec/inputs/file_input_spec.rb +18 -17
  136. data/spec/inputs/hidden_input_spec.rb +32 -31
  137. data/spec/inputs/include_blank_spec.rb +10 -9
  138. data/spec/inputs/label_spec.rb +36 -31
  139. data/spec/inputs/number_input_spec.rb +212 -211
  140. data/spec/inputs/password_input_spec.rb +17 -16
  141. data/spec/inputs/phone_input_spec.rb +17 -16
  142. data/spec/inputs/placeholder_spec.rb +18 -17
  143. data/spec/inputs/radio_input_spec.rb +92 -65
  144. data/spec/inputs/range_input_spec.rb +136 -135
  145. data/spec/inputs/readonly_spec.rb +51 -0
  146. data/spec/inputs/search_input_spec.rb +16 -15
  147. data/spec/inputs/select_input_spec.rb +209 -102
  148. data/spec/inputs/string_input_spec.rb +51 -50
  149. data/spec/inputs/text_input_spec.rb +34 -33
  150. data/spec/inputs/time_picker_input_spec.rb +115 -114
  151. data/spec/inputs/time_select_input_spec.rb +84 -70
  152. data/spec/inputs/time_zone_input_spec.rb +58 -31
  153. data/spec/inputs/url_input_spec.rb +17 -16
  154. data/spec/inputs/with_options_spec.rb +9 -8
  155. data/spec/localizer_spec.rb +18 -17
  156. data/spec/namespaced_class_finder_spec.rb +91 -0
  157. data/spec/schema.rb +22 -0
  158. data/spec/spec_helper.rb +180 -249
  159. data/spec/support/custom_macros.rb +128 -98
  160. data/spec/support/deprecation.rb +2 -1
  161. data/spec/support/shared_examples.rb +13 -0
  162. data/spec/support/specialized_class_finder_shared_example.rb +28 -0
  163. data/spec/support/test_environment.rb +25 -10
  164. metadata +95 -136
  165. data/.travis.yml +0 -28
  166. data/Appraisals +0 -25
  167. data/CHANGELOG +0 -27
  168. data/gemfiles/rails_3.2.gemfile +0 -7
  169. data/gemfiles/rails_4.0.4.gemfile +0 -7
  170. data/gemfiles/rails_4.1.gemfile +0 -7
  171. data/gemfiles/rails_4.gemfile +0 -7
  172. data/gemfiles/rails_edge.gemfile +0 -10
  173. data/lib/formtastic/util.rb +0 -53
  174. data/spec/support/deferred_garbage_collection.rb +0 -21
  175. data/spec/util_spec.rb +0 -52
@@ -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.should have_tag('li.action.link_action a[@href="javascript:history.back()"]')
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.should have_tag('li.action.link_action a[@href="http://foo.bah/baz"]')
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.should have_tag('li.action.link_action a[@href="/mock/path"]')
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
- lambda {
67
+ expect {
67
68
  concat(semantic_form_for(@new_post) do |builder|
68
69
  concat(builder.action(:submit, :as => :link))
69
70
  end)
70
- }.should raise_error(Formtastic::UnsupportedMethodForAction)
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
- lambda {
86
+ expect {
86
87
  concat(semantic_form_for(@new_post) do |builder|
87
88
  concat(builder.action(:reset, :as => :link))
88
89
  end)
89
- }.should raise_error(Formtastic::UnsupportedMethodForAction)
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.should == Formtastic::FormBuilder
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.should == MyCustomFormBuilder
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.should == Formtastic::FormBuilder
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.should be_false
43
- Formtastic::FormBuilder.all_fields_required_by_default.should be_true
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.stub(:title)
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.should.kind_of?(MyCustomFormBuilder)
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/justinfrench/formtastic/issues/657
74
+ # See: https://github.com/formtastic/formtastic/issues/657
74
75
  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
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.stub(:comment).and_return([@fred])
86
- @new_post.stub(:comment_attributes=)
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.should.kind_of?(MyCustomFormBuilder)
89
-
103
+ expect(builder.class).to be(MyCustomFormBuilder)
104
+
90
105
  builder.fields_for(:comment) do |nested_builder|
91
- nested_builder.class.should.kind_of?(MyCustomFormBuilder)
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.stub(:author_attributes=)
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.should.kind_of?(MyCustomFormBuilder)
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.should_not == current_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.should == current_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.should == current_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.stub(:author).and_return(::Author.new)
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.should == Formtastic::Helpers::FormHelper.builder
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.should == Formtastic::Helpers::FormHelper.builder
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.should == Formtastic::Helpers::FormHelper.builder
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.should == Formtastic::Helpers::FormHelper.builder
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).should be_true
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).should be_true
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).should be_true
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).should be_true
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.should == Formtastic::Helpers::FormHelper.builder
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.should == Formtastic::Helpers::FormHelper.builder
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.should == 'post[author]'
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.should =~ /post\[author_attributes\]\[\d+\]/
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.stub(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
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.should have_tag('form fieldset.inputs #post_author_1_login_input')
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.should_not =~ /id="post\[author\]_1_login_input"/
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.stub(:column_for_attribute).and_return(double('column', :type => :string, :limit => 255))
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.should have_tag('form fieldset.inputs #context2_post_author_1_login_input')
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.stub(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
105
- @bob.stub(:errors).and_return(@errors)
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.should =~ /oh noes/
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.replace ''
121
-
122
- @fred.posts.size.should == 1
123
- @fred.posts.first.stub(:persisted?).and_return(true)
124
- @fred.stub(:posts_attributes=)
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.should have_tag 'input#author_posts_attributes_0_id', :count => 1
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.should have_tag 'li.hidden input#author_posts_attributes_0_id'
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.stub(:reflect_on_all_associations).with(:belongs_to).and_return([
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
- lambda { run_generator }.should raise_error(Thor::RequiredArgumentMissingError)
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
- lambda { run_generator %w(Post) }.should_not raise_error
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 { should exist }
47
- it { should contain "<%= f.input :title %>" }
48
- it { should contain "<%= f.input :author %>" }
49
- it { should_not contain "<%= f.input :main_post %>" }
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 { should contain "<%= f.input :title %>" }
60
- it { should contain "<%= f.input :body %>" }
61
- it { should_not contain "<%= f.input :created_at %>" }
62
- it { should_not contain "<%= f.input :updated_at %>" }
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 { should contain "<%= f.input :author %>" }
67
- it { should contain "<%= f.input :reviewer %>" }
68
- it { should contain "<%= f.input :main_post %>" }
69
- it { should_not contain "<%= f.input :attachment %>" }
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 { should exist }
80
- it { should contain "<%= semantic_form_for @post do |f| %>" }
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 { should exist }
90
- it { should contain "= semantic_form_for @post do |f|" }
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 { should_not exist }
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 { should exist }
109
- it { should contain "= semantic_form_for @post do |f|" }
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 { should_not exist }
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 { should exist }
129
+ it { is_expected.to exist }
129
130
  end
130
131
  end
131
132
  end