From feb46f687b2c2a1d62dbaa59e9d87f229c52dc5a Mon Sep 17 00:00:00 2001 From: lukemelia Date: Fri, 30 Mar 2007 12:34:42 +0000 Subject: [PATCH] copy extra_validations plugin from branch git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@507 a4c988fc-2ded-0310-b66e-134b36920a42 --- .../vendor/plugins/extra_validations/init.rb | 2 ++ .../lib/extra_validations.rb | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tracks/vendor/plugins/extra_validations/init.rb create mode 100644 tracks/vendor/plugins/extra_validations/lib/extra_validations.rb diff --git a/tracks/vendor/plugins/extra_validations/init.rb b/tracks/vendor/plugins/extra_validations/init.rb new file mode 100644 index 00000000..17709ad4 --- /dev/null +++ b/tracks/vendor/plugins/extra_validations/init.rb @@ -0,0 +1,2 @@ +require 'extra_validations' +ActiveRecord::Base.extend ExtraValidations \ No newline at end of file diff --git a/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb b/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb new file mode 100644 index 00000000..f5486e96 --- /dev/null +++ b/tracks/vendor/plugins/extra_validations/lib/extra_validations.rb @@ -0,0 +1,29 @@ +module ExtraValidations + + # Validates the value of the specified attribute by checking for a forbidden string + # + # class Person < ActiveRecord::Base + # validates_does_not_contain :first_name, :string => ',' + # end + # + # A string must be provided or else an exception will be raised. + # + # Configuration options: + # * message - A custom error message (default is: "is invalid") + # * string - The string to verify is not included (note: must be supplied!) + # * on Specifies when this validation is active (default is :save, other options :create, :update) + # * if - Specifies a method, proc or string to call to determine if the validation should + # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The + # method, proc or string should return or evaluate to a true or false value. + def validates_does_not_contain(*attr_names) + configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :string => nil } + configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) + + raise(ArgumentError, "A string must be supplied as the :string option of the configuration hash") unless configuration[:string].is_a?(String) + + validates_each(attr_names, configuration) do |record, attr_name, value| + record.errors.add(attr_name, configuration[:message]) if value.to_s =~ Regexp.new(Regexp.escape(configuration[:string])) + end + end + +end