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

Added support for ignoring_interference_by_writer to validate_uniqueness_of #800

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions lib/shoulda/matchers/active_model/validation_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ValidationMatcher
def initialize(attribute)
@attribute = attribute
@strict = false
@ignoring_interference_by_writer = false
@failure_message = nil
@failure_message_when_negated = nil
end
Expand All @@ -22,6 +23,11 @@ def strict
self
end

def ignoring_interference_by_writer
@ignoring_interference_by_writer = true
self
end

def failure_message_when_negated
@failure_message_when_negated || @failure_message
end
Expand Down Expand Up @@ -71,6 +77,10 @@ def allow_value_matcher(value, message)
matcher.strict
end

if ignoring_interference_by_writer?
matcher.ignoring_interference_by_writer
end

matcher
end

Expand All @@ -86,12 +96,20 @@ def disallow_value_matcher(value, message)
matcher.strict
end

if ignoring_interference_by_writer?
matcher.ignoring_interference_by_writer
end

matcher
end

def strict?
@strict
end

def ignoring_interference_by_writer?
@ignoring_interference_by_writer
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,51 @@
end
end

context 'when the model maps all values to upcase' do
let(:add_attribute_upcase_normalization) do
lambda do |m|
setter_name = "#{attribute_name}=".to_sym
attr_name = attribute_name
m.class_eval do
define_method(setter_name) do |value|
upcase_value = value.respond_to?(:upcase) ? value.upcase : value
write_attribute(attr_name, upcase_value)
end
end
end
end

context 'when ignoring_interference_by_writer is not specified' do
it 'raises a CouldNotSetAttributeError' do
record = build_record_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: false },
&add_attribute_upcase_normalization
)
running_matcher = lambda do
validate_uniqueness.case_insensitive.matches?(record)
end

avm_class = Shoulda::Matchers::ActiveModel::AllowValueMatcher
expect(&running_matcher).
to raise_error(avm_class::CouldNotSetAttributeError)
end
end

context 'when ignoring_interference_by_writer is specified' do
it 'accepts' do
record = build_record_validating_uniqueness(
attribute_type: :string,
validation_options: { case_sensitive: false },
&add_attribute_upcase_normalization
)

expect(record).to validate_uniqueness.
case_insensitive.ignoring_interference_by_writer
end
end
end

context 'when the validation is declared with allow_nil' do
context 'given a new record whose attribute is nil' do
it 'accepts' do
Expand Down