Permalink
Browse files

Added the missing file that caused the failness of the weekend.

  • Loading branch information...
1 parent 3856521 commit 102fc61f08a967e8eab3aa4cad67abd0289afd4a Martin Kihlgren committed Apr 21, 2008
Showing with 59 additions and 0 deletions.
  1. +59 −0 dm-validations/lib/dm-validations/acceptance_validator.rb
@@ -0,0 +1,59 @@
+module DataMapper
+ module Validate
+
+ class AcceptanceValidator < GenericValidator
+
+ def self.default_message_for_field(field_name)
+ '%s is not accepted'.t(DataMapper::Inflection.humanize(field_name))
+ end
+
+ def initialize(field_name, options = {})
+ super
+ @options = options
+ @field_name = field_name
+ @options[:allow_nil] = true unless @options.include?(:allow_nil)
+ @options[:accept] ||= "1"
+ @options[:accept] = Array(@options[:accept])
+ end
+
+ def call(target)
+ unless valid?(target)
+ error_message = @options[:message] || DataMapper::Validate::AcceptanceValidator.default_message_for_field(@field_name)
+ add_error(target, error_message , @field_name)
+ return false
+ end
+
+ return true
+ end
+
+ def valid?(target)
+ field_value = target.instance_variable_get("@#{@field_name}")
+ return true if @options[:allow_nil] && field_value.nil?
+ return false if !@options[:allow_nil] && field_value.nil?
+
+ @options[:accept].include?(field_value)
+ end
+
+ end
+
+
+ module ValidatesAcceptanceOf
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+
+ def validates_acceptance_of(*fields)
+ opts = opts_from_validator_args(fields)
+ add_validator_to_context(opts, fields, DataMapper::Validate::AcceptanceValidator)
+ end
+ end
+
+ end
+
+
+
+
+ end
+end

0 comments on commit 102fc61

Please sign in to comment.