From a14f62766e7cfd6bb2ea722927f72e72e2d1bfab Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 5 Apr 2013 16:47:20 -0400 Subject: [PATCH] Replace MultisiteI18n hack with SiteContent and admin editing. --- app/controllers/education_controller.rb | 11 ++++-- app/models/site_content.rb | 2 - app/models/site_content_type.rb | 4 +- lib/multisite_i18n.rb | 31 ---------------- lib/site_content_class_methods.rb | 2 + lib/system_message.rb | 9 ++--- spec/components/multisite_i18n_spec.rb | 37 ------------------- spec/controllers/education_controller_spec.rb | 2 +- 8 files changed, 16 insertions(+), 82 deletions(-) delete mode 100644 lib/multisite_i18n.rb delete mode 100644 spec/components/multisite_i18n_spec.rb diff --git a/app/controllers/education_controller.rb b/app/controllers/education_controller.rb index c2c6762c1d1..8b7933cd6f9 100644 --- a/app/controllers/education_controller.rb +++ b/app/controllers/education_controller.rb @@ -6,11 +6,16 @@ class EducationController < ApplicationController raise Discourse::InvalidAccess.new unless params[:id] =~ /^[a-z0-9\-\_]+$/ raise Discourse::NotFound.new if I18n.t("education.#{params[:id]}", default: MISSING_KEY) == MISSING_KEY + education_posts_text = I18n.t('education.until_posts', count: SiteSetting.educate_until_posts) - markdown_content = MultisiteI18n.t("education.#{params[:id]}", - site_name: SiteSetting.title, - education_posts_text: education_posts_text) + markdown_content = "" + if params[:id] == 'new-topic' + markdown_content = SiteContent.content_for(:education_new_topic, education_posts_text: education_posts_text) + else + markdown_content = SiteContent.content_for(:education_new_reply, education_posts_text: education_posts_text) + end + render text: PrettyText.cook(markdown_content) end diff --git a/app/models/site_content.rb b/app/models/site_content.rb index 70dddaa6f7b..f9d7370000e 100644 --- a/app/models/site_content.rb +++ b/app/models/site_content.rb @@ -13,8 +13,6 @@ class SiteContent < ActiveRecord::Base end add_content_type :usage_tips, default_18n_key: 'system_messages.usage_tips.text_body_template' - add_content_type :welcome_user, default_18n_key: 'system_messages.welcome_user.text_body_template' - add_content_type :welcome_invite, default_18n_key: 'system_messages.welcome_invite.text_body_template' add_content_type :education_new_topic, default_18n_key: 'education.new-topic' add_content_type :education_new_reply, default_18n_key: 'education.new-reply' add_content_type :tos_user_content_license, default_18n_key: 'terms_of_service.user_content_license' diff --git a/app/models/site_content_type.rb b/app/models/site_content_type.rb index 32f42cbf9e8..9b562f44f2a 100644 --- a/app/models/site_content_type.rb +++ b/app/models/site_content_type.rb @@ -1,5 +1,3 @@ -require_dependency 'multisite_i18n' - class SiteContentType attr_accessor :content_type, :format @@ -20,7 +18,7 @@ class SiteContentType def default_content if @opts[:default_18n_key].present? - return MultisiteI18n.t(@opts[:default_18n_key]) + return I18n.t(@opts[:default_18n_key]) end "" end diff --git a/lib/multisite_i18n.rb b/lib/multisite_i18n.rb deleted file mode 100644 index a50ae6fceea..00000000000 --- a/lib/multisite_i18n.rb +++ /dev/null @@ -1,31 +0,0 @@ -# Allow us to override i18n keys based on the current site you're viewing. -module MultisiteI18n - - class << self - - # It would be nice if there was an easier way to detect if a key is missing. - def translation_or_nil(key, opts) - missing_text = "missing multisite translation" - result = I18n.t(key, opts.merge(default: missing_text)) - return nil if result == missing_text - result - end - - def site_translate(current_site, key, opts=nil) - opts ||= {} - translation = MultisiteI18n.translation_or_nil("#{current_site || ""}.#{key}", opts) - if translation.blank? - return I18n.t(key, opts) - else - return translation - end - end - - def t(*args) - MultisiteI18n.site_translate(RailsMultisite::ConnectionManagement.current_db, *args) - end - - alias :translate :t - end - -end diff --git a/lib/site_content_class_methods.rb b/lib/site_content_class_methods.rb index fc1333cb19c..5e4055d1feb 100644 --- a/lib/site_content_class_methods.rb +++ b/lib/site_content_class_methods.rb @@ -17,8 +17,10 @@ module SiteContentClassMethods def content_for(content_type, replacements=nil) replacements ||= {} + replacements = {site_name: SiteSetting.title}.merge!(replacements) replacements = SiteSetting.settings_hash.merge!(replacements) + site_content = SiteContent.select(:content).where(content_type: content_type).first result = "" diff --git a/lib/system_message.rb b/lib/system_message.rb index 883cd72c09e..c7ce4bf1ed9 100644 --- a/lib/system_message.rb +++ b/lib/system_message.rb @@ -1,6 +1,5 @@ # Handle sending a message to a user from the system. require_dependency 'post_creator' -require_dependency 'multisite_i18n' class SystemMessage @@ -17,18 +16,18 @@ class SystemMessage defaults = {site_name: SiteSetting.title, username: @recipient.username, user_preferences_url: "#{Discourse.base_url}/users/#{@recipient.username_lower}/preferences", - new_user_tips: MultisiteI18n.t("system_messages.usage_tips.text_body_template"), + new_user_tips: SiteContent.content_for(:usage_tips), site_password: "", base_url: Discourse.base_url} params = defaults.merge(params) if SiteSetting.access_password.present? - params[:site_password] = MultisiteI18n.t('system_messages.site_password', access_password: SiteSetting.access_password) + params[:site_password] = I18n.t('system_messages.site_password', access_password: SiteSetting.access_password) end - title = MultisiteI18n.t("system_messages.#{type}.subject_template", params) - raw_body = MultisiteI18n.t("system_messages.#{type}.text_body_template", params) + title = I18n.t("system_messages.#{type}.subject_template", params) + raw_body = I18n.t("system_messages.#{type}.text_body_template", params) PostCreator.create(SystemMessage.system_user, raw: raw_body, diff --git a/spec/components/multisite_i18n_spec.rb b/spec/components/multisite_i18n_spec.rb deleted file mode 100644 index 662fbd4042a..00000000000 --- a/spec/components/multisite_i18n_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -require 'spec_helper' -require_dependency 'multisite_i18n' - -describe MultisiteI18n do - - before do - I18n.stubs(:t).with('test', {}).returns('default i18n') - MultisiteI18n.stubs(:translation_or_nil).with("default.test", {}).returns(nil) - MultisiteI18n.stubs(:translation_or_nil).with("other_site.test", {}).returns("overwritten i18n") - end - - context "no value for a multisite key" do - it "it returns the default i18n key" do - MultisiteI18n.site_translate('default', 'test').should == "default i18n" - end - end - - context "with a value for the multisite key" do - it "returns the overwritten value" do - MultisiteI18n.site_translate('other_site', 'test').should == "overwritten i18n" - end - end - - context "when we call t, it uses the current site" do - - it "returns the original" do - MultisiteI18n.t('test').should == 'default i18n' - end - - it "returns the overwritten" do - RailsMultisite::ConnectionManagement.stubs(:current_db).returns('other_site') - MultisiteI18n.t('test').should == "overwritten i18n" - end - - end - -end diff --git a/spec/controllers/education_controller_spec.rb b/spec/controllers/education_controller_spec.rb index ab2c8cc6988..cc2af3deec2 100644 --- a/spec/controllers/education_controller_spec.rb +++ b/spec/controllers/education_controller_spec.rb @@ -26,7 +26,7 @@ describe EducationController do let(:html_content) {"HTML Content"} before do - MultisiteI18n.expects(:t).with("education.new-topic", anything).returns(markdown_content) + SiteContent.expects(:content_for).with(:education_new_topic, anything).returns(markdown_content) PrettyText.expects(:cook).with(markdown_content).returns(html_content) xhr :get, :show, id: 'new-topic' end