-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Added absence validator for active model. #7155
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
02e8148
Added absence validator for active model.
robotex82 c35fa0b
Removed blank lines
robotex82 072b977
Add `ActiveModel::Validations::AbsenceValidator`, a validator to chec…
robotex82 838c5e8
Remove block given check from private find_with_ids
carlosantoniodasilva 4995979
Use cached quoted_table_name instead of going through the connection
carlosantoniodasilva fda107b
Remove not used indifferent_access requires from Base and FinderMethods
carlosantoniodasilva a085d27
`plugin new` adds dummy app tasks when necessary.
senny 6f8b95c
Add CHANGELOG entry for #8108 on master too.
rafaelfranca 50337da
Add test to avoid regression of 58e48d5292242f000dc8a87fdbb1c0ccdcf286d8
4d10d8b
test case to lock down the behavior of #7842
senny 4c14e05
routing prefix slash replacement is no longer necessary
senny f66ec25
Store FlashHashes in the session as plain hashes rather than custom o…
jcoglan 9a68393
Remove free usage of #tap
carlosantoniodasilva 64df864
Add `ActiveModel::Validations::AbsenceValidator`, a validator to chec…
robotex82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module ActiveModel | ||
module Validations | ||
# == Active Model Absence Validator | ||
class AbsenceValidator < EachValidator #:nodoc: | ||
def validate(record) | ||
record.errors.add_on_present(attributes, options) | ||
end | ||
end | ||
|
||
module HelperMethods | ||
# Validates that the specified attributes are blank (as defined by | ||
# Object#blank?). Happens by default on save. | ||
# | ||
# class Person < ActiveRecord::Base | ||
# validates_absence_of :first_name | ||
# end | ||
# | ||
# The first_name attribute must be in the object and it must be blank. | ||
# | ||
# Configuration options: | ||
# * <tt>:message</tt> - A custom error message (default is: "must be blank"). | ||
# | ||
# There is also a list of default options supported by every validator: | ||
# +:if+, +:unless+, +:on+ and +:strict+. | ||
# See <tt>ActiveModel::Validation#validates</tt> for more information | ||
def validates_absence_of(*attr_names) | ||
validates_with AbsenceValidator, _merge_attributes(attr_names) | ||
end | ||
end | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
activemodel/test/cases/validations/absence_validation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# encoding: utf-8 | ||
require 'cases/helper' | ||
require 'models/topic' | ||
require 'models/person' | ||
require 'models/custom_reader' | ||
|
||
class AbsenceValidationTest < ActiveModel::TestCase | ||
teardown do | ||
Topic.reset_callbacks(:validate) | ||
Person.reset_callbacks(:validate) | ||
CustomReader.reset_callbacks(:validate) | ||
end | ||
|
||
def test_validate_absences | ||
Topic.validates_absence_of(:title, :content) | ||
t = Topic.new | ||
t.title = "foo" | ||
t.content = "bar" | ||
assert t.invalid? | ||
assert_equal ["must be blank"], t.errors[:title] | ||
assert_equal ["must be blank"], t.errors[:content] | ||
t.title = "" | ||
t.content = "something" | ||
assert t.invalid? | ||
assert_equal ["must be blank"], t.errors[:content] | ||
t.content = "" | ||
assert t.valid? | ||
end | ||
|
||
def test_accepts_array_arguments | ||
Topic.validates_absence_of %w(title content) | ||
t = Topic.new | ||
t.title = "foo" | ||
t.content = "bar" | ||
assert t.invalid? | ||
assert_equal ["must be blank"], t.errors[:title] | ||
assert_equal ["must be blank"], t.errors[:content] | ||
end | ||
|
||
def test_validates_acceptance_of_with_custom_error_using_quotes | ||
Person.validates_absence_of :karma, :message => "This string contains 'single' and \"double\" quotes" | ||
p = Person.new | ||
p.karma = "good" | ||
assert p.invalid? | ||
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last | ||
end | ||
|
||
def test_validates_absence_of_for_ruby_class | ||
Person.validates_absence_of :karma | ||
p = Person.new | ||
p.karma = "good" | ||
assert p.invalid? | ||
assert_equal ["must be blank"], p.errors[:karma] | ||
p.karma = nil | ||
assert p.valid? | ||
end | ||
|
||
def test_validates_absence_of_for_ruby_class_with_custom_reader | ||
CustomReader.validates_absence_of :karma | ||
p = CustomReader.new | ||
p[:karma] = "excellent" | ||
assert p.invalid? | ||
assert_equal ["must be blank"], p.errors[:karma] | ||
p[:karma] = "" | ||
assert p.valid? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 1.9 hash syntax. Thanks.