Skip to content
Browse files

support array for conditions. implement special condition :changed.

  • Loading branch information...
1 parent e08bb83 commit 1b63ae9a98b284316d779ab64993b77e1d4cf442 @netskin-ci netskin-ci committed Sep 29, 2010
View
29 lib/sequel_sexy_validations.rb
@@ -12,14 +12,14 @@ def self.configure(model, opts={}, &block)
module ClassMethods
def validates(attribute = nil, validations = nil, &block)
if validations
- case validations[:if]
- when :new
- validations[:if] = lambda { |record| record.new? }
+ if validations[:if]
+ validations[:if] = [validations[:if]] unless validations[:if].is_a?(Array)
+ implement_special_validation_conditions!(validations[:if])
end
- case validations[:unless]
- when :new
- validations[:unless] = lambda { |record| record.new? }
+ if validations[:unless]
+ validations[:unless] = [validations[:unless]] unless validations[:unless].is_a?(Array)
+ implement_special_validation_conditions!(validations[:unless])
end
end
@@ -34,6 +34,23 @@ def load_validator(name)
super
end
end
+
+ private
+
+ def implement_special_validation_conditions!(validations)
+ validations.each_with_index do |validation, i|
+ case validation
+ when :new
+ validations[i] = lambda do |record, attribute|
+ record.new?
+ end
+ when :changed
+ validations[i] = lambda do |record, attribute|
+ record.changed_columns.include?(attribute)
+ end
+ end
+ end
+ end
end
module InstanceMethods
View
23 lib/sequel_sexy_validations/validators/presence.rb
@@ -0,0 +1,23 @@
+# encoding: utf-8
+require "sexy_validations/validators/presence"
+
+module Sequel
+ module Plugins
+ module SexyValidations
+ module Validators
+ class Presence < ::SexyValidations::Validators::Presence
+ def self.validate(model, attribute, value, options)
+ real_value = case value.class.name
+ when "Sequel::Plugins::Paperclip::Attachment"
+ value.queued_file
+ else
+ value
+ end
+ super(model, attribute, real_value, options)
+ end
+ end
+ end
+ end
+ end
+end
+
View
2 lib/sequel_sexy_validations/validators/uniqueness.rb
@@ -7,8 +7,6 @@ class Uniqueness
def self.validate(model, attribute, value, options)
return unless value
- return unless model.changed_columns.include?(attribute)
-
unless options.is_a?(Hash)
options = {
:scope => options,

0 comments on commit 1b63ae9

Please sign in to comment.
Something went wrong with that request. Please try again.