app/models/account_domain.rb in iugusdk-1.0.0.alpha.1 vs app/models/account_domain.rb in iugusdk-1.0.0.alpha.2
- old
+ new
@@ -3,10 +3,15 @@
class AccountDomain < ActiveRecord::Base
belongs_to :account
validates :url, :account_id, :presence => true
validate :validate_pattern, :validate_blacklist
+ before_create :validate_not_repeated
+
+ before_destroy { |record| record.update_attributes(:verified => false, :primary => false) }
+ before_destroy :set_first_domain
+
scope :verified, where(:verified => true)
def normalize_host
begin
normalized_url = url
@@ -39,16 +44,19 @@
checked = true if response.code == "200"
rescue
end
AccountDomain.where(:url => self.url).update_all(:verified => false) if checked == true
update_attribute(:verified, checked)
+ set_first_domain
checked
end
def set_primary
- AccountDomain.where(:account_id => account_id).update_all(:primary => false)
- update_attribute(:primary, true)
+ if verified == true
+ AccountDomain.where(:account_id => account_id).update_all(:primary => false)
+ update_attribute(:primary, true)
+ end
end
private
def validate_pattern
@@ -60,9 +68,24 @@
def validate_blacklist
if url
IuguSDK::custom_domain_invalid_hosts.each do |invalid_host|
errors.add(:url, "Domain on Blacklist") if url == invalid_host
end
+ end
+ end
+
+ def validate_not_repeated
+ if url
+ if !AccountDomain.where(:account_id => account_id, :url => url).empty?
+ errors.add(:url, "already used for this account")
+ false
+ end
+ end
+ end
+
+ def set_first_domain
+ if AccountDomain.where(:account_id => account_id, :primary => true).empty?
+ AccountDomain.where(:account_id => account_id, :verified => true).first.try(:update_attribute, :primary, true)
end
end
end