Skip to content

Commit

Permalink
Fixes #179: Allow attributes to accept non-string values
Browse files Browse the repository at this point in the history
* Specifically for `ensure_inclusion_of` matcher to allow 0 for `in_array`
  • Loading branch information
mxie committed Mar 26, 2013
1 parent d5af9bb commit 8ed986c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/shoulda/matchers/active_model/allow_value_matcher.rb
Expand Up @@ -53,11 +53,8 @@ def strict


def matches?(instance) def matches?(instance)
@instance = instance @instance = instance
@values_to_match.none? do |value| set_attributes
@value = value attributes_set_properly? && errors_match?
@instance.send("#{@attribute}=", @value)
errors_match?
end
end end


def failure_message_for_should def failure_message_for_should
Expand All @@ -74,8 +71,29 @@ def description


private private


attr_accessor :value

def set_attributes
@values_to_match.each do |current_value|
self.value = current_value
set_attribute_on_instance(current_value)
end
end

def set_attribute_on_instance(current_value)
@instance.send("#{@attribute}=", current_value)
end

def errors_match? def errors_match?
has_messages? && errors_for_attribute_match? @values_to_match.none? do |value|
has_messages? && errors_for_attribute_match?
end
end

def attributes_set_properly?
@values_to_match.all? do |value|
@instance.send(@attribute.to_sym) == value
end
end end


def has_messages? def has_messages?
Expand Down Expand Up @@ -108,7 +126,7 @@ def errors_match_string?


def expectation def expectation
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : '' includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
[error_source, includes_expected_message, "when #{@attribute} is set to #{@value.inspect}"].join(' ') [error_source, includes_expected_message, "when #{@attribute} is set to #{value.inspect}"].join(' ')
end end


def error_source def error_source
Expand Down
Expand Up @@ -79,6 +79,13 @@
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_integer_inclusion(:in => [0,1,2]).
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))
Expand Down Expand Up @@ -164,6 +171,12 @@
end end
end end


def validating_integer_inclusion(options)
define_model(:example, :attr => :integer) do
validates_inclusion_of :attr, options
end.new
end

def validating_inclusion(options) def validating_inclusion(options)
define_model(:example, :attr => :string) do define_model(:example, :attr => :string) do
validates_inclusion_of :attr, options validates_inclusion_of :attr, options
Expand Down

0 comments on commit 8ed986c

Please sign in to comment.