From 53246538cf7eec1dcd40c5eed831a10c0abc7931 Mon Sep 17 00:00:00 2001 From: Melissa Xie Date: Fri, 15 Mar 2013 17:29:58 -0400 Subject: [PATCH] Remove 'validate_format_of' matcher * See issue #252 --- lib/shoulda/matchers/active_model.rb | 2 - .../validate_format_of_matcher.rb | 108 ------------------ .../validate_format_of_matcher_spec.rb | 75 ------------ 3 files changed, 185 deletions(-) delete mode 100644 lib/shoulda/matchers/active_model/validate_format_of_matcher.rb delete mode 100644 spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb diff --git a/lib/shoulda/matchers/active_model.rb b/lib/shoulda/matchers/active_model.rb index ebdb53a2..8745862a 100644 --- a/lib/shoulda/matchers/active_model.rb +++ b/lib/shoulda/matchers/active_model.rb @@ -9,7 +9,6 @@ require 'shoulda/matchers/active_model/ensure_length_of_matcher' require 'shoulda/matchers/active_model/ensure_inclusion_of_matcher' require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher' require 'shoulda/matchers/active_model/validate_presence_of_matcher' -require 'shoulda/matchers/active_model/validate_format_of_matcher' require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher' require 'shoulda/matchers/active_model/validate_acceptance_of_matcher' require 'shoulda/matchers/active_model/validate_confirmation_of_matcher' @@ -41,7 +40,6 @@ module Shoulda # class User < ActiveRecord::Base # validates_presence_of :name # validates_presence_of :phone_number - # validates_format_of :phone_number, :with => /\\(\\d{3}\\) \\d{3}\\-\\d{4}/ # validates_inclusion_of :status, :in => %w(Activated Pending), :strict => true # attr_accessible :name, :phone_number # end diff --git a/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb deleted file mode 100644 index 9fe478a2..00000000 --- a/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +++ /dev/null @@ -1,108 +0,0 @@ -require 'active_support/deprecation' - -module Shoulda # :nodoc: - module Matchers - module ActiveModel # :nodoc: - # Ensures that the model is not valid if the given attribute is not - # formatted correctly. - # - # Options: - # * with_message - value the test expects to find in - # allow_blank - allows a blank value - # allow_nil - allows a nil value - # errors.on(:attribute). Regexp or String. - # Defaults to the translation for :invalid. - # * with(string to test against) - # * not_with(string to test against) - # - # Examples: - # it { should validate_format_of(:name). - # with('12345'). - # with_message(/is not optional/) } - # it { should validate_format_of(:name). - # not_with('12D45'). - # with_message(/is not optional/) } - # - def validate_format_of(attr) - ValidateFormatOfMatcher.new(attr) - end - - class ValidateFormatOfMatcher < ValidationMatcher # :nodoc: - def initialize(attribute) - ActiveSupport::Deprecation.warn 'The validate_format_of matcher is deprecated and will be removed in 2.0' - super - @options = {} - end - - def allow_blank(allow_blank = true) - @options[:allow_blank] = allow_blank - self - end - - def allow_nil(allow_nil = true) - @options[:allow_nil] = allow_nil - self - end - - def with_message(message) - if message - @expected_message = message - end - self - end - - def with(value) - if @value_to_fail - raise 'You may not call both with and not_with' - else - @value_to_pass = value - self - end - end - - def not_with(value) - if @value_to_pass - raise 'You may not call both with and not_with' - else - @value_to_fail = value - self - end - end - - def matches?(subject) - super(subject) - @expected_message ||= :invalid - - if @value_to_fail - disallows_value_of(@value_to_fail, @expected_message) && allows_blank_value? && allows_nil_value? - else - allows_value_of(@value_to_pass, @expected_message) && allows_blank_value? && allows_nil_value? - end - end - - def description - "have a valid format for #{@attribute}" - end - - private - - def allows_blank_value? - if @options.key?(:allow_blank) - @options[:allow_blank] == allows_value_of('') - else - true - end - end - - def allows_nil_value? - if @options.key?(:allow_nil) - @options[:allow_nil] == allows_value_of(nil) - else - true - end - end - end - - end - end -end diff --git a/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb deleted file mode 100644 index 1277b682..00000000 --- a/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +++ /dev/null @@ -1,75 +0,0 @@ -require 'spec_helper' - -describe Shoulda::Matchers::ActiveModel::ValidateFormatOfMatcher do - context 'a model with a format validation' do - it 'accepts when format matches ' do - validating_format(:with => /^\d{5}$/).should matcher.with('12345') - end - - it 'rejects blank with should_not' do - validating_format(:with => /^\d{5}$/).should_not matcher.with(' ') - end - - it 'rejects blank with not_with' do - validating_format(:with => /^\d{5}$/).should matcher.not_with(' ') - end - - it 'rejects nil' do - validating_format(:with => /^\d{5}$/).should_not matcher.with(nil) - end - - it 'rejects a non-matching format with should_not' do - validating_format(:with => /^\d{5}$/).should_not matcher.with('1234a') - end - - it 'rejects a non-matching format with not_with' do - validating_format(:with => /^\d{5}$/).should matcher.not_with('1234a') - end - - it 'raises an error if you try to call both with and not_with' do - expect { - validate_format_of(:attr).not_with('123456').with('12345') - }.to raise_error(RuntimeError) - end - end - - context 'when allow_blank or allow_nil are set' do - it 'is valid when attr is nil' do - validating_format(:with => /abc/, :allow_nil => true). - should matcher.with(nil) - end - - it 'is valid when attr is blank' do - validating_format(:with => /abc/, :allow_blank => true). - should matcher.with(' ') - end - end - - context '#allow_blank' do - it 'accepts when allow_blank matches' do - validating_format(:with => /abc/, :allow_blank => true). - should matcher.allow_blank - end - - it 'rejects when allow_blank does not match' do - validating_format(:with => /abc/, :allow_blank => false). - should_not matcher.allow_blank - end - end - - context '#allow_nil' do - it 'accepts when allow_nil matches' do - validating_format(:with => /abc/, :allow_nil => true). - should matcher.allow_nil - end - - it 'rejects when allow_nil does not match' do - validating_format(:with => /abc/, :allow_nil => false). - should_not matcher.allow_nil - end - end - - def matcher - validate_format_of(:attr) - end -end