1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
* Causing issues with virtual attributes and error messages (#277)
* Will need to come up with another solution than adding a check for setting an
attribute correctly.
This commit is contained in:
Melissa Xie 2013-03-29 16:10:01 -04:00
parent f96c8c7606
commit 9ffcf37b3a
3 changed files with 12 additions and 26 deletions

View file

@ -1,5 +1,8 @@
# HEAD # HEAD
* Revert previous change in `AllowValueMatcher` that added a check for a
properly-set attribute.
# v 1.5.5 # v 1.5.5
* `AllowValueMatcher` checks that the right value is used for attempts at * `AllowValueMatcher` checks that the right value is used for attempts at
setting the attribute with it setting the attribute with it

View file

@ -56,9 +56,10 @@ module Shoulda # :nodoc:
def matches?(instance) def matches?(instance)
self.instance = instance self.instance = instance
values_to_match.all? do |current_value| values_to_match.none? do |value|
set_attribute_on_instance(current_value) self.value = value
matches_attribute_value?(current_value) && errors_do_not_match? instance.send("#{attribute}=", value)
errors_match?
end end
end end
@ -79,21 +80,12 @@ module Shoulda # :nodoc:
attr_accessor :values_to_match, :message_finder_factory, attr_accessor :values_to_match, :message_finder_factory,
:instance, :attribute, :value, :matched_error :instance, :attribute, :value, :matched_error
def set_attribute_on_instance(current_value) def errors_match?
self.value = current_value has_messages? && errors_for_attribute_match?
instance.send("#{attribute}=", current_value)
end end
def matches_attribute_value?(current_value) def has_messages?
instance.send(attribute.to_sym) == current_value message_finder.has_messages?
end
def errors_do_not_match?
has_no_messages? || !errors_for_attribute_match?
end
def has_no_messages?
!message_finder.has_messages?
end end
def errors_for_attribute_match? def errors_for_attribute_match?

View file

@ -79,13 +79,6 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
end end
context 'an attribute which must be included in an array' do context 'an attribute which must be included in an array' do
context 'given an attribute that does not accept strings' do
it 'allows an attribute to be set as an integer' do
validating_inclusion(:in => [0,1,2], :column_type => :integer).
should ensure_inclusion_of(:attr).in_array([0,1,2])
end
end
it 'accepts with correct array' do it 'accepts with correct array' do
validating_inclusion(:in => %w(one two)). validating_inclusion(:in => %w(one two)).
should ensure_inclusion_of(:attr).in_array(%w(one two)) should ensure_inclusion_of(:attr).in_array(%w(one two))
@ -172,9 +165,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
end end
def validating_inclusion(options) def validating_inclusion(options)
options[:column_type] ||= :string define_model(:example, :attr => :string) do
define_model(:example, :attr => options[:column_type]) do
validates_inclusion_of :attr, options validates_inclusion_of :attr, options
end.new end.new
end end