From 498b0dadf4855e8547d25450c4f288f19e854186 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 9 Feb 2015 11:07:49 -0300 Subject: [PATCH] Refactor profile identifier validation to put the code in a single place --- app/models/profile.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index 3cb60d8..8a0e46c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -317,16 +317,23 @@ class Profile < ActiveRecord::Base @top_level_articles ||= Article.top_level_for(self) end - def self.is_available?(identifier, environment) - !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil? + def self.is_available?(identifier, environment, profile_id=nil) + return false unless identifier =~ IDENTIFIER_FORMAT && !RESERVED_IDENTIFIERS.include?(identifier) + return true if environment.nil? + + profiles = environment.profiles.where(:identifier => identifier) + profiles = profiles.where(['id != ?', profile_id]) if profile_id.present? + !profiles.exists? end validates_presence_of :identifier, :name - validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? } - validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS - validates_uniqueness_of :identifier, :scope => :environment_id validates_length_of :nickname, :maximum => 16, :allow_nil => true validate :valid_template + validate :valid_identifier + + def valid_identifier + errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id) + end def valid_template if template_id.present? && template && !template.is_template -- libgit2 0.21.2