Use setter for key instead AR callback

ref: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6763
This commit is contained in:
Semyon Pupkov 2016-11-16 00:16:45 +05:00
parent d5cd0d67f1
commit 34a86120ed
3 changed files with 18 additions and 3 deletions

View file

@ -6,7 +6,7 @@ class Key < ActiveRecord::Base
belongs_to :user
before_validation :strip_white_space, :generate_fingerprint
before_validation :generate_fingerprint
validates :title, presence: true, length: { within: 0..255 }
validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }
@ -21,8 +21,9 @@ class Key < ActiveRecord::Base
after_destroy :remove_from_shell
after_destroy :post_destroy_hook
def strip_white_space
self.key = key.strip unless key.blank?
def key=(value)
value.strip! unless value.blank?
write_attribute(:key, value)
end
def publishable_key

View file

@ -0,0 +1,4 @@
---
title: Use setter for key instead AR callback
merge_request: 7488
author: Semyon Pupkov

View file

@ -82,4 +82,14 @@ describe Key, models: true do
@key.destroy
end
end
describe '#key=' do
let(:valid_key) do
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0= dummy@gitlab.com"
end
it 'strips white spaces' do
expect(described_class.new(key: " #{valid_key} ").key).to eq(valid_key)
end
end
end