Skip to content

Instantly share code, notes, and snippets.

@thomas-holmes
Created August 13, 2013 17:26
Show Gist options
  • Save thomas-holmes/6223517 to your computer and use it in GitHub Desktop.
Save thomas-holmes/6223517 to your computer and use it in GitHub Desktop.
class Company < ActiveRecord::Base
before_create :generate_api_token
attr_accessible :name, :owner, :owner_id
validates :name, presence: true, uniqueness: { case_sensitive: false }, :length => { :minimum => 5, :maximum => 40 }
validates :owner, presence: true
validates :owner_id, presence: true
has_many :orders
has_many :users
private
def generate_api_token
begin
self.token = SecureRandom.urlsafe_base64
end while self.class.exists?(token: self.token)
end
end
describe "when email is not downcased" do
before { @company.save }
it "should be saved as all lowercase" do
saved_company = Company.find(@company.id)
@company.name = "Another name"
@company.save
expect(@company.token).to eq saved_company.token
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment