Class: SchemaExpectations::ActiveRecord::ValidationReflector
- Inherits:
-
Object
- Object
- SchemaExpectations::ActiveRecord::ValidationReflector
- Defined in:
- lib/schema_expectations/active_record/validation_reflector.rb
Overview
:nodoc:
Constant Summary collapse
- CONDITIONAL_OPTIONS =
%i(on if unless)
- ALLOW_NIL_OPTIONS =
%i(allow_nil allow_blank)
- ALLOW_EMPTY_OPTIONS =
%i(allow_blank)
Instance Method Summary collapse
- #absence ⇒ Object
- #allow_empty_conditions_for_attribute(attribute) ⇒ Object
- #allow_nil_conditions_for_attribute(attribute) ⇒ Object
- #attributes ⇒ Object
- #conditions_for_attribute(attribute) ⇒ Object
- #disallow_empty ⇒ Object
- #disallow_nil ⇒ Object
- #for_unique_scope(scope) ⇒ Object
-
#initialize(model, validators = nil) ⇒ ValidationReflector
constructor
A new instance of ValidationReflector.
- #presence ⇒ Object
- #unconditional ⇒ Object
- #unique_scopes ⇒ Object
Constructor Details
#initialize(model, validators = nil) ⇒ ValidationReflector
Returns a new instance of ValidationReflector.
10 11 12 13 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 10 def initialize(model, validators = nil) @model = model @validators = validators || model.validators end |
Instance Method Details
#absence ⇒ Object
40 41 42 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 40 def absence new_with_validators validators_with_kind :absence end |
#allow_empty_conditions_for_attribute(attribute) ⇒ Object
32 33 34 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 32 def allow_empty_conditions_for_attribute(attribute) attribute, ALLOW_EMPTY_OPTIONS end |
#allow_nil_conditions_for_attribute(attribute) ⇒ Object
28 29 30 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 28 def allow_nil_conditions_for_attribute(attribute) attribute, ALLOW_NIL_OPTIONS end |
#attributes ⇒ Object
15 16 17 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 15 def attributes @validators.flat_map(&:attributes).uniq end |
#conditions_for_attribute(attribute) ⇒ Object
24 25 26 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 24 def conditions_for_attribute(attribute) attribute, CONDITIONAL_OPTIONS end |
#disallow_empty ⇒ Object
52 53 54 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 52 def disallow_empty new_with_validators ALLOW_EMPTY_OPTIONS end |
#disallow_nil ⇒ Object
48 49 50 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 48 def disallow_nil new_with_validators ALLOW_NIL_OPTIONS end |
#for_unique_scope(scope) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 56 def for_unique_scope(scope) scope = scope.sort validators = validators_with_kind :uniqueness validators.select! do |validator| validator_scopes(validator).include? scope end new_with_validators validators end |
#presence ⇒ Object
36 37 38 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 36 def presence new_with_validators validators_with_kind :presence end |
#unconditional ⇒ Object
44 45 46 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 44 def unconditional new_with_validators CONDITIONAL_OPTIONS end |
#unique_scopes ⇒ Object
19 20 21 22 |
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 19 def unique_scopes validators = validators_with_kind :uniqueness validators.flat_map(&method(:validator_scopes)) end |