This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
| 632f7735 » | technoweenie | 2007-11-23 | 1 | require 'uri' | |
| 2 | |||||
| 3fd67d2e » | technoweenie | 2006-02-10 | 3 | class Site < ActiveRecord::Base | |
| 1b252a48 » | technoweenie | 2006-12-03 | 4 | @@default_assigns = {} | |
| 5 | @@theme_path = Pathname.new(RAILS_ROOT) + 'themes' | ||||
| 6 | cattr_reader :theme_path, :default_assigns | ||||
| 9e355018 » | technoweenie | 2006-09-09 | 7 | ||
| 4231359d » | technoweenie | 2006-09-25 | 8 | cattr_accessor :multi_sites_enabled, :cache_sweeper_tracing | |
| a5750df3 » | technoweenie | 2007-02-12 | 9 | ||
| 10 | # @@template_handlers = HashWithIndifferentAccess.new if @@template_handlers.nil? | ||||
| 11 | @@template_handlers = {} | ||||
| 12 | |||||
| 13 | # Register a class that knows how to handle template files with the given | ||||
| 14 | # extension. This can be used to implement new template types. | ||||
| 15 | # The constructor for the class must take a Site instance | ||||
| 16 | # as a parameter, and the class must implement a #render method that | ||||
| 17 | # has the following signature | ||||
| 18 | # def render(section, layout, template, assigns ={}, controller = nil) | ||||
| 19 | # and return the rendered template as a string. | ||||
| 20 | def self.register_template_handler(extension, klass) | ||||
| 21 | @@template_handlers[extension] = klass | ||||
| 22 | end | ||||
| 23 | register_template_handler(".liquid", Mephisto::Liquid::LiquidTemplate) | ||||
| 24 | |||||
| 25 | def self.extensions | ||||
| 26 | @@template_handlers.keys | ||||
| 27 | end | ||||
| 7708bbe9 » | technoweenie | 2006-08-13 | 28 | ||
| 09ecfabb » | francois | 2008-03-03 | 29 | @@spam_detection_engines = [] | |
| 30 | def self.register_spam_detection_engine(name, klass) | ||||
| 31 | @@spam_detection_engines<< [name, klass.name] | ||||
| 32 | end | ||||
| 33 | cattr_reader :spam_detection_engines | ||||
| 34 | |||||
| ca8a7879 » | technoweenie | 2008-02-03 | 35 | has_many :sections, :order => "position", :dependent => :destroy do | |
| 098f8d38 » | technoweenie | 2006-07-01 | 36 | def home | |
| b2294a8c » | technoweenie | 2006-08-30 | 37 | find_by_path '' | |
| 098f8d38 » | technoweenie | 2006-07-01 | 38 | end | |
| 1da1ea5e » | technoweenie | 2006-09-26 | 39 | ||
| 40 | # orders sections in a site | ||||
| 41 | def order!(*sorted_ids) | ||||
| 42 | transaction do | ||||
| 43 | sorted_ids.flatten.each_with_index do |section_id, pos| | ||||
| 44 | Section.update_all ['position = ?', pos], ['id = ? and site_id = ?', section_id, proxy_owner.id] | ||||
| 45 | end | ||||
| 46 | end | ||||
| 47 | end | ||||
| 098f8d38 » | technoweenie | 2006-07-01 | 48 | end | |
| 49 | |||||
| ca8a7879 » | technoweenie | 2008-02-03 | 50 | has_many :articles, :dependent => :destroy do | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 51 | def find_by_permalink(options) | |
| 52 | conditions = | ||||
| 53 | returning ["(contents.published_at IS NOT NULL AND contents.published_at <= ?)", Time.now.utc] do |cond| | ||||
| 54 | if options[:year] | ||||
| 55 | from, to = Time.delta(options[:year], options[:month], options[:day]) | ||||
| 56 | cond.first << ' AND (contents.published_at BETWEEN ? AND ?)' | ||||
| 57 | cond << from << to | ||||
| 58 | end | ||||
| 59 | |||||
| 60 | [:id, :permalink].each do |attr| | ||||
| 61 | if options[attr] | ||||
| 62 | cond.first << " AND (contents.#{attr} = ?)" | ||||
| 63 | cond << options[attr] | ||||
| 64 | end | ||||
| 65 | end | ||||
| 66 | end | ||||
| 67 | |||||
| 82cd4997 » | technoweenie | 2006-09-17 | 68 | find :first, :conditions => conditions, :order => 'published_at desc' | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 69 | end | |
| 70 | end | ||||
| 71 | |||||
| ca8a7879 » | technoweenie | 2008-02-03 | 72 | has_many :comments, :order => 'comments.created_at desc', :dependent => :delete_all | |
| 82cd4997 » | technoweenie | 2006-09-17 | 73 | ||
| ca8a7879 » | technoweenie | 2008-02-03 | 74 | has_many :events, :dependent => :destroy | |
| e30b05e4 » | technoweenie | 2006-04-03 | 75 | ||
| ca8a7879 » | technoweenie | 2008-02-03 | 76 | has_many :cached_pages, :dependent => :destroy | |
| d15983b1 » | technoweenie | 2006-09-08 | 77 | ||
| ca8a7879 » | technoweenie | 2008-02-03 | 78 | has_many :assets, :order => 'created_at desc', :conditions => 'parent_id is null', :dependent => :destroy | |
| 4a65486b » | technoweenie | 2006-08-18 | 79 | ||
| 8507eda7 » | technoweenie | 2006-09-10 | 80 | has_many :memberships, :dependent => :destroy | |
| 2bdff7b6 » | technoweenie | 2006-09-06 | 81 | has_many :members, :through => :memberships, :source => :user | |
| 82 | has_many :admins, :through => :memberships, :source => :user, :conditions => ['memberships.admin = ? or users.admin = ?', true, true] | ||||
| 83 | |||||
| e22c8b36 » | technoweenie | 2006-08-13 | 84 | before_validation :downcase_host | |
| 1cc49c4d » | technoweenie | 2006-09-10 | 85 | before_validation :set_default_attributes | |
| 86 | validates_presence_of :permalink_style, :search_path, :tag_path | ||||
| 87 | validates_format_of :search_path, :tag_path, :with => Format::STRING | ||||
| 88 | validates_format_of :host, :with => Format::DOMAIN | ||||
| e30b05e4 » | technoweenie | 2006-04-03 | 89 | validates_uniqueness_of :host | |
| 1cc49c4d » | technoweenie | 2006-09-10 | 90 | validate :check_permalink_style | |
| 43497e54 » | francois | 2008-03-03 | 91 | validate :spam_engine_is_valid? | |
| ca8a7879 » | technoweenie | 2008-02-03 | 92 | ||
| d6e29a75 » | technoweenie | 2008-02-03 | 93 | after_create :setup_site_theme_directories | |
| ca8a7879 » | technoweenie | 2008-02-03 | 94 | after_create { |site| site.sections.create(:name => 'Home') } | |
| 95 | before_destroy :flush_cache_and_remove_site_directories | ||||
| 6126dfec » | francois | 2008-03-03 | 96 | before_save :clear_approve_comment_if_spam_engine_not_null | |
| 9e355018 » | technoweenie | 2006-09-09 | 97 | ||
| a2678925 » | technoweenie | 2006-09-24 | 98 | with_options :order => 'contents.created_at DESC', :class_name => 'Comment' do |comment| | |
| 9e355018 » | technoweenie | 2006-09-09 | 99 | comment.has_many :comments, :conditions => ['contents.approved = ?', true] | |
| 100 | comment.has_many :unapproved_comments, :conditions => ['contents.approved = ? or contents.approved is null', false] | ||||
| 101 | comment.has_many :all_comments | ||||
| 102 | end | ||||
| ca8a7879 » | technoweenie | 2008-02-03 | 103 | ||
| 1ad91a78 » | francois | 2008-02-29 | 104 | serialize :spam_engine_options, Hash | |
| e545c4ae » | francois | 2008-03-03 | 105 | def spam_engine_options | |
| 106 | read_attribute(:spam_engine_options) || Hash.new | ||||
| 107 | end | ||||
| 1ad91a78 » | francois | 2008-02-29 | 108 | ||
| 4a7bb4a4 » | francois | 2008-02-29 | 109 | def spam_engine | |
| 110 | klass_name = read_attribute(:spam_detection_engine) | ||||
| ba3ae9c8 » | francois | 2008-03-03 | 111 | return Mephisto::SpamDetectionEngines::NullEngine.new(self) if klass_name.blank? | |
| 940af6ed » | francois | 2008-02-29 | 112 | klass_name.constantize.new(self) | |
| 4a7bb4a4 » | francois | 2008-02-29 | 113 | end | |
| 114 | |||||
| ca8a7879 » | technoweenie | 2008-02-03 | 115 | def self.search_by_host_or_title(search_string) | |
| 116 | conditions = search_string.blank? ? nil : ["host LIKE ? OR title LIKE ?"] + ["%#{search_string}%"] * 2 | ||||
| 117 | with_scope( :find => { :conditions => conditions } ) do | ||||
| 118 | yield | ||||
| 119 | end | ||||
| 120 | end | ||||
| 3fd67d2e » | technoweenie | 2006-02-10 | 121 | ||
| e0c49a2d » | technoweenie | 2006-09-08 | 122 | def users(options = {}) | |
| 123 | User.find_all_by_site self, options | ||||
| adf2dc7d » | technoweenie | 2006-09-06 | 124 | end | |
| 125 | |||||
| e0c49a2d » | technoweenie | 2006-09-08 | 126 | def users_with_deleted(options = {}) | |
| 127 | User.find_all_by_site_with_deleted self, options | ||||
| adf2dc7d » | technoweenie | 2006-09-06 | 128 | end | |
| 129 | |||||
| 130 | def user(id) | ||||
| 131 | User.find_by_site self, id | ||||
| 132 | end | ||||
| 133 | |||||
| 134 | def user_with_deleted(id) | ||||
| 135 | User.find_by_site_with_deleted self, id | ||||
| 136 | end | ||||
| 7dec6b4b » | technoweenie | 2006-09-17 | 137 | ||
| 138 | def user_by_token(token) | ||||
| 139 | User.find_by_token(self, token) | ||||
| 140 | end | ||||
| 9e355018 » | technoweenie | 2006-09-09 | 141 | ||
| 7dec6b4b » | technoweenie | 2006-09-17 | 142 | def user_by_email(email) | |
| 143 | User.find_by_email(self, email) | ||||
| 144 | end | ||||
| 145 | |||||
| 634babde » | technoweenie | 2006-09-10 | 146 | def tags | |
| 1e6504e8 » | technoweenie | 2006-10-23 | 147 | Tag.find(:all, :select => "DISTINCT tags.name", | |
| 148 | :joins => "INNER JOIN taggings ON taggings.tag_id = tags.id INNER JOIN contents ON (taggings.taggable_id = contents.id AND | ||||
| 149 | taggings.taggable_type = 'Content')", | ||||
| 150 | :conditions => ['contents.type = ? AND contents.site_id = ?', 'Article', id], | ||||
| 151 | :order => 'tags.name') | ||||
| 634babde » | technoweenie | 2006-09-10 | 152 | end | |
| adf2dc7d » | technoweenie | 2006-09-06 | 153 | ||
| 87dc9608 » | technoweenie | 2006-09-19 | 154 | def theme_path | |
| 155 | @theme_path ||= self.class.theme_path + "site-#{id}" | ||||
| 156 | end | ||||
| 157 | |||||
| b81b8e3b » | technoweenie | 2006-09-24 | 158 | def attachment_path | |
| 159 | theme.path | ||||
| 42b76ad0 » | technoweenie | 2006-09-18 | 160 | end | |
| 87dc9608 » | technoweenie | 2006-09-19 | 161 | ||
| 162 | def themes | ||||
| 163 | return @themes unless @themes.nil? | ||||
| dca28fda » | technoweenie | 2006-10-29 | 164 | @themes = [] | |
| 165 | FileUtils.mkdir_p theme_path | ||||
| 166 | Dir.foreach theme_path do |e| | ||||
| 87dc9608 » | technoweenie | 2006-09-19 | 167 | next if e.first == '.' | |
| dca28fda » | technoweenie | 2006-10-29 | 168 | entry = theme_path + e | |
| 87dc9608 » | technoweenie | 2006-09-19 | 169 | next unless entry.directory? | |
| dca28fda » | technoweenie | 2006-10-29 | 170 | @themes << Theme.new(entry, self) | |
| 87dc9608 » | technoweenie | 2006-09-19 | 171 | end | |
| d69321ba » | technoweenie | 2006-09-19 | 172 | def @themes.[](key) key = key.to_s ; detect { |t| t.name == key } ; end | |
| 416b5314 » | technoweenie | 2006-10-18 | 173 | @themes.sort! {|a,b| a.name <=> b.name} | |
| 87dc9608 » | technoweenie | 2006-09-19 | 174 | end | |
| 175 | |||||
| 42b76ad0 » | technoweenie | 2006-09-18 | 176 | def theme | |
| 45893b3a » | technoweenie | 2007-01-14 | 177 | @theme ||= themes[current_theme_path] || themes.first || raise(MissingThemesError.new(self)) | |
| 23e8d96e » | technoweenie | 2006-09-24 | 178 | end | |
| 179 | |||||
| 916d67d0 » | technoweenie | 2006-09-24 | 180 | def change_theme_to(new_theme_path) | |
| 181 | new_theme = (new_theme_path.is_a?(Theme) ? new_theme_path : themes[new_theme_path]) || raise("No theme '#{new_theme_path}' found") | ||||
| dca28fda » | technoweenie | 2006-10-29 | 182 | update_attribute :current_theme_path, new_theme.path.basename.to_s | |
| 183 | @theme = nil | ||||
| b81b8e3b » | technoweenie | 2006-09-24 | 184 | theme | |
| 185 | end | ||||
| 186 | |||||
| b30f9dfb » | technoweenie | 2006-09-24 | 187 | def import_theme(zip_file, name) | |
| dca28fda » | technoweenie | 2006-10-29 | 188 | imported_name = Theme.import zip_file, :to => theme_path + name | |
| 2d6074da » | technoweenie | 2006-09-24 | 189 | @theme = @themes = @rollback_theme = nil | |
| 190 | themes[imported_name] | ||||
| 191 | end | ||||
| 192 | |||||
| 193 | def move_theme(theme, new_name) | ||||
| dca28fda » | technoweenie | 2006-10-29 | 194 | FileUtils.move theme.base_path, theme_path + new_name | |
| b30f9dfb » | technoweenie | 2006-09-24 | 195 | end | |
| 196 | |||||
| 42b76ad0 » | technoweenie | 2006-09-18 | 197 | [:attachments, :templates, :resources].each { |m| delegate m, :to => :theme } | |
| 198 | |||||
| cf4c79b8 » | technoweenie | 2006-09-10 | 199 | def permalink_for(article) | |
| 1601e475 » | technoweenie | 2006-09-19 | 200 | Mephisto::Dispatcher.build_permalink_with(permalink_style, article) | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 201 | end | |
| 202 | |||||
| ccd2d2de » | technoweenie | 2006-09-10 | 203 | def search_url(query, page = nil) | |
| 632f7735 » | technoweenie | 2007-11-23 | 204 | "/#{search_path}?q=#{CGI::escapeHTML(query)}#{%(&page=#{CGI::escapeHTML(page.to_s)}) unless page.blank?}" | |
| ccd2d2de » | technoweenie | 2006-09-10 | 205 | end | |
| 206 | |||||
| b79ae4cb » | technoweenie | 2006-09-10 | 207 | def tag_url(*tags) | |
| 632f7735 » | technoweenie | 2007-11-23 | 208 | ['', tag_path, *tags.collect { |t| URI::escape(t.to_s) }] * '/' | |
| b79ae4cb » | technoweenie | 2006-09-10 | 209 | end | |
| 210 | |||||
| 9a1562da » | technoweenie | 2006-08-07 | 211 | def accept_comments? | |
| 212 | comment_age.to_i > -1 | ||||
| 213 | end | ||||
| 214 | |||||
| a5750df3 » | technoweenie | 2007-02-12 | 215 | def call_render(section, template_type, assigns = {}, controller = nil) | |
| 6cd908d3 » | technoweenie | 2006-10-18 | 216 | assigns.update('site' => to_liquid(section), 'mode' => template_type) | |
| 1b252a48 » | technoweenie | 2006-12-03 | 217 | assigns.update(default_assigns) unless default_assigns.empty? | |
| a5750df3 » | technoweenie | 2007-02-12 | 218 | template = set_content_template(section, template_type) | |
| 219 | layout = set_layout_template(section, template_type) | ||||
| 220 | handler = @@template_handlers[theme.extension] || @@template_handlers[".liquid"] | ||||
| 221 | handler.new(self).render(section, layout, template, assigns, controller) | ||||
| 93bca3fe » | technoweenie | 2006-08-21 | 222 | end | |
| a5750df3 » | technoweenie | 2007-02-12 | 223 | ||
| 7bd65e0a » | technoweenie | 2006-08-12 | 224 | def to_liquid(current_section = nil) | |
| f868a1b6 » | technoweenie | 2006-09-17 | 225 | SiteDrop.new self, current_section | |
| 66658ac8 » | technoweenie | 2006-02-27 | 226 | end | |
| f9777f35 » | technoweenie | 2006-06-29 | 227 | ||
| 55c4ed28 » | technoweenie | 2006-07-03 | 228 | composed_of :timezone, :class_name => 'TZInfo::Timezone', :mapping => %w(timezone name) | |
| 229 | alias original_timezone_writer timezone= | ||||
| 230 | def timezone=(name) | ||||
| 231 | name = TZInfo::Timezone.new(name) unless name.is_a?(TZInfo::Timezone) | ||||
| 232 | original_timezone_writer(name) | ||||
| 233 | end | ||||
| 234 | |||||
| 4231359d » | technoweenie | 2006-09-25 | 235 | def page_cache_directory | |
| 236 | multi_sites_enabled ? | ||||
| 237 | (RAILS_PATH + (RAILS_ENV == 'test' ? 'tmp' : 'public') + 'cache' + host) : | ||||
| 238 | (RAILS_PATH + (RAILS_ENV == 'test' ? 'tmp/cache' : 'public')) | ||||
| 239 | end | ||||
| 240 | |||||
| 241 | def expire_cached_pages(controller, log_message, pages = nil) | ||||
| 0fd6e749 » | technoweenie | 2006-09-26 | 242 | controller = controller.class unless controller.is_a?(Class) | |
| 4231359d » | technoweenie | 2006-09-25 | 243 | pages ||= cached_pages.find_current(:all) | |
| 244 | returning cached_log_message_for(log_message, pages) do |msg| | ||||
| 245 | controller.logger.warn msg if cache_sweeper_tracing | ||||
| 0fd6e749 » | technoweenie | 2006-09-26 | 246 | pages.each { |p| controller.expire_page(p.url) } | |
| 4231359d » | technoweenie | 2006-09-25 | 247 | CachedPage.expire_pages(self, pages) | |
| 248 | end | ||||
| 249 | end | ||||
| 250 | |||||
| a5750df3 » | technoweenie | 2007-02-12 | 251 | #need non protected method for ErbTemplate - psq | |
| 252 | def find_preferred_template(template_type, custom_template) | ||||
| 253 | preferred = templates.find_preferred(template_type, custom_template) | ||||
| 254 | return preferred if preferred && preferred.file? | ||||
| 255 | raise MissingTemplateError.new(template_type, templates.collect_templates(template_type, custom_template).collect(&:basename)) | ||||
| 256 | end | ||||
| 257 | |||||
| f9777f35 » | technoweenie | 2006-06-29 | 258 | protected | |
| 6126dfec » | francois | 2008-03-03 | 259 | # If we aren't using the null engine, comments must not be approved automatically. | |
| 260 | def clear_approve_comment_if_spam_engine_not_null | ||||
| 6088ac30 » | francois | 2008-03-03 | 261 | returning true do | |
| 262 | if self.spam_engine.null? | ||||
| 263 | # NOP, leave as-is | ||||
| 264 | else | ||||
| 265 | self.approve_comments = false | ||||
| 266 | end | ||||
| 6126dfec » | francois | 2008-03-03 | 267 | end | |
| 268 | end | ||||
| 269 | |||||
| 43497e54 » | francois | 2008-03-03 | 270 | # A validation filter. | |
| 271 | def spam_engine_is_valid? | ||||
| 1f27e358 » | francois | 2008-03-03 | 272 | logger.debug {"==> self.spam_engine.valid_key? #{self.spam_engine.valid_key?}"} | |
| 43497e54 » | francois | 2008-03-03 | 273 | return if self.spam_engine.valid_key? | |
| 274 | if errors = self.spam_engine.errors then | ||||
| 275 | errors.each do |error| | ||||
| 276 | self.errors.add_to_base(error) | ||||
| 277 | end | ||||
| 1f27e358 » | francois | 2008-03-03 | 278 | else | |
| 279 | logger.debug {"==> No errors were logged, add something minimal"} | ||||
| 280 | self.errors.add_to_base("Failed to validate the spam engine's key") | ||||
| 43497e54 » | francois | 2008-03-03 | 281 | end | |
| 282 | end | ||||
| 283 | |||||
| 4231359d » | technoweenie | 2006-09-25 | 284 | def cached_log_message_for(log_message, pages) | |
| 285 | pages.inject([log_message, "Expiring #{pages.size} page(s)"]) { |msg, p| msg << " - #{p.url}" }.join("\n") | ||||
| 286 | end | ||||
| 287 | |||||
| 1601e475 » | technoweenie | 2006-09-19 | 288 | def permalink_variable_format?(var) | |
| 289 | Mephisto::Dispatcher.variable_format?(var) | ||||
| 290 | end | ||||
| 291 | |||||
| cf4c79b8 » | technoweenie | 2006-09-10 | 292 | def permalink_variable?(var) | |
| 1601e475 » | technoweenie | 2006-09-19 | 293 | Mephisto::Dispatcher.variable?(var) | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 294 | end | |
| 295 | |||||
| 1cc49c4d » | technoweenie | 2006-09-10 | 296 | def check_permalink_style | |
| 297 | permalink_style.sub! /^\//, '' | ||||
| 298 | permalink_style.sub! /\/$/, '' | ||||
| 911fe1c8 » | technoweenie | 2007-01-16 | 299 | pieces = permalink_style.split('/') | |
| 1cc49c4d » | technoweenie | 2006-09-10 | 300 | errors.add :permalink_style, 'cannot have blank paths' if pieces.any?(&:blank?) | |
| 9e355018 » | technoweenie | 2006-09-09 | 301 | pieces.each do |p| | |
| 1601e475 » | technoweenie | 2006-09-19 | 302 | errors.add :permalink_style, "cannot contain '#{p}' variable" unless p.blank? || permalink_variable_format?(p).nil? || permalink_variable?(p) | |
| 9e355018 » | technoweenie | 2006-09-09 | 303 | end | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 304 | unless pieces.include?(':id') || pieces.include?(':permalink') | |
| 1cc49c4d » | technoweenie | 2006-09-10 | 305 | errors.add :permalink_style, "must contain either :permalink or :id" | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 306 | end | |
| 307 | if !pieces.include?(':year') && (pieces.include?(':month') || pieces.include?(':day')) | ||||
| 1cc49c4d » | technoweenie | 2006-09-10 | 308 | errors.add :permalink_style, "must contain :year for any date-based permalinks" | |
| cf4c79b8 » | technoweenie | 2006-09-10 | 309 | end | |
| 9e355018 » | technoweenie | 2006-09-09 | 310 | end | |
| 311 | |||||
| e22c8b36 » | technoweenie | 2006-08-13 | 312 | def downcase_host | |
| 313 | self.host = host.to_s.downcase | ||||
| 314 | end | ||||
| 315 | |||||
| 1cc49c4d » | technoweenie | 2006-09-10 | 316 | def set_default_attributes | |
| 317 | self.permalink_style = ':year/:month/:day/:permalink' if permalink_style.blank? | ||||
| 318 | self.search_path = 'search' if search_path.blank? | ||||
| 319 | self.tag_path = 'tags' if tag_path.blank? | ||||
| 320 | [:permalink_style, :search_path, :tag_path].each { |a| send(a).downcase! } | ||||
| 0cf9817f » | technoweenie | 2006-07-08 | 321 | self.timezone = 'UTC' if read_attribute(:timezone).blank? | |
| 1cc49c4d » | technoweenie | 2006-09-10 | 322 | if new_record? | |
| 323 | self.approve_comments = false unless approve_comments? | ||||
| 324 | self.comment_age = 30 unless comment_age | ||||
| 325 | end | ||||
| f9777f35 » | technoweenie | 2006-06-29 | 326 | true | |
| 327 | end | ||||
| 5c3ee7ea » | technoweenie | 2006-08-24 | 328 | ||
| af01235b » | technoweenie | 2006-09-20 | 329 | def set_content_template(section, template_type) | |
| 4ae91c21 » | technoweenie | 2006-10-18 | 330 | preferred_template = | |
| 331 | case template_type | ||||
| 332 | when :page, :section | ||||
| 333 | template_type = :single if template_type == :page | ||||
| 334 | section.template | ||||
| 335 | when :archive | ||||
| 336 | section.archive_template | ||||
| 337 | end | ||||
| af01235b » | technoweenie | 2006-09-20 | 338 | find_preferred_template(template_type, preferred_template) | |
| 5c3ee7ea » | technoweenie | 2006-08-24 | 339 | end | |
| 340 | |||||
| 341 | def set_layout_template(section, template_type) | ||||
| d46eda10 » | technoweenie | 2006-09-10 | 342 | layout_template = | |
| 343 | if section | ||||
| 344 | section.layout | ||||
| 345 | else | ||||
| 346 | case template_type | ||||
| 347 | when :tag then tag_layout | ||||
| 40732b60 » | technoweenie | 2007-01-14 | 348 | when :search then sections.detect(&:home?).layout | |
| d46eda10 » | technoweenie | 2006-09-10 | 349 | end | |
| 350 | end | ||||
| af01235b » | technoweenie | 2006-09-20 | 351 | find_preferred_template(:layout, layout_template) | |
| 352 | end | ||||
| 353 | |||||
| ca8a7879 » | technoweenie | 2008-02-03 | 354 | private | |
| 355 | |||||
| 356 | def setup_site_theme_directories | ||||
| 357 | begin | ||||
| 358 | theme_path = "#{RAILS_ROOT}/themes/site-#{self.id}/simpla" | ||||
| 359 | FileUtils.mkdir_p("#{RAILS_ROOT}/themes/site-#{self.id}") | ||||
| 360 | FileUtils.cp_r("#{RAILS_ROOT}/themes/default", theme_path) | ||||
| 361 | Dir[File.join(theme_path, '**/.svn')].each do |dir| | ||||
| 362 | FileUtils.rm_rf dir | ||||
| 363 | end | ||||
| 364 | rescue | ||||
| 365 | logger.error "ERROR: removing directories for site #{self.host}, check file permissions." | ||||
| 366 | errors.add_to_base "Unable to create theme directories." | ||||
| 367 | false | ||||
| 368 | end | ||||
| 369 | end | ||||
| 370 | |||||
| 371 | def flush_cache_and_remove_site_directories | ||||
| 372 | begin | ||||
| 373 | CachedPage.expire_pages self, self.cached_pages | ||||
| 374 | FileUtils.rm_rf("#{RAILS_ROOT}/themes/site-#{self.id}") | ||||
| 375 | FileUtils.rm_rf("#{RAILS_ROOT}/public/cache/#{self.host}") | ||||
| 376 | rescue | ||||
| 377 | logger.error "ERROR: removing directories for site #{self.host}, check file permissions." | ||||
| 378 | false | ||||
| 379 | end | ||||
| 380 | end | ||||
| 381 | |||||
| 3fd67d2e » | technoweenie | 2006-02-10 | 382 | end | |







