Created
December 15, 2011 23:37
-
-
Save TylerRick/1483504 to your computer and use it in GitHub Desktop.
Custom Validator that automatically provides the %{allowed_options} interpolation variable for use in your error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
activerecord: | |
errors: | |
messages: | |
restrict_to: "is not one of the allowed options (%{allowed_options})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestrictToValidator < ActiveModel::EachValidator | |
ErrorMessage = "An object with the method #include? or a proc or lambda is required, " << | |
"and must be supplied as the :allowed_options option of the configuration hash" | |
def initialize(*args) | |
super | |
@allowed_options = options[:allowed_options] | |
end | |
def check_validity! | |
unless [:include?, :call].any?{ |method| options[:allowed_options].respond_to?(method) } | |
raise ArgumentError, ErrorMessage | |
end | |
end | |
def allowed_options(record) | |
@allowed_options.respond_to?(:call) ? @allowed_options.call(record) : @allowed_options | |
end | |
def allowed_options_string(record) | |
allowed_options = allowed_options(record) | |
if allowed_options.is_a?(Range) | |
"#{allowed_options}" | |
else | |
allowed_options.to_sentence(last_word_connector: ', or ') | |
end | |
end | |
def validate_each(record, attribute, value) | |
allowed_options = allowed_options(record) | |
inclusion_method = inclusion_method(allowed_options) | |
unless allowed_options.send(inclusion_method, value) | |
record.errors.add(attribute, :restrict_to, | |
options.except(:in).merge!( | |
value: value, | |
allowed_options: allowed_options_string(record) | |
) | |
) | |
end | |
end | |
private | |
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the | |
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt> | |
# uses the previous logic of comparing a value with the range endpoints. | |
def inclusion_method(enumerable) | |
enumerable.is_a?(Range) ? :cover? : :include? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://stackoverflow.com/a/8528139/47185