From 0e6b2f884f2238aa64b0d252bc995262f3ecabb3 Mon Sep 17 00:00:00 2001 From: Sean Grove Date: Mon, 21 Mar 2011 02:32:34 -0700 Subject: [PATCH] Add in programmatic support for bushido's custom domains/subdomains --- lib/locomotive/bushido.rb | 46 ++++++++++++++++++++++ lib/locomotive/bushido/custom_domain.rb | 52 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 lib/locomotive/bushido.rb create mode 100644 lib/locomotive/bushido/custom_domain.rb diff --git a/lib/locomotive/bushido.rb b/lib/locomotive/bushido.rb new file mode 100644 index 00000000..aa59df7f --- /dev/null +++ b/lib/locomotive/bushido.rb @@ -0,0 +1,46 @@ +require 'bushido' +require 'locomotive/bushido/custom_domain' + +module Locomotive + module Bushido + + extend ActiveSupport::Concern + + included do + class << self + attr_accessor :bushido_domains + end + end + + module ClassMethods + + def bushido? + ENV["HOSTING_PLATFORM"] == "bushido" + end + + def enable_bushido + self.enhance_site_model + end + + def enhance_site_model + Site.send :include, Locomotive::Bushido::CustomDomain + end + + # manage domains + + def add_bushido_domain(name) + Locomotive.logger "[add bushido domain] #{name}" + Bushido::Domains.add_subdomain(name) + self.bushido_domains << name + end + + def remove_bushido_domain(name) + Locomotive.logger "[remove bushido domain] #{name}" + Bushido::Domains.remove_subdomain(name) + self.bushido_domains.delete(name) + end + + end + + end +end diff --git a/lib/locomotive/bushido/custom_domain.rb b/lib/locomotive/bushido/custom_domain.rb new file mode 100644 index 00000000..5d399e41 --- /dev/null +++ b/lib/locomotive/bushido/custom_domain.rb @@ -0,0 +1,52 @@ +module Locomotive + module Bushido + module CustomDomain + + extend ActiveSupport::Concern + + included do + + after_save :add_bushido_domains + after_destroy :remove_bushido_domains + + alias_method_chain :add_subdomain_to_domains, :bushido + end + + module InstanceMethods + + protected + + def add_subdomain_to_domains_with_bushido + unless self.domains_change.nil? + full_subdomain = "#{self.subdomain}.#{Locomotive.config.default_domain}" + @bushido_domains_change = { + :added => self.domains_change.last - self.domains_change.first - [full_subdomain], + :removed => self.domains_change.first - self.domains_change.last - [full_subdomain] + } + 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 + + end + + end + end +end