Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created May 10, 2009 02:18
Show Gist options
  • Save neerajsingh0101/109474 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/109474 to your computer and use it in GitHub Desktop.
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
module Matchers
def validate_format_of(attr)
ValidateFormatOfMatcher.new(attr)
end
class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
attr_accessor :invalid_value
def with_message(message)
@expected_message = message if message
self
end
def with_invalid_value(value)
@invalid_value = value
self
end
def matches?(subject)
super(subject)
@expected_message ||= :invalid
disallows_value_of(invalid_value, @expected_message)
end
def description
"only allow values for #{@attribute} which match format"
end
end
end
end
end
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
module Macros
def should_validate_format_of(*attributes)
message, invalid_value = get_options!(attributes, :message, :invalid_value)
klass = model_class
attribute = attributes.first
matcher = validate_format_of(attribute).with_message(message).with_invalid_value(invalid_value)
should matcher.description do
assert_accepts matcher, get_instance_of(klass)
end
end
end
end
end
@mrwellmann
Copy link

Hi,
I get an error:
C:/Ruby/lib/ruby/gems/1.8/gems/shoulda-2.10.3/lib/shoulda/private_helpers.rb:9:in `get_options!': Unsupported options given: with (ArgumentError)

from D:/ .. /config/initializers/should_validate_format_of.rb:44:in `should_validate_format_of'

Sry I'm quiet new to ruby and google didn't help jet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment