2011-03-27 23:49:12 +00:00
|
|
|
module Locomotive
|
2011-04-01 00:34:19 +00:00
|
|
|
module Hosting
|
2011-03-27 23:49:12 +00:00
|
|
|
module Bushido
|
|
|
|
module CustomDomain
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2011-03-31 07:10:42 +00:00
|
|
|
validate :subdomain_availability
|
|
|
|
|
2011-04-06 23:34:36 +00:00
|
|
|
before_update :check_subdomain_change
|
2011-04-05 00:18:17 +00:00
|
|
|
|
2011-03-27 23:49:12 +00:00
|
|
|
after_save :add_bushido_domains
|
2011-04-05 00:18:17 +00:00
|
|
|
after_update :record_new_subdomain
|
2011-03-27 23:49:12 +00:00
|
|
|
after_destroy :remove_bushido_domains
|
|
|
|
|
|
|
|
alias_method_chain :add_subdomain_to_domains, :bushido
|
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-03-31 07:10:42 +00:00
|
|
|
def subdomain_availability
|
2011-04-07 00:18:07 +00:00
|
|
|
return true if self.new_record? || !self.subdomain_changed?
|
2011-04-06 23:34:36 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
unless ::Bushido::App.subdomain_available?(self.subdomain)
|
2011-03-31 07:10:42 +00:00
|
|
|
self.errors.add(:subdomain, :exclusion)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-27 23:49:12 +00:00
|
|
|
def add_subdomain_to_domains_with_bushido
|
|
|
|
unless self.domains_change.nil?
|
2011-04-24 23:21:38 +00:00
|
|
|
old_full_subdomain = "#{self.subdomain_was}.#{Locomotive.config.domain}"
|
2011-04-01 00:34:19 +00:00
|
|
|
full_subdomain = "#{self.subdomain}.#{Locomotive.config.domain}"
|
2011-04-24 23:21:38 +00:00
|
|
|
|
2011-03-27 23:49:12 +00:00
|
|
|
@bushido_domains_change = {
|
2011-04-24 23:21:38 +00:00
|
|
|
:added => self.domains_change.last - self.domains_change.first - [old_full_subdomain] - [full_subdomain],
|
|
|
|
:removed => self.domains_change.first - self.domains_change.last - [old_full_subdomain] - [full_subdomain]
|
2011-03-27 23:49:12 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
add_subdomain_to_domains_without_bushido
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_bushido_domains
|
|
|
|
return if @bushido_domains_change.nil?
|
|
|
|
|
|
|
|
@bushido_domains_change[:added].each do |name|
|
|
|
|
Locomotive.add_bushido_domain(name)
|
|
|
|
end
|
|
|
|
@bushido_domains_change[:removed].each do |name|
|
|
|
|
Locomotive.remove_bushido_domain(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_bushido_domains
|
|
|
|
self.domains_without_subdomain.each do |name|
|
|
|
|
Locomotive.remove_bushido_domain(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-05 00:18:17 +00:00
|
|
|
def check_subdomain_change
|
|
|
|
@new_bushido_subdomain = !Locomotive.config.multi_sites? && self.subdomain_changed?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def record_new_subdomain
|
|
|
|
if @new_bushido_subdomain == true
|
|
|
|
Locomotive.set_bushido_subdomain(self.subdomain)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-27 23:49:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|