之间不必要的重复,我有责任属性验证两类:减少两班
class NameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
message = options.fetch(:message, I18n.t('errors.attributes.name.invalid'))
record.errors[attribute] << message unless NameValidator.valid_name?(value)
end
def self.valid_name?(name)
name =~ /\A[a-z][\w\p{Blank}]+\z/i
end
end
,第二个
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
message = options.fetch(:message, I18n.t('errors.attributes.email.invalid'))
record.errors[attribute] << message unless EmailValidator.valid_email?(value)
end
def self.valid_email?(email)
email =~ /\[email protected]+\..+\z/i
end
end
,他们基本上是相同的。我应该使用受保护的实用方法从一个类继承它们吗?
恕我直言,这将是一个很好的问题http://codereview.stackexchange.com/ –