Skip to content

Commit

Permalink
Add full_messages_for support
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshjain999 committed Dec 24, 2023
1 parent ab73e18 commit d003c90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/reform/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ def messages(*args)
@dotted_errors
end

def full_messages
@dotted_errors.collect { |path, errors|
human_field = path.to_s.gsub(/([\.\_])+/, " ").gsub(/(\b\w)+/) { |s| s.capitalize }
errors.collect { |message| "#{human_field} #{message}" }
}.flatten
def full_messages(errors = @dotted_errors)
errors.collect { |path, errors|
human_field = path.to_s.gsub(/([\.\_])+/, " ").gsub(/(\b\w)+/) { |s| s.capitalize }
errors.collect { |message| "#{human_field} #{message}" }
}.flatten
end

def full_messages_for(field)
full_messages(@dotted_errors.select{ |path| path.to_s == field.to_s })
end

def [](name)
Expand Down
15 changes: 15 additions & 0 deletions test/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ class AlbumForm < TestForm
end
end

describe "#full_messages_for" do
it "renders full messages for given field correctly" do
result = form.validate(
"title" => "",
"artists" => [],
"band" => {"name" => "", "label" => {"name" => ""}}
)

assert_equal result, false
assert_equal form.errors.full_messages_for(:title), ["Title must be filled"]
assert_equal form.errors.full_messages_for('band.name'), ["Band Name must be filled"]
assert_equal form.band.errors.full_messages_for(:name), ["Name must be filled"]
end
end

describe "#add" do
let(:album_title) { nil }
it do
Expand Down

0 comments on commit d003c90

Please sign in to comment.