Commit f5a16663f0b038aeb397bda19ebdefa6ad873955

Authored by miks
1 parent e3e9db95

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,14 +13,19 @@ class Key < ActiveRecord::Base
13 :length => { :within => 0..5000 } 13 :length => { :within => 0..5000 }
14 14
15 before_save :set_identifier 15 before_save :set_identifier
  16 + before_validation :strip_white_space
16 after_save :update_repository 17 after_save :update_repository
17 after_destroy :repository_delete_key 18 after_destroy :repository_delete_key
18 validate :unique_key 19 validate :unique_key
19 20
  21 + def strip_white_space
  22 + self.key = self.key.strip
  23 + end
  24 +
20 def unique_key 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 errors.add :key, 'already exist.' 29 errors.add :key, 'already exist.'
25 end 30 end
26 end 31 end