Class: ActionSpec::ValidationResult

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Defined in:
lib/action_spec/validation_result.rb

Constant Summary collapse

BUILT_IN_SCOPES =
%i[path query body headers cookies].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidationResult

Returns a new instance of ValidationResult.



18
19
20
21
# File 'lib/action_spec/validation_result.rb', line 18

def initialize
  @errors = ActiveModel::Errors.new(self)
  @px = build_px
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/action_spec/validation_result.rb', line 16

def errors
  @errors
end

#pxObject (readonly)

Returns the value of attribute px.



16
17
18
# File 'lib/action_spec/validation_result.rb', line 16

def px
  @px
end

Class Method Details

.empty_pxObject



11
12
13
# File 'lib/action_spec/validation_result.rb', line 11

def empty_px
  new.px
end

.human_attribute_name(attribute, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/action_spec/validation_result.rb', line 65

def self.human_attribute_name(attribute, options = {})
  key = attribute.to_s
  defaults = [
    :"activemodel.attributes.#{model_name.i18n_key}.#{key}",
    :"activemodel.attributes.#{model_name.i18n_key}.#{key.tr('.', '_')}",
    key.tr(".", " ").humanize
  ]

  I18n.translate(defaults.shift, **options, default: defaults)
end

.lookup_ancestorsObject



57
58
59
# File 'lib/action_spec/validation_result.rb', line 57

def self.lookup_ancestors
  [self]
end

.model_nameObject



61
62
63
# File 'lib/action_spec/validation_result.rb', line 61

def self.model_name
  @model_name ||= ActiveModel::Name.new(self, nil, "ActionSpec::Parameters")
end

Instance Method Details

#add_error(attribute, type, message: nil, **options) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/action_spec/validation_result.rb', line 43

def add_error(attribute, type, message: nil, **options)
  return errors.add(attribute, message) if message

  if (message = ActionSpec.config.message_for(attribute, type, options))
    errors.add(attribute, message)
  else
    errors.add(attribute, type, **options)
  end
end

#apply_scope_options!(options_by_scope) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/action_spec/validation_result.rb', line 33

def apply_scope_options!(options_by_scope)
  options_by_scope.each do |scope_name, options|
    bucket = px.scope[scope_name]
    next unless bucket

    bucket.compact! if options[:compact]
    bucket.delete_if { |_key, value| value.blank? } if options[:compact_blank]
  end
end

#assign(location, key, value, scopes: []) ⇒ Object



27
28
29
30
31
# File 'lib/action_spec/validation_result.rb', line 27

def assign(location, key, value, scopes: [])
  bucket(location)[key] = value
  px[key] = value if root_bucket?(location)
  Array(scopes).each { |scope_name| scope_bucket(scope_name)[key] = value }
end

#invalid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/action_spec/validation_result.rb', line 23

def invalid?
  errors.any?
end

#read_attribute_for_validation(_attribute) ⇒ Object



53
54
55
# File 'lib/action_spec/validation_result.rb', line 53

def read_attribute_for_validation(_attribute)
  nil
end