Commit 498b0dadf4855e8547d25450c4f288f19e854186
1 parent
6edafc75
Exists in
master
and in
26 other branches
Refactor profile identifier validation to put the code in a single place
Showing
1 changed file
with
12 additions
and
5 deletions
Show diff stats
app/models/profile.rb
@@ -317,16 +317,23 @@ class Profile < ActiveRecord::Base | @@ -317,16 +317,23 @@ class Profile < ActiveRecord::Base | ||
317 | @top_level_articles ||= Article.top_level_for(self) | 317 | @top_level_articles ||= Article.top_level_for(self) |
318 | end | 318 | end |
319 | 319 | ||
320 | - def self.is_available?(identifier, environment) | ||
321 | - !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil? | 320 | + def self.is_available?(identifier, environment, profile_id=nil) |
321 | + return false unless identifier =~ IDENTIFIER_FORMAT && !RESERVED_IDENTIFIERS.include?(identifier) | ||
322 | + return true if environment.nil? | ||
323 | + | ||
324 | + profiles = environment.profiles.where(:identifier => identifier) | ||
325 | + profiles = profiles.where(['id != ?', profile_id]) if profile_id.present? | ||
326 | + !profiles.exists? | ||
322 | end | 327 | end |
323 | 328 | ||
324 | validates_presence_of :identifier, :name | 329 | validates_presence_of :identifier, :name |
325 | - validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? } | ||
326 | - validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS | ||
327 | - validates_uniqueness_of :identifier, :scope => :environment_id | ||
328 | validates_length_of :nickname, :maximum => 16, :allow_nil => true | 330 | validates_length_of :nickname, :maximum => 16, :allow_nil => true |
329 | validate :valid_template | 331 | validate :valid_template |
332 | + validate :valid_identifier | ||
333 | + | ||
334 | + def valid_identifier | ||
335 | + errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id) | ||
336 | + end | ||
330 | 337 | ||
331 | def valid_template | 338 | def valid_template |
332 | if template_id.present? && template && !template.is_template | 339 | if template_id.present? && template && !template.is_template |