Commit f5a16663f0b038aeb397bda19ebdefa6ad873955
1 parent
e3e9db95
Exists in
master
and in
4 other branches
Improved validation. Strip key.
Showing
1 changed file
with
8 additions
and
3 deletions
Show diff stats
app/models/key.rb
... | ... | @@ -13,14 +13,19 @@ class Key < ActiveRecord::Base |
13 | 13 | :length => { :within => 0..5000 } |
14 | 14 | |
15 | 15 | before_save :set_identifier |
16 | + before_validation :strip_white_space | |
16 | 17 | after_save :update_repository |
17 | 18 | after_destroy :repository_delete_key |
18 | 19 | validate :unique_key |
19 | 20 | |
21 | + def strip_white_space | |
22 | + self.key = self.key.strip | |
23 | + end | |
24 | + | |
20 | 25 | def unique_key |
21 | - query = 'key = ?' | |
22 | - query << ' AND project_id IS NULL' unless user_id | |
23 | - if (Key.where(query, key.strip).count > 0) | |
26 | + query = Key.where('key = ?', key) | |
27 | + query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id | |
28 | + if (query.count > 0) | |
24 | 29 | errors.add :key, 'already exist.' |
25 | 30 | end |
26 | 31 | end | ... | ... |