reform 2.3.0.rc2 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +17 -0
  3. data/.gitignore +4 -2
  4. data/CHANGES.md +24 -1
  5. data/Gemfile +1 -7
  6. data/LICENSE.txt +1 -1
  7. data/README.md +5 -5
  8. data/Rakefile +2 -15
  9. data/lib/reform/contract/validate.rb +1 -1
  10. data/lib/reform/form/dry.rb +47 -9
  11. data/lib/reform/form/populator.rb +13 -3
  12. data/lib/reform/form/prepopulate.rb +1 -1
  13. data/lib/reform/form/validate.rb +3 -3
  14. data/lib/reform/validation/groups.rb +0 -1
  15. data/lib/reform/version.rb +1 -1
  16. data/reform.gemspec +3 -4
  17. data/test/call_test.rb +23 -0
  18. data/test/changed_test.rb +6 -6
  19. data/test/coercion_test.rb +17 -17
  20. data/test/{composition_new_api.rb → composition_test.rb} +27 -28
  21. data/test/{contract_new_api.rb → contract_test.rb} +8 -8
  22. data/test/default_test.rb +2 -2
  23. data/test/deserialize_test.rb +8 -8
  24. data/test/docs/validation_test.rb +134 -0
  25. data/test/{errors_new_api.rb → errors_test.rb} +41 -41
  26. data/test/feature_test.rb +2 -2
  27. data/test/fixtures/dry_error_messages.yml +64 -54
  28. data/test/{form_option_new_api.rb → form_option_test.rb} +1 -1
  29. data/test/{form_new_api.rb → form_test.rb} +3 -3
  30. data/test/from_test.rb +10 -10
  31. data/test/{inherit_new_api.rb → inherit_test.rb} +17 -17
  32. data/test/{module_new_api.rb → module_test.rb} +10 -10
  33. data/test/parse_option_test.rb +7 -7
  34. data/test/parse_pipeline_test.rb +1 -1
  35. data/test/{populate_new_api.rb → populate_test.rb} +136 -53
  36. data/test/populator_skip_test.rb +2 -2
  37. data/test/prepopulator_test.rb +16 -16
  38. data/test/read_only_test.rb +2 -2
  39. data/test/readable_test.rb +3 -3
  40. data/test/{reform_new_api.rb → reform_test.rb} +19 -19
  41. data/test/{save_new_api.rb → save_test.rb} +4 -4
  42. data/test/setup_test.rb +9 -9
  43. data/test/{skip_if_new_api.rb → skip_if_test.rb} +12 -12
  44. data/test/skip_setter_and_getter_test.rb +6 -6
  45. data/test/test_helper.rb +6 -7
  46. data/test/{validate_new_api.rb → validate_test.rb} +108 -76
  47. data/test/validation/{dry_validation_new_api.rb → dry_validation_test.rb} +145 -135
  48. data/test/validation/result_test.rb +14 -14
  49. data/test/virtual_test.rb +7 -7
  50. data/test/writeable_test.rb +8 -8
  51. metadata +46 -99
  52. data/.rubocop.yml +0 -30
  53. data/.rubocop_todo.yml +0 -460
  54. data/.travis.yml +0 -32
  55. data/lib/reform/form/dry/new_api.rb +0 -58
  56. data/lib/reform/form/dry/old_api.rb +0 -61
  57. data/test/call_new_api.rb +0 -23
  58. data/test/call_old_api.rb +0 -23
  59. data/test/composition_old_api.rb +0 -184
  60. data/test/contract_old_api.rb +0 -77
  61. data/test/errors_old_api.rb +0 -230
  62. data/test/fixtures/dry_new_api_error_messages.yml +0 -104
  63. data/test/form_old_api.rb +0 -57
  64. data/test/form_option_old_api.rb +0 -24
  65. data/test/inherit_old_api.rb +0 -105
  66. data/test/module_old_api.rb +0 -146
  67. data/test/populate_old_api.rb +0 -304
  68. data/test/reform_old_api.rb +0 -202
  69. data/test/save_old_api.rb +0 -101
  70. data/test/skip_if_old_api.rb +0 -92
  71. data/test/validate_old_api.rb +0 -410
  72. data/test/validation/dry_validation_old_api.rb +0 -772
