Skip to content

Commit

Permalink
Added the ability to inherit a validator based on the :on option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Smith committed Feb 13, 2014
1 parent 5c13fff commit db24519
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ form.errors.messages # => { name: ["can't be blank"] }

```use_database_default``` - Use the value of the ```default``` migration option as the default value for an attribute. Accepts either a boolean (for all attributes), a string or symbol for a single attribute or an array of strings and symbols for multiple attributes.

```additional_attributes``` - Used to specify attributes that cannot automatically be added to the form model. These are generally attributes that have been specified using ```attr_accessor```. Accepts a string, symbolr or an array of strings and symbols for multiple attributes.
```additional_attributes``` - Used to specify attributes that cannot automatically be added to the form model. These are generally attributes that have been specified using ```attr_accessor```. Accepts a string, symbol or an array of strings and symbols for multiple attributes.

```validate_attribute_on``` - By default any validation that specifies an ```:on``` option will not be inherited. This option will allow you to inherit a validator that uses the ```:on``` with a specific value. Example usage: ```validate_password_on: :create`. Accepts a string or symbol. This option will accept any value that the Rails ```:on``` validator option can accept.

### I18n

Expand Down
25 changes: 18 additions & 7 deletions lib/soulless/inheritance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ def get_active_record_attributes(klass, options)
end

def setup_validators(attribute_name, klass, options)
return if options[:skip_validators] || !include_attribute?(attribute_name, options)
return if skip_validators?(attribute_name, options) || !include_attribute?(attribute_name, options)
klass.validators.each do |validator|
if validator.attributes.include?(attribute_name.to_sym)
validator_class = validator.class
validator_options = {}
validator_options.merge!(validator.options)
next if validator_options.include?(:on) && options["validate_#{attribute_name}_on".to_sym] != validator_options[:on]
validator_options.delete(:on)
if validator_class == ActiveRecord::Validations::UniquenessValidator
validator_class = Validations::UniquenessValidator
validator_options.merge!(model: klass)
Expand All @@ -87,6 +89,14 @@ def setup_validators(attribute_name, klass, options)
end
end

def get_attribute_names(attributes, options)
attribute_names = attributes
attribute_names << options[:additional_attributes] if options[:additional_attributes]
attribute_names.flatten!
attribute_names.map!{ |a| a.to_s }
attribute_names
end

def translate_primitive(primitive)
return nil unless primitive
translated_primitive = primitive.to_s.capitalize
Expand All @@ -112,12 +122,13 @@ def include_attribute?(attribute_name, options)
!exclude_attributes.include?(attribute_name) && (only_attributes.empty? || only_attributes.include?(attribute_name))
end

def get_attribute_names(attributes, options)
attribute_names = attributes
attribute_names << options[:additional_attributes] if options[:additional_attributes]
attribute_names.flatten!
attribute_names.map!{ |a| a.to_s }
attribute_names
def skip_validators?(attribute_name, options)
return true if options[:skip_validators] == true
skip_validators = []
skip_validators << options[:skip_validators]
skip_validators.flatten!
skip_validators.collect!{ |v| v.to_s }
skip_validators.include?(attribute_name)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/soulless/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Soulless
VERSION = "0.5.0.rc3"
VERSION = "0.5.0.rc4"
end

0 comments on commit db24519

Please sign in to comment.