diff --git a/database.sqlite3 b/database.sqlite3 index 3f0a7b3..a3e1400 100644 Binary files a/database.sqlite3 and b/database.sqlite3 differ diff --git a/test/model_validations_test.rb b/test/model_validations_test.rb index 8c2957c..9028c55 100644 --- a/test/model_validations_test.rb +++ b/test/model_validations_test.rb @@ -4,12 +4,18 @@ class ModelValidationsTest < MiniTest::Spec class Album include ActiveModel::Validations - attr_accessor :title, :artist, :other_attribute + attr_accessor :title, :artist, :other_attribute, :tracks validates :title, :artist, presence: true validates :other_attribute, presence: true end + class Track + include ActiveModel::Validations + + attr_accessor :title + end + class AlbumRating include ActiveModel::Validations @@ -40,6 +46,17 @@ class CompositeForm < Reform::Form copy_validations_from album: Album, album_rating: AlbumRating end + class AlbumTracksForm < Reform::Form + # property :title + # validates :title, presence: true + + collection :tracks, populate_if_empty: Track do + property :title + + validates :title, presence: true + end + end + let(:album) { Album.new } describe 'non-composite form' do @@ -79,4 +96,17 @@ class CompositeForm < Reform::Form end -end \ No newline at end of file + describe 'validate correct position on collection' do + let(:album) { Album.new } + let(:track1) { Track.new } + let(:track2) { Track.new } + + let(:nested_form) { AlbumTracksForm.new(album) } + + it 'is not valid when title is not present' do + album.tracks = [track1, track2] + nested_form.validate(artist_name: 'test', title: nil, tracks: [{title: 'Track 1'}, {title: nil}]).must_equal false + nested_form.errors[:'tracks.title'][1].must_equal ['can\'t be blank'] + end + end +end