don't blow away the brandConfig someone else is working on

closes: CNVS-21804

this will also do a better job of cleaning up
the app/stylesheets/brandable_css_brands dir
(so when you compile sass, it won't do stuff
for brandConfigs that aren't being used anymore)

test plan:
open theme editor, make some changes, hit preview.
as a different user in a different browser,
open theme editor, make changes, press save.
now in the first window, try to hit save.
before it would break, now it will work

Change-Id: I094f737d35c854764a7c361bec4798b8a2203410
Reviewed-on: https://gerrit.instructure.com/58102
Reviewed-by: Mike Nomitch <mnomitch@instructure.com>
Product-Review: Jennifer Stern <jstern@instructure.com>
Tested-by: Jenkins
QA-Review: Nathan Rogowski <nathan@instructure.com>
This commit is contained in:
Ryan Shaw 2015-07-09 15:02:27 -06:00 committed by Matt Fairbourn
parent d422ee4426
commit 87eba38b3e
4 changed files with 41 additions and 17 deletions

View File

@ -14,6 +14,7 @@ class BrandConfigsController < ApplicationController
end
def create
old_md5 = session[:brand_config_md5]
session[:brand_config_md5] = begin
if params[:brand_config] == ''
false
@ -23,19 +24,22 @@ class BrandConfigsController < ApplicationController
create_brand_config(variables).md5
end
end
BrandConfig.destroy_if_unused(old_md5)
redirect_to brand_configs_new_path
end
def save_to_account
@domain_root_account.update_attributes!(brand_config_md5: session[:brand_config_md5].presence)
BrandConfig.clean_up_unused
session.delete(:brand_config_md5)
old_md5 = @domain_root_account.brand_config_md5
new_md5 = session.delete(:brand_config_md5).presence
@domain_root_account.brand_config = new_md5 && BrandConfig.find(new_md5)
@domain_root_account.save!
BrandConfig.destroy_if_unused(old_md5)
redirect_to :back, notice: t('Success! All users on this domain will now see this branding.')
end
def destroy
session.delete(:brand_config_md5)
BrandConfig.clean_up_unused
BrandConfig.destroy_if_unused(session.delete(:brand_config_md5))
flash[:notice] = t('Theme editor changes have been cancelled.')
render json: {success: true}
end

View File

@ -35,18 +35,28 @@ class BrandConfig < ActiveRecord::Base
end.compact.join("\n")
end
def filename
File.join(CONFIG['paths']['branded_scss_folder'], md5, '_brand_variables.scss')
def scss_file
scss_dir.join('_brand_variables.scss')
end
def scss_dir
BrandableCSS.branded_scss_folder.join(md5)
end
def public_folder
"dist/brandable_css/#{md5}"
end
def save_file!
logger.info "saving BrandConfig: #{filename}"
FileUtils.mkdir_p(File.dirname(filename))
File.write(filename, to_scss)
def save_scss_file!
logger.info "saving brand variables file: #{scss_file}"
scss_dir.mkpath
scss_file.write(to_scss)
end
def remove_scss_file!
return unless scss_dir.exist?
logger.info "removing: #{scss_dir}"
scss_dir.rmtree
end
def compile_css!
@ -58,16 +68,22 @@ class BrandConfig < ActiveRecord::Base
end
def save_and_sync_to_s3!
save_file!
save_scss_file!
compile_css!
sync_to_s3!
end
def self.clean_up_unused
BrandConfig.
def self.destroy_if_unused(md5)
return unless md5
unused_brand_config = BrandConfig.
where(md5: md5).
where('NOT EXISTS (SELECT 1 FROM accounts WHERE brand_config_md5=brand_configs.md5)').
where('NOT share').
destroy_all
first
if unused_brand_config
unused_brand_config.destroy
unused_brand_config.remove_scss_file!
end
end
end

View File

@ -28,6 +28,10 @@ module BrandableCSS
default
end
def branded_scss_folder
Pathname.new(CONFIG['paths']['branded_scss_folder'])
end
def variants
@variants ||= CONFIG['variants'].map{|(k)| k }.freeze
end

View File

@ -3,15 +3,15 @@ namespace :brand_configs do
"Set BRAND_CONFIG_MD5=<whatever> to save just that one, otherwise writes a file for each BrandConfig in db."
task :write => :environment do
if md5 = ENV['BRAND_CONFIG_MD5']
BrandConfig.find(md5).save_file!
BrandConfig.find(md5).save_scss_file!
else
Rake::Task['brand_configs:clean'].invoke
BrandConfig.find_each(&:save_file!)
BrandConfig.find_each(&:save_scss_file!)
end
end
desc "Remove all Brand Variable scss files"
task :clean do
rm_rf BrandConfig::CONFIG['paths']['branded_scss_folder']
rm_rf BrandableCSS.branded_scss_folder
end
end