From a01e204d29b4e0c083877df97fb7dc8039f09978 Mon Sep 17 00:00:00 2001 From: did Date: Thu, 28 Apr 2011 17:04:18 +0200 Subject: [PATCH] refactor the way locomotive was updating a template and its descendants + add custom action_mailer settings for the bushi.do env + fix bug about index and 404 pages positions during the import + fix a little display issue when a subdomain is wrong in the site edit page --- app/models/extensions/page/parse.rb | 20 +++++++------------ .../extensions/site/subdomain_domains.rb | 6 +++++- app/models/snippet.rb | 6 ++++-- doc/TODO | 2 +- lib/locomotive/hosting/bushido.rb | 20 +++++++++++++++++-- .../hosting/bushido/custom_domain.rb | 7 ++----- .../hosting/heroku/custom_domain.rb | 7 ++----- lib/locomotive/import/pages.rb | 4 ++-- lib/locomotive/mongoid/patches.rb | 16 +++++++++++++++ 9 files changed, 57 insertions(+), 31 deletions(-) diff --git a/app/models/extensions/page/parse.rb b/app/models/extensions/page/parse.rb index a9086f13..e95f0772 100644 --- a/app/models/extensions/page/parse.rb +++ b/app/models/extensions/page/parse.rb @@ -80,21 +80,15 @@ module Extensions # group them by fullpath for better performance cached = template_descendants.inject({}) { |memo, page| memo[page.fullpath] = page; memo } - self._update_direct_template_descendants(template_descendants, cached) - - # finally save them all - template_descendants.map(&:save) - end - - def _update_direct_template_descendants(template_descendants, cached) - direct_descendants = template_descendants.select do |page| - ((page.template_dependencies || []) - (self.template_dependencies || [])).size == 1 + template_descendants.each do |page| + page.send(:_parse_and_serialize_template, { :cached_parent => self, :cached_pages => cached }) end - direct_descendants.each do |page| - page.send(:_parse_and_serialize_template, { :cached_parent => self, :cached_pages => cached }) - - page.send(:_update_direct_template_descendants, template_descendants, cached) + # finally save them all + ::Page.without_callback(:save, :after, :update_template_descendants) do + template_descendants.each do |page| + page.save(:validate => false) + end end end diff --git a/app/models/extensions/site/subdomain_domains.rb b/app/models/extensions/site/subdomain_domains.rb index 257933c0..aefdc227 100644 --- a/app/models/extensions/site/subdomain_domains.rb +++ b/app/models/extensions/site/subdomain_domains.rb @@ -46,7 +46,7 @@ module Extensions end def domains_without_subdomain - (self.domains || []) - [self.full_subdomain] + (self.domains || []) - [self.full_subdomain_was] - [self.full_subdomain] end def domains_with_subdomain @@ -57,6 +57,10 @@ module Extensions "#{self.subdomain}.#{Locomotive.config.domain}" end + def full_subdomain_was + "#{self.subdomain_was}.#{Locomotive.config.domain}" + end + protected def domains_must_be_valid_and_unique diff --git a/app/models/snippet.rb b/app/models/snippet.rb index c8a4fce4..a2d4f69f 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -37,9 +37,11 @@ class Snippet pages.each do |page| self._change_snippet_inside_template(page.template.root) - page.instance_variable_set(:@template_changed, true) + page.send(:_serialize_template) - page.send(:_serialize_template) && page.save + Page.without_callback(:save, :after, :update_template_descendants) do + page.save(:validate => false) + end end end diff --git a/doc/TODO b/doc/TODO index 2e0d02a9..6487944f 100644 --- a/doc/TODO +++ b/doc/TODO @@ -7,7 +7,7 @@ x bugs: - bushido version - upgrade warning if new version of locomotive (maybe based on the commit id) - duostack version -- 2 different sites on the same main domain (one in www, the other one in something else) (Raphael Costa) +x 2 different sites on the same main domain (one in www, the other one in something else) (Raphael Costa) - seo section for the page form: seo title, seo keywords, seo description - icon for redirection page in the pages section (back-office) - write my first tutorial about locomotive diff --git a/lib/locomotive/hosting/bushido.rb b/lib/locomotive/hosting/bushido.rb index aff74708..166d05de 100644 --- a/lib/locomotive/hosting/bushido.rb +++ b/lib/locomotive/hosting/bushido.rb @@ -27,6 +27,8 @@ module Locomotive self.enhance_site_model_with_bushido + self.setup_smtp_settings + self.bushido_domains = ::Bushido::App.domains self.bushido_subdomain = ::Bushido::App.subdomain end @@ -36,12 +38,25 @@ module Locomotive Site.send :include, Locomotive::Hosting::Bushido::FirstInstallation end + def setup_smtp_settings + ActionMailer::Base.delivery_method = :smtp + ActionMailer::Base.smtp_settings = { + :authentication => ENV['SMTP_AUTHENTICATION'], + :address => ENV['SMTP_SERVER'], + :port => ENV['SMTP_PORT'], + :domain => ENV['SMTP_DOMAIN'], + :user_name => ENV['SMTP_USER'], + :password => ENV['SMTP_PASSWORD'], + :enable_starttls_auto => ENV['SMTP_TLS'].to_s == 'true' + } + end + # manage domains def add_bushido_domain(name) Locomotive.logger "[add bushido domain] #{name}" ::Bushido::App.add_domain(name) - + if ::Bushido::Command.last_command_successful? self.bushido_domains << name end @@ -50,7 +65,7 @@ module Locomotive def remove_bushido_domain(name) Locomotive.logger "[remove bushido domain] #{name}" ::Bushido::App.remove_domain(name) - + if ::Bushido::Command.last_command_successful? self.bushido_domains.delete(name) end @@ -64,6 +79,7 @@ module Locomotive self.bushido_subdomain = name end end + end end end diff --git a/lib/locomotive/hosting/bushido/custom_domain.rb b/lib/locomotive/hosting/bushido/custom_domain.rb index 6055efcc..a3dc551f 100644 --- a/lib/locomotive/hosting/bushido/custom_domain.rb +++ b/lib/locomotive/hosting/bushido/custom_domain.rb @@ -31,12 +31,9 @@ module Locomotive def add_subdomain_to_domains_with_bushido unless self.domains_change.nil? - old_full_subdomain = "#{self.subdomain_was}.#{Locomotive.config.domain}" - full_subdomain = "#{self.subdomain}.#{Locomotive.config.domain}" - @bushido_domains_change = { - :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] + :added => self.domains_change.last - self.domains_change.first - [self.full_subdomain_was] - [self.full_subdomain], + :removed => self.domains_change.first - self.domains_change.last - [self.full_subdomain_was] - [self.full_subdomain] } end diff --git a/lib/locomotive/hosting/heroku/custom_domain.rb b/lib/locomotive/hosting/heroku/custom_domain.rb index 1370ef3f..236a5c1b 100644 --- a/lib/locomotive/hosting/heroku/custom_domain.rb +++ b/lib/locomotive/hosting/heroku/custom_domain.rb @@ -18,12 +18,9 @@ module Locomotive def add_subdomain_to_domains_with_heroku unless self.domains_change.nil? - old_full_subdomain = "#{self.subdomain_was}.#{Locomotive.config.domain}" - full_subdomain = "#{self.subdomain}.#{Locomotive.config.domain}" - @heroku_domains_change = { - :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] + :added => self.domains_change.last - self.domains_change.first - [self.full_subdomain_was] - [self.full_subdomain], + :removed => self.domains_change.first - self.domains_change.last - [self.full_subdomain_was] - [self.full_subdomain] } end diff --git a/lib/locomotive/import/pages.rb b/lib/locomotive/import/pages.rb index a10f1db6..3647af7b 100644 --- a/lib/locomotive/import/pages.rb +++ b/lib/locomotive/import/pages.rb @@ -5,6 +5,8 @@ module Locomotive def process context[:done] = {} # initialize the hash storing pages already processed + self.add_page('404') + self.add_page('index') Dir[File.join(theme_path, 'templates', '**/*')].each do |template_path| @@ -16,8 +18,6 @@ module Locomotive self.add_page(fullpath) end - self.add_page('404') - # make sure all the pages were processed (redirection pages without template for instance) self.pages.each { |fullpath, attributes| self.add_page_without_template(fullpath.to_s) } end diff --git a/lib/locomotive/mongoid/patches.rb b/lib/locomotive/mongoid/patches.rb index 86e8f738..0a172046 100644 --- a/lib/locomotive/mongoid/patches.rb +++ b/lib/locomotive/mongoid/patches.rb @@ -89,4 +89,20 @@ module Mongoid #:nodoc: end end + + + # without callback feature + module Callbacks + + module ClassMethods + + def without_callback(*args, &block) + skip_callback(*args) + yield + set_callback(*args) + end + + end + + end end \ No newline at end of file