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

Add full_messages_for support #539

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source "https://rubygems.org"

gemspec

gem 'dry-validation', '~> 1.5.0'
gem 'dry-validation'
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
Loading