Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix infinite loop by parent attr #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/reform/form/active_model/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ def full_messages
private

def full_messages_for_nested_fields(form_fields)
form_fields.map { |field| full_messages_for_twin(field[1]) }
form_fields
.to_a
.reject { |field| field[0] == "parent" }
.map { |field| full_messages_for_twin(field[1]) }
end

def full_messages_for_twin(object)
Expand Down
43 changes: 43 additions & 0 deletions test/parent_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test_helper'
require 'disposable/twin/parent'

class ParentTest < BaseTest

class AlbumForm < Reform::Form

feature Disposable::Twin::Parent

property :band
validates :band, presence: true

collection :songs, virtual: true, default: [], populator: :populate_songs! do
property :name
validate :unique_name

def unique_name
if name == parent.band
errors.add(:name, "Song name shouldn't be the same as #{parent.band}")
end
end
end

def populate_songs!(fragment:, **)
existed_song = songs.find { |song| song.name == fragment[:name] }
return existed_song if existed_song
songs.append(OpenStruct.new(name: fragment[:name]))
end

end

let (:form) {
AlbumForm.new(OpenStruct.new(
:band => "Killer Queen"
))
}

it "allows nested collection validation messages to be shown" do
form.validate(songs: [{ name: "Killer Queen" }])
_(form.errors.full_messages).must_equal(["Name Song name shouldn't be the same as Killer Queen"])
end

end