Module: Spira::Validations::ClassMethods

Defined in:
lib/spira/validations.rb,
lib/spira/validations/uniqueness.rb

Instance Method Summary collapse

Instance Method Details

#create!(properties = {}, options = {}, &block) ⇒ Object

Creates an object just like Base.create but calls save! instead of save so an exception is raised if the record is invalid.



30
31
32
33
34
35
36
37
38
39
# File 'lib/spira/validations.rb', line 30

def create!(properties = {}, options = {}, &block)
  if properties.is_a?(Array)
    properties.collect { |attr| create!(attr, options, &block) }
  else
    object = new(properties, options)
    yield(object) if block_given?
    object.save!
    object
  end
end

#validates_uniqueness_of(*attr_names) ⇒ Object

Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named “davidhh”.

class Person < Spira::Base validates_uniqueness_of :user_name end



38
39
40
# File 'lib/spira/validations/uniqueness.rb', line 38

def validates_uniqueness_of(*attr_names)
  validates_with UniquenessValidator, _merge_attributes(attr_names)
end