class Stocks::Validators::Exists

An ActiveRecord validator which validates that an attribute on a model is a valid ticker symbol. The default attribute is :symbol but may be specified by defining symbol_field when declaring the validator.

Example Usage:

require 'stocks/validators/exists'

class Position < ActiveRecord::Base
  validates_with Stocks::Validators::Exists, symbol_field: :field_name
end

Public Class Methods

message(symbol) click to toggle source

Generates the error message that is inserted into record.errors.

Args
  • symbol The symbol to insert into the error message

Returns

An error message representing the validation failure

# File lib/stocks/validators/exists.rb, line 27
def self.message(symbol)
  ERROR_MESSAGE % symbol
end

Public Instance Methods

validate(record) click to toggle source

Validates that a record has a valid symbol.

Args
  • record The record to validate

# File lib/stocks/validators/exists.rb, line 35
def validate(record)
  field = options[:symbol_field] || :symbol
  symbol = record.send(field)
  if (!Stocks.exists?(symbol))
    record.errors[field] << self.class.message(symbol)
  end
end