From b60be8889fcd276b01cce89924ea4175b437ebcd Mon Sep 17 00:00:00 2001 From: Prace Fen Date: Wed, 6 Feb 2013 22:48:55 +0800 Subject: [PATCH 01/11] Remove some gems because they are never used in this project. --- Gemfile | 9 ++------- app/views/facebook/complete.html.erb | 9 +++++++++ app/views/twitter/complete.html.erb | 9 +++++++++ app/views/user_open_ids/complete.html.erb | 9 +++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 app/views/facebook/complete.html.erb create mode 100644 app/views/twitter/complete.html.erb create mode 100644 app/views/user_open_ids/complete.html.erb diff --git a/Gemfile b/Gemfile index fe84fb1f10c..0a0adf65d09 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ gem 'hiredis' gem 'em-redis' gem 'rails' gem 'pg' -gem 'haml' gem 'sass' gem 'rake' # errbit is broken with 3.1.3 for now @@ -20,8 +19,6 @@ gem 'nokogiri' gem 'seed-fu' gem 'sanitize' - -gem 'slim', '<= 1.3.0' gem 'sinatra', :require => nil gem 'clockwork', :require => false @@ -70,11 +67,10 @@ group :assets do gem 'uglifier' # gem "asset_sync" gem 'turbo-sprockets-rails3' + # need this to compile coffee on the fly + gem 'coffee-script' end -# need this to compile coffee on the fly -gem 'coffee-script' - gem 'hpricot' gem 'jquery-rails' @@ -97,7 +93,6 @@ group :test, :development do gem 'guard-rspec' gem 'guard-spork' gem 'mocha', :require => false - gem 'test-unit', :require => "test/unit" gem 'simplecov', :require => false gem 'image_optim' gem 'certified' diff --git a/app/views/facebook/complete.html.erb b/app/views/facebook/complete.html.erb new file mode 100644 index 00000000000..8c0c190a038 --- /dev/null +++ b/app/views/facebook/complete.html.erb @@ -0,0 +1,9 @@ + + + + + + diff --git a/app/views/twitter/complete.html.erb b/app/views/twitter/complete.html.erb new file mode 100644 index 00000000000..8c0c190a038 --- /dev/null +++ b/app/views/twitter/complete.html.erb @@ -0,0 +1,9 @@ + + + + + + diff --git a/app/views/user_open_ids/complete.html.erb b/app/views/user_open_ids/complete.html.erb new file mode 100644 index 00000000000..5b58e63c97d --- /dev/null +++ b/app/views/user_open_ids/complete.html.erb @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file From ee8d68842ebf834f8e5d5e2deb6e42f6fa050d72 Mon Sep 17 00:00:00 2001 From: Dan Neumann Date: Wed, 6 Feb 2013 19:52:14 -0600 Subject: [PATCH 02/11] Strip leading and trailing slug punctuation. --- lib/slug.rb | 1 + spec/components/slug_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/lib/slug.rb b/lib/slug.rb index 9678130ed24..c4fe8fdfb0f 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -23,6 +23,7 @@ module Slug str.gsub!(/[^a-z0-9 -]/, '') str.gsub!(/\s+/, '-') str.gsub!(/\-+/, '-') + str.gsub!(/^-|-$/, '') str end diff --git a/spec/components/slug_spec.rb b/spec/components/slug_spec.rb index 6111b73710e..fde5ebd8e33 100644 --- a/spec/components/slug_spec.rb +++ b/spec/components/slug_spec.rb @@ -27,6 +27,13 @@ describe Slug do Slug.for("a....b.....c").should == "a-b-c" end + it 'strips trailing punctuation' do + Slug.for("hello...").should == "hello" + end + + it 'strips leading punctuation' do + Slug.for("...hello").should == "hello" + end end From 98bf443d82de365e9b3b7ef7db934085a4f55e55 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Thu, 7 Feb 2013 00:41:27 -0500 Subject: [PATCH 03/11] Fix guard warning with version spec for rb-notify. --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index fe84fb1f10c..e438c2405fc 100644 --- a/Gemfile +++ b/Gemfile @@ -102,7 +102,7 @@ group :test, :development do gem 'image_optim' gem 'certified' gem 'rb-fsevent' - gem 'rb-inotify', :require => RUBY_PLATFORM.include?('linux') && 'rb-inotify' + gem 'rb-inotify', '~> 0.8.8', :require => RUBY_PLATFORM.include?('linux') && 'rb-inotify' gem 'terminal-notifier-guard', :require => RUBY_PLATFORM.include?('darwin') && 'terminal-notifier-guard' end diff --git a/Gemfile.lock b/Gemfile.lock index 15a5bbebb93..85225874b69 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -256,7 +256,7 @@ GEM thor (>= 0.14.6, < 2.0) rake (10.0.3) rb-fsevent (0.9.3) - rb-inotify (0.9.0) + rb-inotify (0.8.8) ffi (>= 0.5.0) rdoc (3.12) json (~> 1.4) @@ -416,7 +416,7 @@ DEPENDENCIES rails_multisite! rake rb-fsevent - rb-inotify + rb-inotify (~> 0.8.8) redis redis-rails rest-client From bfa7a5fd678cbc7382a4b034f291dbfcc8642869 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Thu, 7 Feb 2013 00:42:47 -0500 Subject: [PATCH 04/11] Increase timeout for spork startup. --- Guardfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index 0fcebb98d8a..63fc2c0c5a4 100644 --- a/Guardfile +++ b/Guardfile @@ -1,4 +1,4 @@ -guard 'spork' do +guard :spork, wait: 120 do watch('config/application.rb') watch('config/environment.rb') watch(%r{^config/environments/.*\.rb$}) From 43d4eb681e0c073160e1145034226066083ac8b6 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Thu, 7 Feb 2013 01:10:46 -0500 Subject: [PATCH 05/11] Fix Guardfile warning about deprecated :version arg. --- Guardfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index 63fc2c0c5a4..6dca485a9d6 100644 --- a/Guardfile +++ b/Guardfile @@ -27,7 +27,7 @@ guard 'jasmine', jasmine_options do watch(%r{spec/javascripts/spec\.(js\.coffee| watch(%r{app/assets/javascripts/(.+?)\.(js\.coffee|js|coffee)$}) { "spec/javascripts" } end -guard 'rspec', :focus_on_failed => true, :version => 2, :cli => "--drb" do +guard 'rspec', :focus_on_failed => true, :cli => "--drb" do watch(%r{^spec/.+_spec\.rb$}) #watch(%r{^lib/jobs/(.+)\.rb$}) { |m| "spec/components/jobs/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "spec/components/#{m[1]}_spec.rb" } From f54e530a3ebb5ffa60c0fb620e35b206fb7d99ea Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Thu, 7 Feb 2013 01:13:24 -0500 Subject: [PATCH 06/11] Tuneups of Vagrant Increase memory allocation to 512MB so guest VM is not swapping. SSH forwarding, DNS host nat resolver. Don't use NFS on darwin, just use standard folder sharing. --- Vagrantfile | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index f974697d1be..c723a86a320 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,14 +1,29 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : # See https://github.com/discourse/core/blob/master/DEVELOPMENT.md # Vagrant::Config.run do |config| config.vm.box = 'discourse-pre' config.vm.box_url = 'http://www.discourse.org/vms/discourse-pre.box' + + # Make this VM reachable on the host network as well, so that other + # VM's running other browsers can access our dev server. config.vm.network :hostonly, '192.168.10.200' + # Make it so that network access from the vagrant guest is able to + # use SSH private keys that are present on the host without copying + # them into the VM. + config.ssh.forward_agent = true + + # This setting gives the VM 512MB of MEMORIES instead of the default 384. + config.vm.customize ["modifyvm", :id, "--memory", 512] + + # This setting makes it so that network access from inside the vagrant guest + # is able to resolve DNS using the hosts VPN connection. + config.vm.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] + config.vm.forward_port 3000, 4000 config.vm.forward_port 1080, 4080 # Mailcatcher - if RUBY_PLATFORM =~ /darwin/ - config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true) - end + config.vm.share_folder("v-root", "/vagrant", ".") end From 3b49f96e9abc5d70dd721a5c84e7d55baf80e478 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Thu, 7 Feb 2013 01:18:45 -0500 Subject: [PATCH 07/11] Update & simplify vagrant instructions. The gem installer is deprecated, direct people to the real installer. No longer need to require XCode and RVM installs, just install vagrant and go. Password prompt during boot only happens on windows, not OS X and Linux hosts. --- DEVELOPMENT.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index aff60de62c4..95c7d384c86 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -7,13 +7,11 @@ on Discourse with: ### Getting Started -1. Install the Xcode tools: https://developer.apple.com/xcode/ -2. Install VirtualBox: https://www.virtualbox.org/wiki/Downloads -3. Install Ruby 1.9.3. We recommend RVM: https://rvm.io/ -4. Open a terminal -5. Clone the project: `git@github.com:discourse/discourse.git` -6. Enter the project directory: `cd discourse` -7. Install vagrant: `gem install vagrant` +1. Install VirtualBox: https://www.virtualbox.org/wiki/Downloads +2. Install Vagrant: https://www.vagrantup.com/ +3. Open a terminal +4. Clone the project: `git@github.com:discourse/discourse.git` +5. Enter the project directory: `cd discourse` ### Using Vagrant @@ -22,7 +20,7 @@ When you're ready to start working, boot the VM: vagrant up ``` -It should prompt you for your admin password. This is so it can mount your local files inside the VM for an easy workflow. +On Windows, it will prompt you for your admin password. This is so it can mount your local files inside the VM for an easy workflow. (The first time you do this, it will take a while as it downloads the VM image and installs it. Go grab a coffee.) From 85973ce6b0a68fc1726cc5485affbf099147a2b9 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 7 Feb 2013 18:11:56 +1100 Subject: [PATCH 08/11] added delete all posts button wired up the ability to enable all themes --- .../admin/models/admin_user.js.coffee | 10 ++++--- .../admin/templates/flags.js.handlebars | 2 +- .../admin/templates/user.js.handlebars | 8 ++++++ app/controllers/admin/users_controller.rb | 5 ++++ app/models/post.rb | 4 +-- app/models/site_customization.rb | 20 ++++++++++++++ app/models/user.rb | 12 +++++++++ .../admin_detailed_user_serializer.rb | 7 ++++- app/views/layouts/application.html.erb | 6 +---- config/locales/en.yml | 1 + config/routes.rb | 1 + lib/guardian.rb | 7 +++++ spec/models/user_spec.rb | 27 ++++++++++++++++--- 13 files changed, 93 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/admin/models/admin_user.js.coffee b/app/assets/javascripts/admin/models/admin_user.js.coffee index 148149b767f..a4fb524e91d 100644 --- a/app/assets/javascripts/admin/models/admin_user.js.coffee +++ b/app/assets/javascripts/admin/models/admin_user.js.coffee @@ -1,10 +1,14 @@ window.Discourse.AdminUser = Discourse.Model.extend + deleteAllPosts: -> + @set('can_delete_all_posts', false) + $.ajax "/admin/users/#{@get('id')}/delete_all_posts", type: 'PUT' + # Revoke the user's admin access revokeAdmin: -> @set('admin',false) @set('can_grant_admin',true) - @set('can_revoke_admin',false) + @set('can_revoke_admin',false) $.ajax "/admin/users/#{@get('id')}/revoke_admin", type: 'PUT' grantAdmin: -> @@ -18,13 +22,11 @@ window.Discourse.AdminUser = Discourse.Model.extend type: 'POST' bootbox.alert("Message sent to all clients!") - - approve: -> @set('can_approve', false) @set('approved', true) @set('approved_by', Discourse.get('currentUser')) - $.ajax "/admin/users/#{@get('id')}/approve", type: 'PUT' + $.ajax "/admin/users/#{@get('id')}/approve", type: 'PUT' username_lower:(-> @get('username').toLowerCase() diff --git a/app/assets/javascripts/admin/templates/flags.js.handlebars b/app/assets/javascripts/admin/templates/flags.js.handlebars index 929076b5cec..17670abeee9 100644 --- a/app/assets/javascripts/admin/templates/flags.js.handlebars +++ b/app/assets/javascripts/admin/templates/flags.js.handlebars @@ -38,7 +38,7 @@ -
{{avatar user imageSize="small"}} {{message}}
+ diff --git a/app/assets/javascripts/admin/templates/user.js.handlebars b/app/assets/javascripts/admin/templates/user.js.handlebars index c109eb7b853..1d29846be58 100644 --- a/app/assets/javascripts/admin/templates/user.js.handlebars +++ b/app/assets/javascripts/admin/templates/user.js.handlebars @@ -139,6 +139,14 @@
{{i18n admin.user.post_count}}
{{content.post_count}}
+
+ {{#if content.can_delete_all_posts}} + + {{/if}} +
{{i18n admin.user.posts_read_count}}
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index f9c842b199d..6186d40b220 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -19,6 +19,11 @@ class Admin::UsersController < Admin::AdminController render_serialized(@user, AdminDetailedUserSerializer, root: false) end + def delete_all_posts + @user = User.where(id: params[:user_id]).first + @user.delete_all_posts!(guardian) + render nothing: true + end def ban @user = User.where(id: params[:user_id]).first guardian.ensure_can_ban!(@user) diff --git a/app/models/post.rb b/app/models/post.rb index 42d7852019d..550de6fe2b1 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -319,11 +319,11 @@ class Post < ActiveRecord::Base Post.transaction do self.last_version_at = revised_at updater.call(true) - EditRateLimiter.new(updated_by).performed! + EditRateLimiter.new(updated_by).performed! unless opts[:bypass_rate_limiter] # If a new version is created of the last post, bump it. unless Post.where('post_number > ? and topic_id = ?', self.post_number, self.topic_id).exists? - topic.update_column(:bumped_at, Time.now) + topic.update_column(:bumped_at, Time.now) unless opts[:bypass_bump] end end diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index af2dca2f063..625b907ce21 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -1,5 +1,6 @@ class SiteCustomization < ActiveRecord::Base + ENABLED_KEY = '7e202ef2-56d7-47d5-98d8-a9c8d15e57dd' CACHE_PATH = 'stylesheet-cache' @lock = Mutex.new @@ -49,18 +50,36 @@ footer:after{ content: '#{error}' }" self.remove_from_cache! end + def self.enabled_style + @cache ||= {} + preview_style = @cache[ENABLED_KEY] + return nil if preview_style == :none + return preview_style if preview_style + + @lock.synchronize do + style = self.where(enabled: true).first + if style + @cache[ENABLED_KEY] = style.key + else + @cache[ENABLED_KEY] = :none + end + end + end def self.custom_stylesheet(preview_style) + preview_style ||= enabled_style style = lookup_style(preview_style) style.stylesheet_link_tag.html_safe if style end def self.custom_header(preview_style) + preview_style ||= enabled_style style = lookup_style(preview_style) style.header.html_safe if style end def self.override_default_style(preview_style) + preview_style ||= enabled_style style = lookup_style(preview_style) style.override_default_style if style end @@ -103,6 +122,7 @@ footer:after{ content: '#{error}' }" end def remove_from_cache! + self.class.remove_from_cache!(ENABLED_KEY) self.class.remove_from_cache!(self.key) end diff --git a/app/models/user.rb b/app/models/user.rb index 91ce8efde0f..3ed14f3b015 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -335,6 +335,18 @@ class User < ActiveRecord::Base PrettyText.excerpt(bio_cooked, 350) end + def delete_all_posts!(guardian) + raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self + + posts.order("post_number desc").each do |p| + if p.post_number == 1 + p.topic.destroy + else + p.destroy + end + end + end + def is_banned? !banned_till.nil? && banned_till > DateTime.now end diff --git a/app/serializers/admin_detailed_user_serializer.rb b/app/serializers/admin_detailed_user_serializer.rb index c23bc79ff6a..d1254f553ed 100644 --- a/app/serializers/admin_detailed_user_serializer.rb +++ b/app/serializers/admin_detailed_user_serializer.rb @@ -8,7 +8,8 @@ class AdminDetailedUserSerializer < AdminUserSerializer :post_count, :flags_given_count, :flags_received_count, - :private_topics_count + :private_topics_count, + :can_delete_all_posts has_one :approved_by, serializer: BasicUserSerializer, embed: :objects @@ -19,6 +20,10 @@ class AdminDetailedUserSerializer < AdminUserSerializer def can_grant_admin scope.can_grant_admin?(object) end + + def can_delete_all_posts + scope.can_delete_all_posts?(object) + end def moderator object.has_trust_level?(:moderator) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 94ec95c7e43..106abd5f98d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -50,11 +50,7 @@ <%- unless SiteCustomization.override_default_style(session[:preview_style]) %> - <%- if params[:shiny] %> - <%=stylesheet_link_tag "shiny/shiny"%> - <%- else %> - <%=stylesheet_link_tag "application"%> - <%- end %> + <%=stylesheet_link_tag "application"%> <%- end %> <%- if mini_profiler_enabled? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 27c3ff8e3b9..db014fc301a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -369,6 +369,7 @@ en: ban_failed: "Something went wrong banning this user {{error}}" unban_failed: "Something went wrong unbanning this user {{error}}" ban_duration: "How long would you like to ban the user for? (days)" + delete_all_posts: "Delete all posts" ban: "Ban" unban: "Unban" banned: "Banned?" diff --git a/config/routes.rb b/config/routes.rb index b0a8db09293..9bcd44afd2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,7 @@ Discourse::Application.routes.draw do put 'approve-bulk' => 'users#approve_bulk' end put 'ban' => 'users#ban' + put 'delete_all_posts' => 'users#delete_all_posts' put 'unban' => 'users#unban' put 'revoke_admin' => 'users#revoke_admin' put 'grant_admin' => 'users#grant_admin' diff --git a/lib/guardian.rb b/lib/guardian.rb index 72f475a4ee2..48b4d412871 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -167,6 +167,13 @@ class Guardian @user.id == user_id end + def can_delete_all_posts?(user) + return false unless is_admin? + return false if user.created_at < 7.days.ago + + true + end + # Support for ensure_{blah}! methods. def method_missing(method, *args, &block) if method.to_s =~ /^ensure_(.*)\!$/ diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e758f7311b7..28b25af358b 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -67,12 +67,8 @@ describe User do user.reload user.posts_read_count.should == 1 end - end - end - - end context '.enqueue_welcome_message' do @@ -174,6 +170,29 @@ describe User do end + describe 'delete posts' do + before do + @post1 = Fabricate(:post) + @user = @post1.user + @post2 = Fabricate(:post, topic: @post1.topic, user: @user) + @post3 = Fabricate(:post, user: @user) + @posts = [@post1, @post2, @post3] + @guardian = Guardian.new(Fabricate(:admin)) + end + + it 'allows moderator to delete all posts' do + @user.delete_all_posts!(@guardian) + @posts.each do |p| + p.reload + if p + p.topic.should be_nil + else + p.should be_nil + end + end + end + end + describe 'new' do From 168bb01439120610fa8bcf1960910e28e2b7fbfc Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 7 Feb 2013 18:25:18 +1100 Subject: [PATCH 09/11] allow for empty headers properly --- app/models/site_customization.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index 625b907ce21..3b455785c72 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -75,7 +75,11 @@ footer:after{ content: '#{error}' }" def self.custom_header(preview_style) preview_style ||= enabled_style style = lookup_style(preview_style) - style.header.html_safe if style + if style && style.header + style.header.html_safe + else + "" + end end def self.override_default_style(preview_style) From efa4495fc2a0011cd1c8f49fb42b0dd77f4ff5b4 Mon Sep 17 00:00:00 2001 From: Alessandro Vermeulen Date: Thu, 7 Feb 2013 13:44:10 +0100 Subject: [PATCH 10/11] Added bundle install command to easy setup When I tried to follow the steps mentioned I needed to perform `bundle install` as well. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 77a16ffff46..a25f8a0b0d6 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ If you're interested in helping us develop Discourse, please start with our **[D ``` git clone git@github.com:discourse/discourse.git cd discourse +bundle install rake db:create rake db:migrate rake db:seed_fu From 4fcd924797d6afdbf9362f387edf6f72ed72402e Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 8 Feb 2013 00:55:04 +1100 Subject: [PATCH 11/11] I committed a regression around multi site today and site customization, added tests so it don't happen again --- app/models/site_customization.rb | 22 ++++++++++++------- spec/models/site_customization_spec.rb | 29 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index 3b455785c72..4dd77a299b8 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -50,30 +50,36 @@ footer:after{ content: '#{error}' }" self.remove_from_cache! end - def self.enabled_style + def self.enabled_key + ENABLED_KEY.dup << RailsMultisite::ConnectionManagement.current_db + end + + def self.enabled_style_key @cache ||= {} - preview_style = @cache[ENABLED_KEY] + preview_style = @cache[self.enabled_key] return nil if preview_style == :none return preview_style if preview_style @lock.synchronize do style = self.where(enabled: true).first if style - @cache[ENABLED_KEY] = style.key + @cache[self.enabled_key] = style.key + return style.key else - @cache[ENABLED_KEY] = :none + @cache[self.enabled_key] = :none + return nil end end end def self.custom_stylesheet(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) style.stylesheet_link_tag.html_safe if style end def self.custom_header(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) if style && style.header style.header.html_safe @@ -83,7 +89,7 @@ footer:after{ content: '#{error}' }" end def self.override_default_style(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) style.override_default_style if style end @@ -126,7 +132,7 @@ footer:after{ content: '#{error}' }" end def remove_from_cache! - self.class.remove_from_cache!(ENABLED_KEY) + self.class.remove_from_cache!(self.class.enabled_key) self.class.remove_from_cache!(self.key) end diff --git a/spec/models/site_customization_spec.rb b/spec/models/site_customization_spec.rb index 7be20ae75c3..68a554cf9f2 100644 --- a/spec/models/site_customization_spec.rb +++ b/spec/models/site_customization_spec.rb @@ -16,6 +16,35 @@ describe SiteCustomization do end context 'caching' do + + context 'enabled style' do + before do + @customization = customization + end + + it 'finds no style when none enabled' do + SiteCustomization.enabled_style_key.should be_nil + end + + it 'finds the enabled style' do + @customization.enabled = true + @customization.save + SiteCustomization.enabled_style_key.should == @customization.key + end + + it 'finds no enabled style on other sites' do + @customization.enabled = true + @customization.save + + RailsMultisite::ConnectionManagement.expects(:current_db).returns("foo").twice + # the mocking is tricky, lets remove the record so we can properly pretend we are on another db + # this bypasses the before / after stuff + SiteCustomization.exec_sql('delete from site_customizations') + + SiteCustomization.enabled_style_key.should be_nil + end + end + it 'should allow me to lookup a filename containing my preview stylesheet' do SiteCustomization.custom_stylesheet(customization.key).should == ""