Skip to content

Instantly share code, notes, and snippets.

@martinstreicher
Created September 23, 2016 20:08
Show Gist options
  • Save martinstreicher/c270005c38cfa874ca095c9ddf10ffbd to your computer and use it in GitHub Desktop.
Save martinstreicher/c270005c38cfa874ca095c9ddf10ffbd to your computer and use it in GitHub Desktop.
class Device < ActiveRecord::Base
belongs_to :participant
validates :participant, presence: true
validates :provider, presence: true
validates :serial_number, presence: true, uniqueness: { scope: :provider }
validates :serial_number, format: { with: /\A\d{15}\z/, message: 'IMEI must be 15 digits'},
if: lambda { |d| d.provider == 'bodytrace' }
before_validation :normalize
private
def normalize
self.provider = provider&.downcase
self.serial_number = self.class.normalize_serial_number serial_number
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment