1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/models/topic.rb
bogdanvlviv 3ff2c15837
Add cases to test combining validation conditions
- Test condition that is defined by array of conditions
 - Test condition that is defined by combining :if and :unless
 - Test local condition that is defined by :if
 - Test local condition that is defined by :unless

See http://edgeguides.rubyonrails.org/active_record_validations.html#combining-validation-conditions
2017-11-06 23:07:51 +00:00

41 lines
803 B
Ruby

# frozen_string_literal: true
class Topic
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
def self._validates_default_keys
super | [ :message ]
end
attr_accessor :title, :author_name, :content, :approved, :created_at
attr_accessor :after_validation_performed
after_validation :perform_after_validation
def initialize(attributes = {})
attributes.each do |key, value|
send "#{key}=", value
end
end
def condition_is_true
true
end
def condition_is_false
false
end
def perform_after_validation
self.after_validation_performed = true
end
def my_validation
errors.add :title, "is missing" unless title
end
def my_validation_with_arg(attr)
errors.add attr, "is missing" unless send(attr)
end
end