Class: SchemaExpectations::ActiveRecord::ValidationReflector

Inherits:
Object
  • Object
show all
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

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

#absenceObject



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)
  options_for_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)
  options_for_attribute attribute, ALLOW_NIL_OPTIONS
end

#attributesObject



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)
  options_for_attribute attribute, CONDITIONAL_OPTIONS
end

#disallow_emptyObject



52
53
54
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 52

def disallow_empty
  new_with_validators validators_without_options ALLOW_EMPTY_OPTIONS
end

#disallow_nilObject



48
49
50
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 48

def disallow_nil
  new_with_validators validators_without_options 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

#presenceObject



36
37
38
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 36

def presence
  new_with_validators validators_with_kind :presence
end

#unconditionalObject



44
45
46
# File 'lib/schema_expectations/active_record/validation_reflector.rb', line 44

def unconditional
  new_with_validators validators_without_options CONDITIONAL_OPTIONS
end

#unique_scopesObject



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