lib/shamu/attributes/validation.rb in shamu-0.0.13 vs lib/shamu/attributes/validation.rb in shamu-0.0.14
- old
+ new
@@ -44,11 +44,15 @@
# validates :email, presence: true
def attribute( name, *args, **options, &block )
super
validation_options = options.each_with_object({}) do |(key, value), opts|
- opts[key] = value unless attribute_option_keys.include?( key )
+ next if attribute_option_keys.include?( key )
+
+ validator = "#{ key.to_s.camelize }Validator"
+ key = "shamu/attributes/validators/#{ key }" if Shamu::Attributes::Validators.const_defined?( validator.to_sym ) # rubocop:disable Metrics/LineLength
+ opts[ key ] = value
end
validates name, validation_options if validation_options.any?
end
end
@@ -65,9 +69,17 @@
# Validate the attributes and expose any errors via {#errors}.
def validate
@validated = true
run_validations!
end
+
+ private
+
+ def assign_attribute!( * )
+ # If any attribute changes we should re-run the validations
+ @validated = false
+ super
+ end
end
end
end
end