module Scrivener::Validations
Public Instance Methods
assert_confirmation(att, error = [att, :not_confirmed])
click to toggle source
Encapsulates the pattern of wanting to validate a password or email address field with a confirmation.
class SignUp < Scrivener attr_accessor :password, :password_confirmation def validate assert_present(:password) assert_confirmation(:password) end end signup = SignUp.new signup.password = "123456" signup.password_confirmation = "nonsense" signup.valid? # => false signup.errors[:password] # => [:not_confirmed]
# File lib/scrivener/contrib.rb, line 76 def assert_confirmation(att, error = [att, :not_confirmed]) assert(send(:"#{att}_confirmation") == send(att), error) end
assert_exact_length(att, val, error = [att, :wrong_length])
click to toggle source
Validates that the specified attribute matches exactly the length restriction supplied.
class SignUp < Scrivener attr_accessor :status def validate assert_exact_length(:status, 1) end end signup = Signup.new(status: "10") signup.valid? # => false signup.errors[:status] # => [:wrong_length]
# File lib/scrivener/contrib.rb, line 54 def assert_exact_length(att, val, error = [att, :wrong_length]) assert(send(att).to_s.length == val, error) end
assert_maximum_length(att, val, error = [att, :too_long])
click to toggle source
Validates that maximum size of the specified attribute.
class SignUp < Scrivener attr_accessor :password def validate assert_minimum_length(:password, 72) end end signup = Signup.new(password: _very_long_password) signup.valid? # => false signup.errors[:password] # => [:too_long]
# File lib/scrivener/contrib.rb, line 35 def assert_maximum_length(att, val, error = [att, :too_long]) assert(send(att).to_s.length <= val, error) end
assert_minimum_length(att, val, error = [att, :too_short])
click to toggle source
Validates that minimum size of the specified attribute.
class SignUp < Scrivener attr_accessor :password def validate assert_minimum_length(:password, 8) end end signup = Signup.new(password: "short") signup.valid? # => false signup.errors[:password] # => [:too_short]
# File lib/scrivener/contrib.rb, line 17 def assert_minimum_length(att, val, error = [att, :too_short]) assert(send(att).to_s.length >= val, error) end
assert_unique(att, model, error = [att, :not_unique])
click to toggle source
Validates that an attribute is unique.
class User < Ohm::Model attribute :email unique :email end class SignUp < Scrivener attr_accessor :email def validate assert_unique(:email, :User) end end user = User.create(email: "mayn.kjaer@gmail.com") signup = SignUp.new(email: user.email) signup.valid? # => false signup.errors[:email] # => [:not_unique]
# File lib/scrivener/ohm.rb, line 26 def assert_unique(att, model, error = [att, :not_unique]) model = Utils.const(self.class, model) record = model.with(att, send(att)) assert(record.nil?, error) end