reform 2.1.0 → 2.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.travis.yml +4 -12
  4. data/CHANGES.md +8 -0
  5. data/README.md +36 -743
  6. data/Rakefile +1 -31
  7. data/gemfiles/{Gemfile.rails-3.1 → Gemfile.disposable-0.3} +1 -2
  8. data/lib/reform.rb +0 -9
  9. data/lib/reform/contract.rb +5 -1
  10. data/lib/reform/form.rb +1 -4
  11. data/lib/reform/form/composition.rb +1 -2
  12. data/lib/reform/form/dry.rb +29 -16
  13. data/lib/reform/form/module.rb +15 -3
  14. data/lib/reform/form/validate.rb +1 -1
  15. data/lib/reform/validation.rb +3 -3
  16. data/lib/reform/version.rb +1 -1
  17. data/reform.gemspec +3 -10
  18. data/test/coercion_test.rb +7 -7
  19. data/test/composition_test.rb +5 -1
  20. data/test/contract_test.rb +10 -4
  21. data/test/deserialize_test.rb +3 -3
  22. data/test/errors_test.rb +48 -28
  23. data/test/form_option_test.rb +3 -1
  24. data/test/form_test.rb +19 -14
  25. data/test/module_test.rb +51 -11
  26. data/test/populate_test.rb +21 -7
  27. data/test/reform_test.rb +24 -20
  28. data/test/save_test.rb +10 -4
  29. data/test/skip_if_test.rb +5 -3
  30. data/test/test_helper.rb +3 -43
  31. data/test/validate_test.rb +34 -14
  32. data/test/validation/dry_test.rb +60 -0
  33. data/test/validation/dry_validation_test.rb +65 -43
  34. data/test/validation/errors.yml +4 -0
  35. metadata +16 -192
  36. data/database.sqlite3 +0 -0
  37. data/gemfiles/Gemfile.rails-3.2 +0 -7
  38. data/gemfiles/Gemfile.rails-4.0 +0 -8
  39. data/gemfiles/Gemfile.rails-4.1 +0 -8
  40. data/gemfiles/Gemfile.rails-4.2 +0 -8
  41. data/lib/reform/active_record.rb +0 -4
  42. data/lib/reform/form/active_model.rb +0 -87
  43. data/lib/reform/form/active_model/form_builder_methods.rb +0 -48
  44. data/lib/reform/form/active_model/model_reflections.rb +0 -46
  45. data/lib/reform/form/active_model/model_validations.rb +0 -110
  46. data/lib/reform/form/active_model/validations.rb +0 -107
  47. data/lib/reform/form/active_record.rb +0 -30
  48. data/lib/reform/form/lotus.rb +0 -59
  49. data/lib/reform/form/multi_parameter_attributes.rb +0 -48
  50. data/lib/reform/form/validation/unique_validator.rb +0 -54
  51. data/lib/reform/rails.rb +0 -13
  52. data/test/active_model_custom_validation_translations_test.rb +0 -75
  53. data/test/active_model_test.rb +0 -207
  54. data/test/active_model_validation_for_property_named_format_test.rb +0 -18
  55. data/test/active_record_test.rb +0 -273
  56. data/test/builder_test.rb +0 -32
  57. data/test/custom_validation_test.rb +0 -47
  58. data/test/dummy/Rakefile +0 -7
  59. data/test/dummy/app/controllers/albums_controller.rb +0 -18
  60. data/test/dummy/app/controllers/application_controller.rb +0 -4
  61. data/test/dummy/app/controllers/musician_controller.rb +0 -5
  62. data/test/dummy/app/forms/album_form.rb +0 -18
  63. data/test/dummy/app/helpers/application_helper.rb +0 -2
  64. data/test/dummy/app/models/album.rb +0 -4
  65. data/test/dummy/app/models/song.rb +0 -3
  66. data/test/dummy/app/views/albums/new.html.erb +0 -28
  67. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  68. data/test/dummy/config.ru +0 -4
  69. data/test/dummy/config/application.rb +0 -20
  70. data/test/dummy/config/boot.rb +0 -10
  71. data/test/dummy/config/database.yml +0 -22
  72. data/test/dummy/config/environment.rb +0 -5
  73. data/test/dummy/config/environments/development.rb +0 -16
  74. data/test/dummy/config/environments/production.rb +0 -46
  75. data/test/dummy/config/environments/test.rb +0 -33
  76. data/test/dummy/config/locales/en.yml +0 -14
  77. data/test/dummy/config/routes.rb +0 -4
  78. data/test/dummy/db/test.sqlite3 +0 -0
  79. data/test/form_builder_test.rb +0 -138
  80. data/test/lotus/Gemfile +0 -5
  81. data/test/lotus/lotus_test.rb +0 -31
  82. data/test/lotus_test.rb +0 -150
  83. data/test/model_reflections_test.rb +0 -138
  84. data/test/model_validations_test.rb +0 -82
  85. data/test/mongoid_test.rb +0 -313
  86. data/test/multi_parameter_attributes_test.rb +0 -50
  87. data/test/rails/integration_test.rb +0 -54
  88. data/test/unique_test.rb +0 -135
  89. data/test/validation/activemodel_validation_test.rb +0 -252
