私は回避策を書き、それを宝石に抽象化しました:
https://github.com/synth/conditional_attribute_validator
Gemfile:
gem 'conditional_attribute_validator', :git => "git://github.com/synth/conditional_attribute_validator.git"
例:
class User
include ConditionalAttributeValidator
validate_attributes_with_condition :login, :password, :password_confirmation, :unless => :has_another_form_of_authentication?
end
ソース:
def validate_attributes_with_condition(*args)
opts = args.extract_options!
raise "Must have an :if or :unless option" unless opts.has_key?(:if) or opts.has_key?(:unless)
merge_methods = self.methods.grep(/merge_.*_options/)
args.each do |field|
merge_methods.grep(/#{Regexp.quote(field)}/).each do |m|
self.send(m, opts)
end
end
end
Rails は、オプションを既存の検証にマージするために、指定された検証に基づいてmerge_ attr _options メソッドを自動的に作成します。したがって、これらのメソッドを検索して繰り返し処理し、そのメソッドが特定のフィールドに適用されるかどうかを確認します。その場合は、 merge_attr_optionsメソッドを呼び出してオプションを渡します。
これは初期化時に実行されるだけなので、パフォーマンスについてはあまり心配していません。