data/.travis.yml DELETED
@@ -1,32 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- bundler_args: --without benchmarks tools
4
- rvm:
5
- - ruby-head
6
- - 2.5
7
- - 2.4
8
- env:
9
- - "DRY_VALIDATION='~> 1.3.0'"
10
- - "DRY_VALIDATION='~> 1.2.0'"
11
- - "DRY_VALIDATION='~> 1.1.0'"
12
- - "DRY_VALIDATION='~> 1.0.0'"
13
- - "DRY_VALIDATION='~> 0.13.0'"
14
- - "DRY_VALIDATION='~> 0.12.0'"
15
- matrix:
16
- fast_finish: true
17
- allow_failures:
18
- - rvm: ruby-head
19
- include:
20
- - { rvm: 2.2, env: "DRY_VALIDATION='~> 0.11.0'" }
21
- - { rvm: 2.3, env: "DRY_VALIDATION='~> 0.12.0'" }
22
- - { rvm: 2.3, env: "DRY_VALIDATION='~> 0.13.0'" }
23
-
24
-
25
- # maybe move this to slack in a private channel
26
- # notifications:
27
- # webhooks:
28
- # urls:
29
- # - https://webhooks.gitter.im/e/680e86d98056f2ae2fd7
30
- # on_success: change # options: [always|never|change] default: always
31
- # on_failure: always # options: [always|never|change] default: always
32
- # on_start: never # options: [always|never|change] default: always
@@ -1,58 +0,0 @@
1
- module Reform::Form::Dry
2
- module NewApi
3
- class Contract < Dry::Validation::Contract
4
- end
5
-
6
- module Validations
7
- module ClassMethods
8
- def validation_group_class
9
- Group
10
- end
11
- end
12
-
13
- def self.included(includer)
14
- includer.extend(ClassMethods)
15
- end
16
-
17
- class Group
18
- include InputHash
19
-
20
- def initialize(options = {})
21
- options ||= {}
22
- @validator = options[:schema] || Reform::Form::Dry::NewApi::Contract
23
-
24
- @schema_inject_params = options[:with] || {}
25
- end
26
-
27
- def instance_exec(&block)
28
- Dry::Validation.load_extensions(:hints)
29
- @block = block
30
- end
31
-
32
- def call(form)
33
- dynamic_options = {}
34
- dynamic_options[:form] = form if @schema_inject_params[:form]
35
- inject_options = @schema_inject_params.merge(dynamic_options)
36
-
37
- Dry::Schema::DSL.class_eval do
38
- inject_options.each do |key, value|
39
- define_method(key) { value }
40
- end
41
- end
42
-
43
- # when passing options[:schema] the class instance is already created so we just need to call
44
- # "call"
45
- @validator = @validator.build(&@block) if @validator == Reform::Form::Dry::NewApi::Contract
46
-
47
- # TODO: only pass submitted values to Schema#call?
48
- dry_result = @validator.call(input_hash(form))
49
- # dry_messages = dry_result.messages
50
-
51
- return dry_result
52
-
53
- _reform_errors = Reform::Contract::Errors.new(dry_result) # TODO: dry should be merged here.
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,61 +0,0 @@
1
- module Reform::Form::Dry
2
- module OldApi
3
- class Schema < Dry::Validation::Schema
4
- end
5
-
6
- module Validations
7
- module ClassMethods
8
- def validation_group_class
9
- Group
10
- end
11
- end
12
-
13
- def self.included(includer)
14
- includer.extend(ClassMethods)
15
- end
16
-
17
- class Group
18
- include InputHash
19
-
20
- def initialize(options = {})
21
- options ||= {}
22
- schema_class = options[:schema] || Reform::Form::Dry::OldApi::Schema
23
- @validator = Dry::Validation.Schema(schema_class, build: false)
24
-
25
- @schema_inject_params = options[:with] || {}
26
- end
27
-
28
- def instance_exec(&block)
29
- @validator = Dry::Validation.Schema(@validator, build: false, &block)
30
- # inject the keys into the configure block automatically
31
- keys = @schema_inject_params.keys
32
- @validator.class_eval do
33
- configure do
34
- keys.each { |k| option k }
35
- end
36
- end
37
- end
38
-
39
- def call(form)
40
- dynamic_options = {}
41
- dynamic_options[:form] = form if @schema_inject_params[:form]
42
- inject_options = @schema_inject_params.merge(dynamic_options)
43
-
44
- # TODO: only pass submitted values to Schema#call?
45
- dry_result = call_schema(inject_options, input_hash(form))
46
- # dry_messages = dry_result.messages
47
-
48
- return dry_result
49
-
50
- _reform_errors = Reform::Contract::Errors.new(dry_result) # TODO: dry should be merged here.
51
- end
52
-
53
- private
54
-
55
- def call_schema(inject_options, input)
56
- @validator.new(@validator.rules, inject_options).(input)
57
- end
58
- end
59
- end
60
- end
61
- end
data/test/call_new_api.rb DELETED
@@ -1,23 +0,0 @@
1
- require "test_helper"
2
-
3
- class CallTest < Minitest::Spec
4
- Song = Struct.new(:title)
5
-
6
- class SongForm < TestForm
7
- property :title
8
-
9
- validation do
10
- params { required(:title).filled }
11
- end
12
- end
13
-
14
- let(:form) { SongForm.new(Song.new) }
15
-
16
- it { form.(title: "True North").success?.must_equal true }
17
- it { form.(title: "True North").failure?.must_equal false }
18
- it { form.(title: "").success?.must_equal false }
19
- it { form.(title: "").failure?.must_equal true }
20
-
21
- it { form.(title: "True North").errors.messages.must_equal({}) }
22
- it { form.(title: "").errors.messages.must_equal(title: ["must be filled"]) }
23
- end
data/test/call_old_api.rb DELETED
@@ -1,23 +0,0 @@
1
- require "test_helper"
2
-
3
- class CallTest < Minitest::Spec
4
- Song = Struct.new(:title)
5
-
6
- class SongForm < TestForm
7
- property :title
8
-
9
- validation do
10
- required(:title).filled
11
- end
12
- end
13
-
14
- let(:form) { SongForm.new(Song.new) }
15
-
16
- it { form.(title: "True North").success?.must_equal true }
17
- it { form.(title: "True North").failure?.must_equal false }
18
- it { form.(title: "").success?.must_equal false }
19
- it { form.(title: "").failure?.must_equal true }
20
-
21
- it { form.(title: "True North").errors.messages.must_equal({}) }
22
- it { form.(title: "").errors.messages.must_equal(title: ["must be filled"]) }
23
- end
@@ -1,184 +0,0 @@
1
- require "test_helper"
2
-
3
- class FormCompositionInheritanceTest < MiniTest::Spec
4
- module SizePrice
5
- include Reform::Form::Module
6
-
7
- property :price
8
- property :size
9
-
10
- module InstanceMethods
11
- def price(for_size: size)
12
- case for_size.to_sym
13
- when :s then super() * 1
14
- when :m then super() * 2
15
- when :l then super() * 3
16
- end
17
- end
18
- end
19
- end
20
-
21
- class OutfitForm < TestForm
22
- include Reform::Form::Composition
23
- include SizePrice
24
-
25
- property :price, inherit: true, on: :tshirt
26
- property :size, inherit: true, on: :measurement
27
- end
28
-
29
- let(:measurement) { Measurement.new(:l) }
30
- let(:tshirt) { Tshirt.new(2, :m) }
31
- let(:form) { OutfitForm.new(tshirt: tshirt, measurement: measurement) }
32
-
33
- Tshirt = Struct.new(:price, :size)
34
- Measurement = Struct.new(:size)
35
-
36
- it { form.price.must_equal 6 }
37
- it { form.price(for_size: :s).must_equal 2 }
38
- end
39
-
40
- class FormCompositionTest < MiniTest::Spec
41
- Song = Struct.new(:id, :title, :band)
42
- Requester = Struct.new(:id, :name, :requester)
43
- Band = Struct.new(:title)
44
-
45
- class RequestForm < TestForm
46
- include Composition
47
-
48
- property :name, on: :requester
49
- property :requester_id, on: :requester, from: :id
50
- properties :title, :id, on: :song
51
- # property :channel # FIXME: what about the "main model"?
52
- property :channel, virtual: true, on: :song
53
- property :requester, on: :requester
54
- property :captcha, on: :song, virtual: true
55
-
56
- validation do
57
- required(:name).filled
58
- required(:title).filled
59
- end
60
-
61
- property :band, on: :song do
62
- property :title
63
- end
64
- end
65
-
66
- let(:form) { RequestForm.new(song: song, requester: requester) }
67
- let(:song) { Song.new(1, "Rio", band) }
68
- let(:requester) { Requester.new(2, "Duran Duran", "MCP") }
69
- let(:band) { Band.new("Duran^2") }
70
-
71
- # delegation form -> composition works
72
- it { form.id.must_equal 1 }
73
- it { form.title.must_equal "Rio" }
74
- it { form.name.must_equal "Duran Duran" }
75
- it { form.requester_id.must_equal 2 }
76
- it { assert_nil form.channel }
77
- it { form.requester.must_equal "MCP" } # same name as composed model.
78
- it { assert_nil form.captcha }
79
-
80
- # #model just returns <Composition>.
81
- it { form.mapper.must_be_kind_of Disposable::Composition }
82
-
83
- # #model[] -> composed models
84
- it { form.model[:requester].must_equal requester }
85
- it { form.model[:song].must_equal song }
86
-
87
- it "creates Composition for you" do
88
- form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb").must_equal true
89
- form.validate("title" => "", "name" => "Frenzal Rhomb").must_equal false
90
- end
91
-
92
- describe "#save" do
93
- # #save with {}
94
- it do
95
- hash = {}
96
-
97
- form.save do |map|
98
- hash[:name] = form.name
99
- hash[:title] = form.title
100
- end
101
-
102
- hash.must_equal({name: "Duran Duran", title: "Rio"})
103
- end
104
-
105
- it "provides nested symbolized hash as second block argument" do
106
- form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb", "channel" => "JJJ", "captcha" => "wonderful")
107
-
108
- hash = nil
109
-
110
- form.save do |map|
111
- hash = map
112
- end
113
-
114
- hash.must_equal({
115
- song: {"title" => "Greyhound", "id" => 1, "channel" => "JJJ", "captcha" => "wonderful", "band" => {"title" => "Duran^2"}},
116
- requester: {"name" => "Frenzal Rhomb", "id" => 2, "requester" => "MCP"}
117
- }
118
- )
119
- end
120
-
121
- it "xxx pushes data to models and calls #save when no block passed" do
122
- song.extend(Saveable)
123
- requester.extend(Saveable)
124
- band.extend(Saveable)
125
-
126
- form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb", "captcha" => "1337")
127
- form.captcha.must_equal "1337" # TODO: move to separate test.
128
-
129
- form.save
130
-
131
- requester.name.must_equal "Frenzal Rhomb"
132
- requester.saved?.must_equal true
133
- song.title.must_equal "Greyhound"
134
- song.saved?.must_equal true
135
- song.band.title.must_equal "Duran^2"
136
- song.band.saved?.must_equal true
137
- end
138
-
139
- it "returns true when models all save successfully" do
140
- song.extend(Saveable)
141
- requester.extend(Saveable)
142
- band.extend(Saveable)
143
-
144
- form.save.must_equal true
145
- end
146
-
147
- it "returns false when one or more models don't save successfully" do
148
- module Unsaveable
149
- def save
150
- false
151
- end
152
- end
153
-
154
- song.extend(Unsaveable)
155
- requester.extend(Saveable)
156
- band.extend(Saveable)
157
-
158
- form.save.must_equal false
159
- end
160
- end
161
- end
162
-
163
- class FormCompositionCollectionTest < MiniTest::Spec
164
- Book = Struct.new(:id, :name)
165
- Library = Struct.new(:id) do
166
- def books
167
- [Book.new(1, "My book")]
168
- end
169
- end
170
-
171
- class LibraryForm < TestForm
172
- include Reform::Form::Composition
173
-
174
- collection :books, on: :library do
175
- property :id
176
- property :name
177
- end
178
- end
179
-
180
- let(:form) { LibraryForm.new(library: library) }
181
- let(:library) { Library.new(2) }
182
-
183
- it { form.save { |hash| hash.must_equal({library: {"books" => [{"id" => 1, "name" => "My book"}]}}) } }
184
- end
@@ -1,77 +0,0 @@
1
- require "test_helper"
2
-
3
- class ContractTest < MiniTest::Spec
4
- Song = Struct.new(:title, :album, :composer)
5
- Album = Struct.new(:name, :duration, :songs, :artist)
6
- Artist = Struct.new(:name)
7
-
8
- class ArtistForm < TestForm
9
- property :name
10
- end
11
-
12
- class AlbumForm < TestContract
13
- property :name
14
-
15
- properties :duration
16
- properties :year, :style, readable: false
17
-
18
- validation do
19
- required(:name).filled
20
- end
21
-
22
- collection :songs do
23
- property :title
24
- validation do
25
- required(:title).filled
26
- end
27
-
28
- property :composer do
29
- property :name
30
- validation do
31
- required(:name).filled
32
- end
33
- end
34
- end
35
-
36
- property :artist, form: ArtistForm
37
- end
38
-
39
- let(:song) { Song.new("Broken") }
40
- let(:song_with_composer) { Song.new("Resist Stance", nil, composer) }
41
- let(:composer) { Artist.new("Greg Graffin") }
42
- let(:artist) { Artist.new("Bad Religion") }
43
- let(:album) { Album.new("The Dissent Of Man", 123, [song, song_with_composer], artist) }
44
-
45
- let(:form) { AlbumForm.new(album) }
46
-
47
- # accept `property form: SongForm`.
48
- it do
49
- form.artist.must_be_instance_of ArtistForm
50
- end
51
-
52
- describe ".properties" do
53
- it "defines a property when called with one argument" do
54
- form.must_respond_to :duration
55
- end
56
-
57
- it "defines several properties when called with multiple arguments" do
58
- form.must_respond_to :year
59
- form.must_respond_to :style
60
- end
61
-
62
- it "passes options to each property when options are provided" do
63
- readable = AlbumForm.new(album).options_for(:style)[:readable]
64
- readable.must_equal false
65
- end
66
-
67
- it "returns the list of defined properties" do
68
- returned_value = AlbumForm.properties(:hello, :world, virtual: true)
69
- returned_value.must_equal %i[hello world]
70
- end
71
- end
72
-
73
- describe "#options_for" do
74
- it { AlbumForm.options_for(:name).extend(Declarative::Inspect).inspect.must_equal "#<Disposable::Twin::Definition: @options={:private_name=>:name, :name=>\"name\"}>" }
75
- it { AlbumForm.new(album).options_for(:name).extend(Declarative::Inspect).inspect.must_equal "#<Disposable::Twin::Definition: @options={:private_name=>:name, :name=>\"name\"}>" }
76
- end
77
- end