@@ -1,50 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ReformTest < ReformSpec
4
- describe "Date" do
5
- Person = Struct.new(:date_of_birth)
6
- let (:form) { DateOfBirthForm.new(Person.new) }
7
-
8
- class DateOfBirthForm < Reform::Form
9
- feature Reform::Form::ActiveModel::FormBuilderMethods
10
- feature Reform::Form::MultiParameterAttributes
11
- property :date_of_birth, type: Date, :multi_params => true
12
- end
13
-
14
- it "munges multi-param date fields into a valid Date attribute" do
15
- date_of_birth_params = { "date_of_birth(1i)"=>"1950", "date_of_birth(2i)"=>"1", "date_of_birth(3i)"=>"1" }
16
- form.validate(date_of_birth_params)
17
- form.date_of_birth.must_equal Date.civil(1950, 1, 1)
18
- end
19
-
20
- it "handles invalid Time input" do
21
- date_of_birth_params = { "date_of_birth(1i)"=>"1950", "date_of_birth(2i)"=>"99", "date_of_birth(3i)"=>"1" }
22
- form.validate(date_of_birth_params)
23
- form.date_of_birth.must_equal nil
24
- end
25
- end
26
-
27
- describe "DateTime" do
28
- Party = Struct.new(:start_time)
29
- let (:form) { PartyForm.new(Party.new) }
30
-
31
- class PartyForm < Reform::Form
32
- feature Reform::Form::ActiveModel::FormBuilderMethods
33
- feature Reform::Form::MultiParameterAttributes
34
- property :start_time, type: DateTime, :multi_params => true
35
- end
36
-
37
- it "munges multi-param date and time fields into a valid Time attribute" do
38
- start_time_params = { "start_time(1i)"=>"2000", "start_time(2i)"=>"1", "start_time(3i)"=>"1", "start_time(4i)"=>"12", "start_time(5i)"=>"00" }
39
- time_format = "%Y-%m-%d %H:%M"
40
- form.validate(start_time_params)
41
- form.start_time.strftime(time_format).must_equal DateTime.strptime("2000-01-01 12:00", time_format)
42
- end
43
-
44
- it "handles invalid Time input" do
45
- start_time_params = { "start_time(1i)"=>"2000", "start_time(2i)"=>"99", "start_time(3i)"=>"1", "start_time(4i)"=>"12", "start_time(5i)"=>"00" }
46
- form.validate(start_time_params)
47
- form.start_time.must_equal nil
48
- end
49
- end
50
- end
@@ -1,54 +0,0 @@
1
- require_relative "../test_helper"
2
-
3
- require "dummy/config/environment"
4
- require "rails/test_help" # adds stuff like @routes, etc.
5
-
6
- # require "haml"
7
- # require "haml/template" # Thanks, Nathan!
8
-
9
- #ActiveRecord::Schema.define do
10
- # create_table :artists do |table|
11
- # table.column :name, :string
12
- # table.timestamps
13
- # end
14
-
15
- # create_table :songs do |table|
16
- # table.column :title, :string
17
- # table.column :album_id, :integer
18
- # end
19
-
20
- # create_table :albums do |table|
21
- # table.column :title, :string
22
- # end
23
- # end
24
-
25
- class HasOneAndHasManyTest < ActionController::TestCase
26
- tests AlbumsController
27
-
28
- test "rendering 1-1 and 1-n" do
29
- get :new
30
- #puts @response.body
31
-
32
- assert_select "form"
33
-
34
- assert_select "form input" do |els|
35
- assert_select "[name=?]", "album[title]"
36
- assert_select "[name=?]", "album[songs_attributes][1][title]"
37
- assert_select "[name=?]", "album[songs_attributes][0][title]"
38
- end
39
- end
40
-
41
- test "submitting invalid form" do
42
- params = {
43
- "album"=>{"title"=>"Rio",
44
- "songs_attributes"=>{
45
- "0"=>{"name"=>""},
46
- "1"=>{"name"=>""}
47
- }}, "commit"=>"Create Album"}
48
-
49
- post :create, params
50
-
51
- assert_select "form"
52
- assert_select "li", "Songs title can&#39;t be blank"
53
- end
54
- end
@@ -1,135 +0,0 @@
1
- require "test_helper"
2
-
3
- require "reform/form/validation/unique_validator.rb"
4
- require "reform/form/active_record"
5
-
6
- class UniquenessValidatorOnCreateTest < MiniTest::Spec
7
- class SongForm < Reform::Form
8
- include ActiveRecord
9
- property :title
10
- validates :title, unique: true
11
- end
12
-
13
- it do
14
- Song.delete_all
15
-
16
- form = SongForm.new(Song.new)
17
- form.validate("title" => "How Many Tears").must_equal true
18
- form.save
19
-
20
- form = SongForm.new(Song.new)
21
- form.validate("title" => "How Many Tears").must_equal false
22
- form.errors.to_s.must_equal "{:title=>[\"has already been taken\"]}"
23
- end
24
- end
25
-
26
-
27
- class UniquenessValidatorOnUpdateTest < MiniTest::Spec
28
- class SongForm < Reform::Form
29
- include ActiveRecord
30
- property :title
31
- validates :title, unique: true
32
- end
33
-
34
- it do
35
- Song.delete_all
36
- @song = Song.create(title: "How Many Tears")
37
-
38
- form = SongForm.new(@song)
39
- form.validate("title" => "How Many Tears").must_equal true
40
- form.save
41
-
42
- form = SongForm.new(@song)
43
- form.validate("title" => "How Many Tears").must_equal true
44
- end
45
- end
46
-
47
-
48
- class UniqueWithCompositionTest < MiniTest::Spec
49
- class SongForm < Reform::Form
50
- include ActiveRecord
51
- include Composition
52
-
53
- property :title, on: :song
54
- validates :title, unique: true
55
- end
56
-
57
- it do
58
- Song.delete_all
59
-
60
- form = SongForm.new(song: Song.new)
61
- form.validate("title" => "How Many Tears").must_equal true
62
- form.save
63
- end
64
- end
65
-
66
-
67
- class UniqueValidatorWithScopeTest < MiniTest::Spec
68
- class SongForm < Reform::Form
69
- include ActiveRecord
70
- property :album_id
71
- property :title
72
- validates :title, unique: { scope: :album_id }
73
- end
74
-
75
- it do
76
- Song.delete_all
77
-
78
- album = Album.new
79
- album.save
80
-
81
- form = SongForm.new(Song.new)
82
- form.validate(album_id: album.id, title: 'How Many Tears').must_equal true
83
- form.save
84
-
85
- form = SongForm.new(Song.new)
86
- form.validate(album_id: album.id, title: 'How Many Tears').must_equal false
87
- form.errors.to_s.must_equal "{:title=>[\"has already been taken\"]}"
88
-
89
- album = Album.new
90
- album.save
91
-
92
- form = SongForm.new(Song.new)
93
- form.validate(album_id: album.id, title: 'How Many Tears').must_equal true
94
- end
95
- end
96
-
97
- class UniqueValidatorWithScopeArrayTest < MiniTest::Spec
98
- class SongForm < Reform::Form
99
- include ActiveRecord
100
- property :album_id
101
- property :artist_id
102
- property :title
103
- validates :title, unique: { scope: [:album_id, :artist_id] }
104
- end
105
-
106
- it do
107
- Song.delete_all
108
-
109
- album1 = Album.new
110
- album1.save
111
-
112
- artist1 = Artist.new
113
- artist1.save
114
-
115
- form = SongForm.new(Song.new)
116
- form.validate(album_id: album1.id, artist_id: artist1.id, title: 'How Many Tears').must_equal true
117
- form.save
118
-
119
- form = SongForm.new(Song.new)
120
- form.validate(album_id: album1.id, artist_id: artist1.id, title: 'How Many Tears').must_equal false
121
- form.errors.to_s.must_equal "{:title=>[\"has already been taken\"]}"
122
-
123
- album2 = Album.new
124
- album2.save
125
-
126
- form = SongForm.new(Song.new)
127
- form.validate(album_id: album2.id, artist_id: artist1.id, title: 'How Many Tears').must_equal true
128
-
129
- artist2 = Artist.new
130
- artist2.save
131
-
132
- form = SongForm.new(Song.new)
133
- form.validate(album_id: album1.id, artist_id: artist2.id, title: 'How Many Tears').must_equal true
134
- end
135
- end
@@ -1,252 +0,0 @@
1
- require "test_helper"
2
- require "reform/form/lotus"
3
-
4
- class ActiveModelValidationTest < MiniTest::Spec
5
- Session = Struct.new(:username, :email, :password, :confirm_password)
6
- Album = Struct.new(:name, :songs, :artist)
7
- Artist = Struct.new(:name)
8
-
9
- class SessionForm < Reform::Form
10
- include Reform::Form::ActiveModel::Validations
11
-
12
-
13
- property :username
14
- property :email
15
- property :password
16
- property :confirm_password
17
-
18
- validation :default do
19
- validates :username, presence: true
20
- validates :email, presence: true
21
- end
22
-
23
- validation :email, if: :default do
24
- # validate :email_ok? # FIXME: implement that.
25
- validates :email, length: {is: 3}
26
- end
27
-
28
- validation :nested, if: :default do
29
- validates :password, presence: true, length: {is: 1}
30
- end
31
-
32
- validation :confirm, if: :default, after: :email do
33
- validates :confirm_password, length: {is: 2}
34
- end
35
- end
36
-
37
- let (:form) { SessionForm.new(Session.new) }
38
-
39
- # valid.
40
- it do
41
- form.validate({username: "Helloween", email: "yep", password: "9", confirm_password:"dd"}).must_equal true
42
- form.errors.messages.inspect.must_equal "{}"
43
- end
44
-
45
- # invalid.
46
- it do
47
- form.validate({}).must_equal false
48
- form.errors.messages.inspect.must_equal "{:username=>[\"can't be blank\"], :email=>[\"can't be blank\"]}"
49
- end
50
-
51
- # partially invalid.
52
- # 2nd group fails.
53
- let (:character) { self.class.rails4_2? ? :character : :characters}
54
- it do
55
- form.validate(username: "Helloween", email: "yo").must_equal false
56
- form.errors.messages.inspect.must_equal "{:email=>[\"is the wrong length (should be 3 characters)\"], :confirm_password=>[\"is the wrong length (should be 2 characters)\"], :password=>[\"can't be blank\", \"is the wrong length (should be 1 #{character})\"]}"
57
- end
58
- # 3rd group fails.
59
- it do
60
- form.validate(username: "Helloween", email: "yo!").must_equal false
61
- form.errors.messages.inspect.must_equal"{:confirm_password=>[\"is the wrong length (should be 2 characters)\"], :password=>[\"can't be blank\", \"is the wrong length (should be 1 #{character})\"]}"
62
- end
63
- # 4th group with after: fails.
64
- it do
65
- form.validate(username: "Helloween", email: "yo!", password: "1", confirm_password: "9").must_equal false
66
- form.errors.messages.inspect.must_equal "{:confirm_password=>[\"is the wrong length (should be 2 characters)\"]}"
67
- end
68
-
69
-
70
- describe "implicit :default group" do
71
- # implicit :default group.
72
- class LoginForm < Reform::Form
73
- include Reform::Form::ActiveModel::Validations
74
-
75
-
76
- property :username
77
- property :email
78
- property :password
79
- property :confirm_password
80
-
81
- validates :username, presence: true
82
- validates :email, presence: true
83
- validates :password, presence: true
84
-
85
- validation :after_default, if: :default do
86
- validates :confirm_password, presence: true
87
- end
88
- end
89
-
90
- let (:form) { LoginForm.new(Session.new) }
91
-
92
- # valid.
93
- it do
94
- form.validate({username: "Helloween", email: "yep", password: "9", confirm_password: 9}).must_equal true
95
- form.errors.messages.inspect.must_equal "{}"
96
- end
97
-
98
- # invalid.
99
- it do
100
- form.validate({password: 9}).must_equal false
101
- form.errors.messages.inspect.must_equal "{:username=>[\"can't be blank\"], :email=>[\"can't be blank\"]}"
102
- end
103
-
104
- # partially invalid.
105
- # 2nd group fails.
106
- it do
107
- form.validate(password: 9).must_equal false
108
- form.errors.messages.inspect.must_equal "{:username=>[\"can't be blank\"], :email=>[\"can't be blank\"]}"
109
- end
110
- end
111
-
112
-
113
- # describe "overwriting a group" do
114
- # class OverwritingForm < Reform::Form
115
- # include Reform::Form::ActiveModel::Validations
116
-
117
- # property :username
118
- # property :email
119
-
120
- # validation :email do
121
- # validates :email, presence: true # is not considered, but overwritten.
122
- # end
123
-
124
- # validation :email do # overwrites the above.
125
- # validates :username, presence: true
126
- # end
127
- # end
128
-
129
- # let (:form) { OverwritingForm.new(Session.new) }
130
-
131
- # # valid.
132
- # it do
133
- # form.validate({username: "Helloween"}).must_equal true
134
- # end
135
-
136
- # # invalid.
137
- # it do
138
- # form.validate({}).must_equal false
139
- # form.errors.messages.inspect.must_equal "{:username=>[\"username can't be blank\"]}"
140
- # end
141
- # end
142
-
143
-
144
- describe "inherit: true in same group" do
145
- class InheritSameGroupForm < Reform::Form
146
- include Reform::Form::ActiveModel::Validations
147
-
148
- property :username
149
- property :email
150
-
151
- validation :email do
152
- validates :email, presence: true
153
- end
154
-
155
- validation :email, inherit: true do # extends the above.
156
- validates :username, presence: true
157
- end
158
- end
159
-
160
- let (:form) { InheritSameGroupForm.new(Session.new) }
161
-
162
- # valid.
163
- it do
164
- form.validate({username: "Helloween", email: 9}).must_equal true
165
- end
166
-
167
- # invalid.
168
- it do
169
- form.validate({}).must_equal false
170
- form.errors.messages.inspect.must_equal "{:email=>[\"can't be blank\"], :username=>[\"can't be blank\"]}"
171
- end
172
- end
173
-
174
-
175
- describe "if: with lambda" do
176
- class IfWithLambdaForm < Reform::Form
177
- include Reform::Form::ActiveModel::Validations
178
-
179
- property :username
180
- property :email
181
- property :password
182
-
183
- validation :email do
184
- validates :email, presence: true
185
- end
186
-
187
- # run this is :email group is true.
188
- validation :after_email, if: lambda { |results| results[:email]==true } do # extends the above.
189
- validates :username, presence: true
190
- end
191
-
192
- # block gets evaled in form instance context.
193
- validation :password, if: lambda { |results| email == "john@trb.org" } do
194
- validates :password, presence: true
195
- end
196
- end
197
-
198
- let (:form) { IfWithLambdaForm.new(Session.new) }
199
-
200
- # valid.
201
- it do
202
- form.validate({username: "Strung Out", email: 9}).must_equal true
203
- end
204
-
205
- # invalid.
206
- it do
207
- form.validate({email: 9}).must_equal false
208
- form.errors.messages.inspect.must_equal "{:username=>[\"can't be blank\"]}"
209
- end
210
- end
211
-
212
-
213
- # TODO: describe "multiple errors for property" do
214
-
215
- describe "::validate" do
216
- class ValidateForm < Reform::Form
217
- include Reform::Form::ActiveModel::Validations
218
-
219
- property :username
220
- validates :username, presence: true
221
- validate :username_ok?#, context: :entity
222
-
223
- def username_ok?#(value)
224
- errors.add(:username, "not ok") if username == "yo"
225
- end
226
- end
227
-
228
- let (:form) { ValidateForm.new(Session.new) }
229
-
230
- # invalid.
231
- it do
232
- form.validate({username: "yo"}).must_equal false
233
- form.errors.messages.inspect.must_equal "{:username=>[\"not ok\"]}"
234
- end
235
-
236
- # valid.
237
- it do
238
- form.validate({username: "not yo"}).must_equal true
239
- form.errors.empty?.must_equal true
240
- end
241
- end
242
-
243
-
244
- describe "validates: :acceptance" do
245
- class AcceptanceForm < Reform::Form
246
- property :accept, virtual: true, validates: { acceptance: true }
247
- end
248
-
249
- it { AcceptanceForm.new(nil).validate(accept: "0").must_equal false }
250
- it { AcceptanceForm.new(nil).validate(accept: "1").must_equal true }
251
- end
252
- end