Class: Phillumeny::ActiveModel::ValidatePresenceOfAny

Inherits:
Object
  • Object
show all
Defined in:
lib/phillumeny/active_model.rb

Overview

Used for testing conditional validation of presence where you need one of the other

Examples:


class Webpage

  include ActiveModel::Validations

  attr_accessor :body
  attr_accessor :headline
  attr_accessor :title

  validates :body,
            presence: true

  validates :headline,
            presence: true,
            unless: -> { title.present? }

  validates :title,
            presence: true,
            inclusion: { allow_blank: true, in: ['this'] },
            unless: -> { headline.present? && headline != 'needs title' }

end

RSpec.describe Webpage, type: :model do

  it { should     validate_presence_of_any(:headline, :title).valid_values(title: 'this') }
  it { should_not validate_presence_of_any(:headline, :title) }
  it { should_not validate_presence_of_any(:headline, :title).valid_value(:title, 'that') }

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up arguments for the matcher



77
78
79
# File 'lib/phillumeny/active_model.rb', line 77

def initialize(args)
  @attributes = args
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



47
48
49
# File 'lib/phillumeny/active_model.rb', line 47

def attributes
  @attributes
end

#subjectObject (readonly)

Returns the value of attribute subject.



48
49
50
# File 'lib/phillumeny/active_model.rb', line 48

def subject
  @subject
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Description used when matcher is used

Returns:

  • (String)


55
56
57
# File 'lib/phillumeny/active_model.rb', line 55

def description
  "validate the presence of any of these attributes: #{attributes.join(', ')}"
end

#failure_messageString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compiles the error message for display

Returns:

  • (String)


64
65
66
67
68
69
70
# File 'lib/phillumeny/active_model.rb', line 64

def failure_message
  messages = [subject.inspect]
  attributes.each do |attribute|
    messages << subject.errors.full_messages_for(attribute)
  end
  messages.compact.join("\n")
end

#matches?(subject) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the logic to determine if expectations are being met

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
# File 'lib/phillumeny/active_model.rb', line 86

def matches?(subject)
  @subject = subject
  store_initial_values
  invalid_when_none_present? && attributes.all? do |attribute|
    clear_attributes_and_errors
    initialize_value(attribute)
    free_of_errors_on_attribute?(attribute) ? other_attributes_valid?(attribute) : false
  end
end

#valid_value(attribute, value) ⇒ self

Explicitly set the value to test for an attribute if ‘X’ is not acceptable

Examples:

it { should validate_presence_of_any(:headline, :title).valid_value(:title, 'A valid title') }

Returns:

  • (self)


104
105
106
107
# File 'lib/phillumeny/active_model.rb', line 104

def valid_value(attribute, value)
  attribute_values[attribute] = value
  self
end

#valid_values(**values) ⇒ self

Explicitly set the value to test for an attribute if ‘X’ is not acceptable

Examples:

it do
  should validate_presence_of_any(:headline, :title).valid_values(title: 'A valid title')
end

Returns:

  • (self)


119
120
121
122
# File 'lib/phillumeny/active_model.rb', line 119

def valid_values(**values)
  attribute_values.merge!(values)
  self
end