From f430aa7b12d7c630ef62a56ffbb623c1b841231a Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 8 Sep 2015 14:26:04 +0200 Subject: [PATCH] Generate client keys --- app/models/client.rb | 7 +++++++ spec/models/client_spec.rb | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/client.rb b/app/models/client.rb index 3a825c3..bb8f315 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -10,7 +10,14 @@ # class Client < ActiveRecord::Base + before_create :generate_key + def transactions Transaction.where(origin: name) end + + private + def generate_key + self.key = SecureRandom.base64(16) unless self.key + end end diff --git a/spec/models/client_spec.rb b/spec/models/client_spec.rb index 640750d..33e905f 100644 --- a/spec/models/client_spec.rb +++ b/spec/models/client_spec.rb @@ -12,5 +12,11 @@ require 'rails_helper' RSpec.describe Client, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it "should have a valid factory" do + expect(create(:client)).to be_valid + end + + it "should generate a key" do + expect(create(:client).key).to be_present + end end