Skip to content

Commit

Permalink
allow_values_for checks for any message unless otherwise given [#90 s…
Browse files Browse the repository at this point in the history
…tatus:resolved].
  • Loading branch information
josevalim committed Aug 25, 2009
1 parent 9eb9cff commit 1c81305
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
2 changes: 2 additions & 0 deletions remarkable_activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* allow_values_for checks for any message unless otherwise given [#90]

* Allow booleans to be given to validate_inclusion_of [#89]

* Allow allow_values_for and allow_mass_assignment_of matchers to be executed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AllowValuesForMatcher < Remarkable::ActiveRecord::Base #:nodoc:

collection_assertions :is_valid?, :is_invalid?, :allow_nil?, :allow_blank?

default_options :message => :invalid
default_options :message => /.*/

before_assert do
first_value = @options[:in].is_a?(Array) ? @options[:in].first : @options[:in]
Expand Down Expand Up @@ -62,14 +62,16 @@ def interpolation_options

end

# Ensures that the attribute can be set to the given values.
# Ensures that the attribute can be set to the given values. It checks
# for any message in the given attribute unless a :message is explicitely
# given.
#
# == Options
#
# * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
# * <tt>:allow_blank</tt> - when supplied, validates if it allows blank or not.
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
# Regexp, string or symbol. Default = <tt>/.*/</tt>
#
# == Examples
#
Expand All @@ -81,17 +83,6 @@ def allow_values_for(attribute, *args, &block)
AllowValuesForMatcher.new(attribute, options.merge!(:in => args), &block).spec(self)
end

# Deprecated. Use allow_values_for instead.
#
def validate_format_of(*args)
if caller[0] =~ /\macros.rb/
warn "[DEPRECATION] should_validate_format_of is deprecated, use should_allow_values_for instead."
else
warn "[DEPRECATION] validate_format_of is deprecated, use allow_values_for instead. Called from #{caller[0]}."
end
allow_values_for(*args)
end

end
end
end
9 changes: 2 additions & 7 deletions remarkable_activerecord/spec/allow_values_for_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def define_and_validate(options={})

describe 'matchers' do
it { should define_and_validate(:with => /X|Y|Z/).in('X', 'Y', 'Z') }
it { should_not define_and_validate(:with => /X|Y|Z/).in('A') }

it { should define_and_validate(:with => /X|Y|Z/, :message => 'valid').in('X', 'Y', 'Z').message('valid') }
it { should_not define_and_validate(:with => /X|Y|Z/).in('A') }
it { should_not define_and_validate(:with => /X|Y|Z/, :message => 'invalid').in('A') }

create_optional_boolean_specs(:allow_nil, self, :with => /X|Y|Z/)
create_optional_boolean_specs(:allow_blank, self, :with => /X|Y|Z/)
Expand All @@ -51,11 +51,6 @@ def define_and_validate(options={})

should_allow_values_for :title, 'X'
should_not_allow_values_for :title, 'A'

describe 'deprecation' do
it { should validate_format_of(:title, 'X') }
should_not_validate_format_of :title, 'A'
end
end

describe 'failures' do
Expand Down

0 comments on commit 1c81305

Please sign in to comment.