From 6ee2849df64e048bce387a4a289d16c7276e770f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 12 Jan 2015 19:59:43 +0100 Subject: [PATCH] move SiteText.{head,bottom} to SiteCustomization and remove redundant SiteText.top --- .../admin/models/site_customization.js | 18 +++++++-- .../admin/templates/customize_css_html.hbs | 4 ++ .../admin/views/admin_customize_view.js | 13 +++++-- .../discourse/templates/discovery.hbs | 1 - .../javascripts/discourse/templates/topic.hbs | 1 - .../discourse/templates/user/user.hbs | 1 - .../admin/site_customizations_controller.rb | 1 + app/controllers/application_controller.rb | 5 +-- app/models/site_customization.rb | 37 ++++++++----------- app/models/site_text.rb | 3 -- app/views/layouts/application.html.erb | 8 +++- app/views/layouts/crawler.html.erb | 4 +- app/views/layouts/no_js.html.erb | 4 +- config/locales/client.en.yml | 6 +++ ...172258_add_new_site_customization_types.rb | 6 +++ ...migrate_site_text_to_site_customization.rb | 23 ++++++++++++ 16 files changed, 92 insertions(+), 43 deletions(-) create mode 100644 db/migrate/20150112172258_add_new_site_customization_types.rb create mode 100644 db/migrate/20150112172259_migrate_site_text_to_site_customization.rb diff --git a/app/assets/javascripts/admin/models/site_customization.js b/app/assets/javascripts/admin/models/site_customization.js index 94a0567842c..14fdefb2fdb 100644 --- a/app/assets/javascripts/admin/models/site_customization.js +++ b/app/assets/javascripts/admin/models/site_customization.js @@ -7,7 +7,12 @@ @module Discourse **/ Discourse.SiteCustomization = Discourse.Model.extend({ - trackedProperties: ['enabled', 'name', 'stylesheet', 'header', 'footer', 'mobile_stylesheet', 'mobile_header', 'mobile_footer'], + trackedProperties: [ + 'enabled', 'name', + 'stylesheet', 'header', 'footer', + 'mobile_stylesheet', 'mobile_header', 'mobile_footer', + 'head_tag', 'body_tag' + ], description: function() { return "" + this.name + (this.enabled ? ' (*)' : ''); @@ -25,8 +30,10 @@ Discourse.SiteCustomization = Discourse.Model.extend({ if (changed) { this.set('savingStatus', ''); } return changed; - - }.property('enabled', 'name', 'stylesheet', 'header', 'footer', 'mobile_stylesheet', 'mobile_header', 'mobile_footer', 'originals'), + }.property('enabled', 'name', 'originals', + 'stylesheet', 'header', 'footer', + 'mobile_stylesheet', 'mobile_header', 'mobile_footer', + 'head_tag', 'body_tag'), startTrackingChanges: function() { var self = this; @@ -43,6 +50,7 @@ Discourse.SiteCustomization = Discourse.Model.extend({ save: function() { this.set('savingStatus', I18n.t('saving')); this.set('saving',true); + var data = { name: this.name, enabled: this.enabled, @@ -51,7 +59,9 @@ Discourse.SiteCustomization = Discourse.Model.extend({ footer: this.footer, mobile_stylesheet: this.mobile_stylesheet, mobile_header: this.mobile_header, - mobile_footer: this.mobile_footer + mobile_footer: this.mobile_footer, + head_tag: this.head_tag, + body_tag: this.body_tag }; var siteCustomization = this; diff --git a/app/assets/javascripts/admin/templates/customize_css_html.hbs b/app/assets/javascripts/admin/templates/customize_css_html.hbs index d28bdc6d352..957c7371d55 100644 --- a/app/assets/javascripts/admin/templates/customize_css_html.hbs +++ b/app/assets/javascripts/admin/templates/customize_css_html.hbs @@ -20,6 +20,8 @@
  • {{i18n 'admin.customize.css'}}
  • {{i18n 'admin.customize.header'}}
  • {{i18n 'admin.customize.footer'}}
  • +
  • {{fa-icon "file-text-o"}} {{i18n 'admin.customize.head_tag.text'}}
  • +
  • {{fa-icon "file-text-o"}} {{i18n 'admin.customize.body_tag.text'}}
  • {{fa-icon "mobile"}} {{i18n 'admin.customize.css'}}
  • {{fa-icon "mobile"}} {{i18n 'admin.customize.header'}}
  • {{fa-icon "mobile"}} {{i18n 'admin.customize.footer'}}
  • @@ -37,6 +39,8 @@ {{#if view.stylesheetActive}}{{aceEditor content=selectedItem.stylesheet mode="scss"}}{{/if}} {{#if view.headerActive}}{{aceEditor content=selectedItem.header mode="html"}}{{/if}} {{#if view.footerActive}}{{aceEditor content=selectedItem.footer mode="html"}}{{/if}} + {{#if view.headTagActive}}{{aceEditor content=selectedItem.head_tag mode="html"}}{{/if}} + {{#if view.bodyTagActive}}{{aceEditor content=selectedItem.body_tag mode="html"}}{{/if}} {{#if view.mobileStylesheetActive}}{{aceEditor content=selectedItem.mobile_stylesheet mode="scss"}}{{/if}} {{#if view.mobileHeaderActive}}{{aceEditor content=selectedItem.mobile_header mode="html"}}{{/if}} {{#if view.mobileFooterActive}}{{aceEditor content=selectedItem.mobile_footer mode="html"}}{{/if}} diff --git a/app/assets/javascripts/admin/views/admin_customize_view.js b/app/assets/javascripts/admin/views/admin_customize_view.js index 9e267b07bff..703f66514e4 100644 --- a/app/assets/javascripts/admin/views/admin_customize_view.js +++ b/app/assets/javascripts/admin/views/admin_customize_view.js @@ -15,6 +15,8 @@ Discourse.AdminCustomizeView = Discourse.View.extend({ headerActive: Em.computed.equal('selected', 'header'), footerActive: Em.computed.equal('selected', 'footer'), + headTagActive: Em.computed.equal('selected', 'head_tag'), + bodyTagActive: Em.computed.equal('selected', 'body_tag'), stylesheetActive: Em.computed.equal('selected', 'stylesheet'), mobileHeaderActive: Em.computed.equal('selected', 'mobileHeader'), mobileFooterActive: Em.computed.equal('selected', 'mobileFooter'), @@ -23,10 +25,13 @@ Discourse.AdminCustomizeView = Discourse.View.extend({ actions: { selectHeader: function() { this.set('selected', 'header'); }, selectFooter: function() { this.set('selected', 'footer'); }, + selectHeadTag: function() { this.set('selected', 'head_tag'); }, + selectBodyTag: function() { this.set('selected', 'body_tag'); }, selectStylesheet: function() { this.set('selected', 'stylesheet'); }, selectMobileHeader: function() { this.set('selected', 'mobileHeader'); }, selectMobileFooter: function() { this.set('selected', 'mobileFooter'); }, selectMobileStylesheet: function() { this.set('selected', 'mobileStylesheet'); }, + toggleMaximize: function() { this.set("maximized", !this.get("maximized")); @@ -39,16 +44,16 @@ Discourse.AdminCustomizeView = Discourse.View.extend({ }, }, - didInsertElement: function() { + _init: function() { var controller = this.get('controller'); Mousetrap.bindGlobal('mod+s', function() { controller.send("save"); return false; }); - }, + }.on("didInsertElement"), - willDestroyElement: function() { + _cleanUp: function() { Mousetrap.unbindGlobal('mod+s'); - } + }.on("willDestroyElement") }); diff --git a/app/assets/javascripts/discourse/templates/discovery.hbs b/app/assets/javascripts/discourse/templates/discovery.hbs index b370b38ee44..9fb408e9649 100644 --- a/app/assets/javascripts/discourse/templates/discovery.hbs +++ b/app/assets/javascripts/discourse/templates/discovery.hbs @@ -1,5 +1,4 @@
    - {{custom-html "top"}} {{global-notice}} {{discourse-banner user=currentUser banner=site.banner}}
    diff --git a/app/assets/javascripts/discourse/templates/topic.hbs b/app/assets/javascripts/discourse/templates/topic.hbs index 252a1cdc630..f21fe7c3259 100644 --- a/app/assets/javascripts/discourse/templates/topic.hbs +++ b/app/assets/javascripts/discourse/templates/topic.hbs @@ -1,5 +1,4 @@
    - {{custom-html "top"}} {{global-notice}} {{discourse-banner user=currentUser banner=site.banner overlay=view.hasScrolled hide=errorLoading}}
    diff --git a/app/assets/javascripts/discourse/templates/user/user.hbs b/app/assets/javascripts/discourse/templates/user/user.hbs index cccf2d5b27c..3d1a8079b2e 100644 --- a/app/assets/javascripts/discourse/templates/user/user.hbs +++ b/app/assets/javascripts/discourse/templates/user/user.hbs @@ -1,5 +1,4 @@
    - {{custom-html "top"}} {{global-notice}}
    diff --git a/app/controllers/admin/site_customizations_controller.rb b/app/controllers/admin/site_customizations_controller.rb index 73ac7e734fa..0e8ed67b778 100644 --- a/app/controllers/admin/site_customizations_controller.rb +++ b/app/controllers/admin/site_customizations_controller.rb @@ -54,6 +54,7 @@ class Admin::SiteCustomizationsController < Admin::AdminController params.require(:site_customization) .permit(:name, :stylesheet, :header, :footer, :mobile_stylesheet, :mobile_header, :mobile_footer, + :head_tag, :body_tag, :position, :enabled, :key, :stylesheet_baked) end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ed1dbd5c954..9f59caa99ed 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -274,10 +274,7 @@ class ApplicationController < ActionController::Base end def custom_html_json - data = { - top: SiteText.text_for(:top), - footer: SiteCustomization.custom_footer(session[:preview_style]) - } + data = { footer: SiteCustomization.custom_footer(session[:preview_style]) } if DiscoursePluginRegistry.custom_html data.merge! DiscoursePluginRegistry.custom_html diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index f9c6349e031..bbfe0f75c86 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -77,14 +77,11 @@ class SiteCustomization < ActiveRecord::Base end end - def self.custom_header(preview_style=nil, target=:desktop) - preview_style ||= ENABLED_KEY - lookup_field(preview_style, target, :header) - end - - def self.custom_footer(preview_style=nil, target=:dekstop) - preview_style ||= ENABLED_KEY - lookup_field(preview_style,target,:footer) + %i{header footer head_tag body_tag}.each do |name| + define_singleton_method("custom_#{name}") do |preview_style=nil, target=:desktop| + preview_style ||= ENABLED_KEY + lookup_field(preview_style, target, name) + end end def self.lookup_field(key, target, field) @@ -95,20 +92,18 @@ class SiteCustomization < ActiveRecord::Base lookup = @cache[cache_key] return lookup.html_safe if lookup - styles = - if key == ENABLED_KEY - order(:name).where(enabled:true).to_a - else - [find_by(key: key)].compact - end + styles = if key == ENABLED_KEY + order(:name).where(enabled:true).to_a + else + [find_by(key: key)].compact + end - val = - if styles.present? - styles.map do |style| - lookup = target == :mobile ? "mobile_#{field}" : field - style.send(lookup) - end.compact.join("\n") - end + val = if styles.present? + styles.map do |style| + lookup = target == :mobile ? "mobile_#{field}" : field + style.send(lookup) + end.compact.join("\n") + end (@cache[cache_key] = val || "").html_safe end diff --git a/app/models/site_text.rb b/app/models/site_text.rb index 6046bc50237..88620766a66 100644 --- a/app/models/site_text.rb +++ b/app/models/site_text.rb @@ -25,9 +25,6 @@ class SiteText < ActiveRecord::Base add_text_type :education_new_topic, default_18n_key: 'education.new-topic' add_text_type :education_new_reply, default_18n_key: 'education.new-reply' add_text_type :login_required_welcome_message, default_18n_key: 'login_required.welcome_message' - add_text_type :top, allow_blank: true, format: :html - add_text_type :bottom, allow_blank: true, format: :html - add_text_type :head, allow_blank: true, format: :html def site_text_type @site_text_type ||= SiteText.find_text_type(text_type) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index cc71444dde9..02618bfcf04 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,7 +22,9 @@ <%= script "admin"%> <%- end %> - <%= raw SiteText.text_for(:head) %> + <%- unless customization_disabled? %> + <%= raw SiteCustomization.custom_head_tag(session[:preview_style]) %> + <%- end %> <%= render_google_universal_analytics_code %> @@ -87,6 +89,8 @@ <%= render_google_analytics_code %> - <%= raw SiteText.text_for(:bottom) %> + <%- unless customization_disabled? %> + <%= raw SiteCustomization.custom_body_tag(session[:preview_style]) %> + <%- end %> diff --git a/app/views/layouts/crawler.html.erb b/app/views/layouts/crawler.html.erb index b68ad9bbf65..8775cb8c79c 100644 --- a/app/views/layouts/crawler.html.erb +++ b/app/views/layouts/crawler.html.erb @@ -5,7 +5,9 @@ <%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %> <%= render partial: "layouts/head" %> - <%= raw SiteText.text_for(:head) %> + <%- unless customization_disabled? %> + <%= raw SiteCustomization.custom_head_tag(session[:preview_style]) %> + <%- end %> <%= yield :head %> diff --git a/app/views/layouts/no_js.html.erb b/app/views/layouts/no_js.html.erb index 66c350aeae1..64999a39209 100644 --- a/app/views/layouts/no_js.html.erb +++ b/app/views/layouts/no_js.html.erb @@ -5,7 +5,9 @@ <%=SiteSetting.title%> <%= render partial: "layouts/head" %> - <%= raw SiteText.text_for(:head) %> + <%- unless customization_disabled? %> + <%= raw SiteCustomization.custom_head_tag(session[:preview_style]) %> + <%- end %> <%= yield(:no_js_head) %> diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index f5432f9bd55..d88ab5c735f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1716,6 +1716,12 @@ en: css: "CSS" header: "Header" footer: "Footer" + head_tag: + text: "" + title: "HTML that will be inserted before the tag" + body_tag: + text: "" + title: "HTML that will be inserted before the tag" override_default: "Do not include standard style sheet" enabled: "Enabled?" preview: "preview" diff --git a/db/migrate/20150112172258_add_new_site_customization_types.rb b/db/migrate/20150112172258_add_new_site_customization_types.rb new file mode 100644 index 00000000000..dcfe2db6de6 --- /dev/null +++ b/db/migrate/20150112172258_add_new_site_customization_types.rb @@ -0,0 +1,6 @@ +class AddNewSiteCustomizationTypes < ActiveRecord::Migration + def change + add_column :site_customizations, :head_tag, :text + add_column :site_customizations, :body_tag, :text + end +end diff --git a/db/migrate/20150112172259_migrate_site_text_to_site_customization.rb b/db/migrate/20150112172259_migrate_site_text_to_site_customization.rb new file mode 100644 index 00000000000..a0288528ccf --- /dev/null +++ b/db/migrate/20150112172259_migrate_site_text_to_site_customization.rb @@ -0,0 +1,23 @@ +class MigrateSiteTextToSiteCustomization < ActiveRecord::Migration + + def up + execute <<-SQL + INSERT INTO site_customizations + (name, user_id, enabled, key, created_at, updated_at, head_tag, body_tag) + VALUES ( + 'Migrated from Site Text', + -1, + 't', + '#{SecureRandom.uuid}', + now(), + now(), + (SELECT value FROM site_texts WHERE text_type = 'top' LIMIT 1), + (SELECT value FROM site_texts WHERE text_type = 'bottom' LIMIT 1) + ) + SQL + end + + def down + end + +end