diff --git a/app/assets/javascripts/admin/controllers/admin-site-contents-edit.js.es6 b/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 similarity index 74% rename from app/assets/javascripts/admin/controllers/admin-site-contents-edit.js.es6 rename to app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 index ef08b84ce4e..140e5537669 100644 --- a/app/assets/javascripts/admin/controllers/admin-site-contents-edit.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 @@ -4,9 +4,9 @@ export default Ember.ObjectController.extend({ saveDisabled: function() { if (this.get('saving')) { return true; } - if ((!this.get('content.allow_blank')) && Ember.empty(this.get('content.content'))) { return true; } + if ((!this.get('allow_blank')) && Ember.empty(this.get('value'))) { return true; } return false; - }.property('saving', 'content.content'), + }.property('saving', 'value'), actions: { saveChanges: function() { diff --git a/app/assets/javascripts/admin/controllers/admin-site-contents.js.es6 b/app/assets/javascripts/admin/controllers/admin-site-text.js.es6 similarity index 100% rename from app/assets/javascripts/admin/controllers/admin-site-contents.js.es6 rename to app/assets/javascripts/admin/controllers/admin-site-text.js.es6 diff --git a/app/assets/javascripts/admin/models/site_content.js b/app/assets/javascripts/admin/models/site_content.js deleted file mode 100644 index 1cc93ba7f98..00000000000 --- a/app/assets/javascripts/admin/models/site_content.js +++ /dev/null @@ -1,25 +0,0 @@ -Discourse.SiteContent = Discourse.Model.extend({ - - markdown: Em.computed.equal('format', 'markdown'), - plainText: Em.computed.equal('format', 'plain'), - html: Em.computed.equal('format', 'html'), - css: Em.computed.equal('format', 'css'), - - save: function() { - return Discourse.ajax("/admin/customize/site_contents/" + this.get('content_type'), { - type: 'PUT', - data: {content: this.get('content')} - }); - } - -}); - -Discourse.SiteContent.reopenClass({ - - find: function(type) { - return Discourse.ajax("/admin/customize/site_contents/" + type).then(function (data) { - return Discourse.SiteContent.create(data.site_content); - }); - } - -}); diff --git a/app/assets/javascripts/admin/models/site_content_type.js b/app/assets/javascripts/admin/models/site_content_type.js deleted file mode 100644 index 86b2c912157..00000000000 --- a/app/assets/javascripts/admin/models/site_content_type.js +++ /dev/null @@ -1,11 +0,0 @@ -Discourse.SiteContentType = Discourse.Model.extend(); - -Discourse.SiteContentType.reopenClass({ - findAll: function() { - return Discourse.ajax("/admin/customize/site_content_types").then(function(data) { - return data.map(function(ct) { - return Discourse.SiteContentType.create(ct); - }); - }); - } -}); diff --git a/app/assets/javascripts/admin/models/site_text.js b/app/assets/javascripts/admin/models/site_text.js new file mode 100644 index 00000000000..0d671c43340 --- /dev/null +++ b/app/assets/javascripts/admin/models/site_text.js @@ -0,0 +1,21 @@ +Discourse.SiteText = Discourse.Model.extend({ + markdown: Em.computed.equal('format', 'markdown'), + plainText: Em.computed.equal('format', 'plain'), + html: Em.computed.equal('format', 'html'), + css: Em.computed.equal('format', 'css'), + + save: function() { + return Discourse.ajax("/admin/customize/site_text/" + this.get('text_type'), { + type: 'PUT', + data: {value: this.get('value')} + }); + } +}); + +Discourse.SiteText.reopenClass({ + find: function(type) { + return Discourse.ajax("/admin/customize/site_text/" + type).then(function (data) { + return Discourse.SiteText.create(data.site_text); + }); + } +}); diff --git a/app/assets/javascripts/admin/models/site_text_type.js b/app/assets/javascripts/admin/models/site_text_type.js new file mode 100644 index 00000000000..1a181bd8e8c --- /dev/null +++ b/app/assets/javascripts/admin/models/site_text_type.js @@ -0,0 +1,11 @@ +Discourse.SiteTextType = Discourse.Model.extend(); + +Discourse.SiteTextType.reopenClass({ + findAll: function() { + return Discourse.ajax("/admin/customize/site_text_types").then(function(data) { + return data.map(function(ct) { + return Discourse.SiteTextType.create(ct); + }); + }); + } +}); diff --git a/app/assets/javascripts/admin/routes/admin-site-contents-edit.js.es6 b/app/assets/javascripts/admin/routes/admin-site-contents-edit.js.es6 deleted file mode 100644 index a658954c57b..00000000000 --- a/app/assets/javascripts/admin/routes/admin-site-contents-edit.js.es6 +++ /dev/null @@ -1,11 +0,0 @@ -export default Ember.Route.extend({ - - serialize: function(model) { - return {content_type: model.get('content_type')}; - }, - - model: function(params) { - return Discourse.SiteContent.find(params.content_type); - } - -}); diff --git a/app/assets/javascripts/admin/routes/admin-site-text-edit.js.es6 b/app/assets/javascripts/admin/routes/admin-site-text-edit.js.es6 new file mode 100644 index 00000000000..5131eff16ec --- /dev/null +++ b/app/assets/javascripts/admin/routes/admin-site-text-edit.js.es6 @@ -0,0 +1,5 @@ +export default Ember.Route.extend({ + model: function(params) { + return Discourse.SiteText.find(params.text_type); + } +}); diff --git a/app/assets/javascripts/admin/routes/admin-site-contents.js.es6 b/app/assets/javascripts/admin/routes/admin-site-text.js.es6 similarity index 57% rename from app/assets/javascripts/admin/routes/admin-site-contents.js.es6 rename to app/assets/javascripts/admin/routes/admin-site-text.js.es6 index d32f55483ea..df5cfacd894 100644 --- a/app/assets/javascripts/admin/routes/admin-site-contents.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-site-text.js.es6 @@ -1,5 +1,5 @@ export default Ember.Route.extend({ model: function() { - return Discourse.SiteContentType.findAll(); + return Discourse.SiteTextType.findAll(); } }); diff --git a/app/assets/javascripts/admin/routes/admin_routes.js b/app/assets/javascripts/admin/routes/admin_routes.js index 46ab4b6d501..535d82da79b 100644 --- a/app/assets/javascripts/admin/routes/admin_routes.js +++ b/app/assets/javascripts/admin/routes/admin_routes.js @@ -15,8 +15,8 @@ Discourse.Route.buildRoutes(function() { this.resource('adminCustomize', { path: '/customize' } ,function() { this.route('colors'); this.route('css_html'); - this.resource('adminSiteContents', { path: '/site_contents' }, function() { - this.route('edit', {path: '/:content_type'}); + this.resource('adminSiteText', { path: '/site_text' }, function() { + this.route('edit', {path: '/:text_type'}); }); }); this.route('api'); diff --git a/app/assets/javascripts/admin/templates/customize.js.handlebars b/app/assets/javascripts/admin/templates/customize.js.handlebars index 7902d4b8eac..fd690c69593 100644 --- a/app/assets/javascripts/admin/templates/customize.js.handlebars +++ b/app/assets/javascripts/admin/templates/customize.js.handlebars @@ -3,7 +3,7 @@ diff --git a/app/assets/javascripts/admin/templates/site_contents.js.handlebars b/app/assets/javascripts/admin/templates/site_text.js.handlebars similarity index 71% rename from app/assets/javascripts/admin/templates/site_contents.js.handlebars rename to app/assets/javascripts/admin/templates/site_text.js.handlebars index 6279b06afbd..de38984b2f2 100644 --- a/app/assets/javascripts/admin/templates/site_contents.js.handlebars +++ b/app/assets/javascripts/admin/templates/site_text.js.handlebars @@ -3,7 +3,7 @@ diff --git a/app/assets/javascripts/admin/templates/site_contents_edit.js.handlebars b/app/assets/javascripts/admin/templates/site_text_edit.js.handlebars similarity index 68% rename from app/assets/javascripts/admin/templates/site_contents_edit.js.handlebars rename to app/assets/javascripts/admin/templates/site_text_edit.js.handlebars index d79058d122f..d87d17bd59e 100644 --- a/app/assets/javascripts/admin/templates/site_contents_edit.js.handlebars +++ b/app/assets/javascripts/admin/templates/site_text_edit.js.handlebars @@ -2,16 +2,16 @@

{{description}}

{{#if markdown}} - {{pagedown-editor value=model.content}} + {{pagedown-editor value=value}} {{/if}} {{#if plainText}} - {{textarea value=model.content class="plain"}} + {{textarea value=value class="plain"}} {{/if}} {{#if html}} - {{aceEditor content=model.content mode="html"}} + {{aceEditor content=value mode="html"}} {{/if}} {{#if css}} - {{aceEditor content=model.content mode="css"}} + {{aceEditor content=value mode="css"}} {{/if}}
diff --git a/app/assets/javascripts/admin/templates/site_contents_index.js.handlebars b/app/assets/javascripts/admin/templates/site_text_index.js.handlebars similarity index 100% rename from app/assets/javascripts/admin/templates/site_contents_index.js.handlebars rename to app/assets/javascripts/admin/templates/site_text_index.js.handlebars diff --git a/app/controllers/admin/site_content_types_controller.rb b/app/controllers/admin/site_content_types_controller.rb deleted file mode 100644 index fd7eaf44237..00000000000 --- a/app/controllers/admin/site_content_types_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Admin::SiteContentTypesController < Admin::AdminController - - def index - render_serialized(SiteContent.content_types, SiteContentTypeSerializer) - end - -end diff --git a/app/controllers/admin/site_contents_controller.rb b/app/controllers/admin/site_contents_controller.rb deleted file mode 100644 index e900dbb5433..00000000000 --- a/app/controllers/admin/site_contents_controller.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Admin::SiteContentsController < Admin::AdminController - - def show - site_content = SiteContent.find_or_new(params[:id].to_s) - render_serialized(site_content, SiteContentSerializer) - end - - def update - site_content = SiteContent.find_or_new(params[:id].to_s) - - # Updating to nothing is the same as removing it - if params[:content].present? - site_content.content = params[:content] - site_content.save! - else - site_content.destroy - end - - render nothing: true - end -end diff --git a/app/controllers/admin/site_text_controller.rb b/app/controllers/admin/site_text_controller.rb new file mode 100644 index 00000000000..c92a0bd40df --- /dev/null +++ b/app/controllers/admin/site_text_controller.rb @@ -0,0 +1,21 @@ +class Admin::SiteTextController < Admin::AdminController + + def show + site_text = SiteText.find_or_new(params[:id].to_s) + render_serialized(site_text, SiteTextSerializer) + end + + def update + site_text = SiteText.find_or_new(params[:id].to_s) + + # Updating to nothing is the same as removing it + if params[:value].present? + site_text.value = params[:value] + site_text.save! + else + site_text.destroy + end + + render nothing: true + end +end diff --git a/app/controllers/admin/site_text_types_controller.rb b/app/controllers/admin/site_text_types_controller.rb new file mode 100644 index 00000000000..9f2efef6ba9 --- /dev/null +++ b/app/controllers/admin/site_text_types_controller.rb @@ -0,0 +1,7 @@ +class Admin::SiteTextTypesController < Admin::AdminController + + def index + render_serialized(SiteText.text_types, SiteTextTypeSerializer) + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 08b2bf346cf..00be872e02b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -256,12 +256,12 @@ class ApplicationController < ActionController::Base def custom_html_json data = { - top: SiteContent.content_for(:top), - bottom: SiteContent.content_for(:bottom) + top: SiteText.text_for(:top), + bottom: SiteText.text_for(:bottom) } if SiteSetting.tos_accept_required && !current_user - data[:tos_signup_form_message] = SiteContent.content_for(:tos_signup_form_message) + data[:tos_signup_form_message] = SiteText.text_for(:tos_signup_form_message) end if DiscoursePluginRegistry.custom_html diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index efa4d2cbf01..ee4f386ac47 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -114,7 +114,7 @@ module ApplicationHelper # Look up site content for a key. If the key is blank, you can supply a block and that # will be rendered instead. def markdown_content(key, replacements=nil) - result = PrettyText.cook(SiteContent.content_for(key, replacements || {})).html_safe + result = PrettyText.cook(SiteText.text_for(key, replacements || {})).html_safe if result.blank? && block_given? yield nil diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index e3849d92af5..4232a647972 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -17,7 +17,7 @@ class UserNotifications < ActionMailer::Base build_email(user.email, template: 'user_notifications.signup_after_approval', email_token: opts[:email_token], - new_user_tips: SiteContent.content_for(:usage_tips)) + new_user_tips: SiteText.text_for(:usage_tips)) end def authorize_email(user, opts={}) @@ -197,7 +197,7 @@ class UserNotifications < ActionMailer::Base end end - top = SiteContent.content_for(:notification_email_top) + top = SiteText.text_for(:notification_email_top) html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render( template: 'email/notification', diff --git a/app/models/site_content.rb b/app/models/site_content.rb deleted file mode 100644 index 58b241d9142..00000000000 --- a/app/models/site_content.rb +++ /dev/null @@ -1,45 +0,0 @@ -require_dependency 'site_content_type' -require_dependency 'site_content_class_methods' - -class SiteContent < ActiveRecord::Base - extend SiteContentClassMethods - - self.primary_key = 'content_type' - - validates_presence_of :content - - def self.formats - @formats ||= Enum.new(:plain, :markdown, :html, :css) - end - - add_content_type :usage_tips, default_18n_key: 'system_messages.usage_tips.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' - add_content_type :tos_miscellaneous, default_18n_key: 'terms_of_service.miscellaneous' - add_content_type :login_required_welcome_message, default_18n_key: 'login_required.welcome_message' - add_content_type :tos_signup_form_message, default_18n_key: 'terms_of_service.signup_form_message', format: :html - add_content_type :top, allow_blank: true, format: :html - add_content_type :bottom, allow_blank: true, format: :html - add_content_type :head, allow_blank: true, format: :html - add_content_type :notification_email_top, allow_blank: true, format: :markdown - - def site_content_type - @site_content_type ||= SiteContent.content_types.find {|t| t.content_type == content_type.to_sym} - end - -end - -# == Schema Information -# -# Table name: site_contents -# -# content_type :string(255) not null, primary key -# content :text not null -# created_at :datetime not null -# updated_at :datetime not null -# -# Indexes -# -# index_site_contents_on_content_type (content_type) UNIQUE -# diff --git a/app/models/site_content_type.rb b/app/models/site_content_type.rb deleted file mode 100644 index 1b825accce8..00000000000 --- a/app/models/site_content_type.rb +++ /dev/null @@ -1,27 +0,0 @@ -class SiteContentType - - attr_accessor :content_type, :format - - def initialize(content_type, format, opts=nil) - @opts = opts || {} - @content_type = content_type - @format = format - end - - def title - I18n.t("content_types.#{content_type}.title") - end - - def description - I18n.t("content_types.#{content_type}.description") - end - - def allow_blank? - !!@opts[:allow_blank] - end - - def default_content - @opts[:default_18n_key].present? ? I18n.t(@opts[:default_18n_key]) : "" - end - -end diff --git a/app/models/site_text.rb b/app/models/site_text.rb new file mode 100644 index 00000000000..acbf32983d7 --- /dev/null +++ b/app/models/site_text.rb @@ -0,0 +1,44 @@ +require_dependency 'site_text_type' +require_dependency 'site_text_class_methods' + +class SiteText < ActiveRecord::Base + extend SiteTextClassMethods + self.primary_key = 'text_type' + + validates_presence_of :value + + def self.formats + @formats ||= Enum.new(:plain, :markdown, :html, :css) + end + + add_text_type :usage_tips, default_18n_key: 'system_messages.usage_tips.text_body_template' + 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 :tos_user_content_license, default_18n_key: 'terms_of_service.user_content_license' + add_text_type :tos_miscellaneous, default_18n_key: 'terms_of_service.miscellaneous' + add_text_type :login_required_welcome_message, default_18n_key: 'login_required.welcome_message' + add_text_type :tos_signup_form_message, default_18n_key: 'terms_of_service.signup_form_message', format: :html + 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 + add_text_type :notification_email_top, allow_blank: true, format: :markdown + + def site_text_type + @site_text_type ||= SiteText.find_text_type(text_type) + end + +end + +# == Schema Information +# +# Table name: site_text +# +# text_type :string(255) not null, primary key +# value :text not null +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_site_text_on_text_type (text_type) UNIQUE +# diff --git a/app/models/site_text_type.rb b/app/models/site_text_type.rb new file mode 100644 index 00000000000..3b505e870cf --- /dev/null +++ b/app/models/site_text_type.rb @@ -0,0 +1,27 @@ +class SiteTextType + + attr_accessor :text_type, :format + + def initialize(text_type, format, opts=nil) + @opts = opts || {} + @text_type = text_type + @format = format + end + + def title + I18n.t("content_types.#{text_type}.title") + end + + def description + I18n.t("content_types.#{text_type}.description") + end + + def allow_blank? + !!@opts[:allow_blank] + end + + def default_text + @opts[:default_18n_key].present? ? I18n.t(@opts[:default_18n_key]) : "" + end + +end diff --git a/app/serializers/site_content_serializer.rb b/app/serializers/site_content_serializer.rb deleted file mode 100644 index 942b09416e9..00000000000 --- a/app/serializers/site_content_serializer.rb +++ /dev/null @@ -1,30 +0,0 @@ -class SiteContentSerializer < ApplicationSerializer - - attributes :content_type, - :title, - :description, - :content, - :format, - :allow_blank? - - def title - object.site_content_type.title - end - - def description - object.site_content_type.description - end - - def format - object.site_content_type.format - end - - def content - return object.content if object.content.present? - object.site_content_type.default_content - end - - def allow_blank? - object.site_content_type.allow_blank? - end -end diff --git a/app/serializers/site_content_type_serializer.rb b/app/serializers/site_content_type_serializer.rb deleted file mode 100644 index da906dedbd4..00000000000 --- a/app/serializers/site_content_type_serializer.rb +++ /dev/null @@ -1,13 +0,0 @@ -class SiteContentTypeSerializer < ApplicationSerializer - - attributes :content_type, :title - - def content_type - object.content_type - end - - def title - object.title - end - -end diff --git a/app/serializers/site_text_serializer.rb b/app/serializers/site_text_serializer.rb new file mode 100644 index 00000000000..31caab4b990 --- /dev/null +++ b/app/serializers/site_text_serializer.rb @@ -0,0 +1,34 @@ +class SiteTextSerializer < ApplicationSerializer + + attributes :text_type, + :title, + :description, + :value, + :format, + :allow_blank? + + def title + object.site_text_type.title + end + + def text_type + object.text_type + end + + def description + object.site_text_type.description + end + + def format + object.site_text_type.format + end + + def value + return object.value if object.value.present? + object.site_text_type.default_text + end + + def allow_blank? + object.site_text_type.allow_blank? + end +end diff --git a/app/serializers/site_text_type_serializer.rb b/app/serializers/site_text_type_serializer.rb new file mode 100644 index 00000000000..02ee847e454 --- /dev/null +++ b/app/serializers/site_text_type_serializer.rb @@ -0,0 +1,13 @@ +class SiteTextTypeSerializer < ApplicationSerializer + + attributes :text_type, :title + + def text_type + object.text_type + end + + def title + object.title + end + +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 78c0b62efc2..67dd5a727b8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,7 +18,7 @@ <%= script "admin"%> <%- end %> - <%= raw SiteContent.content_for(:head) %> + <%= raw SiteText.text_for(:head) %> <%= render_google_universal_analytics_code %> @@ -76,7 +76,7 @@ <%= yield :data %> - + <%= render :partial => "common/discourse_javascript" %> diff --git a/app/views/layouts/crawler.html.erb b/app/views/layouts/crawler.html.erb index c11d73a075f..803fd304dc1 100644 --- a/app/views/layouts/crawler.html.erb +++ b/app/views/layouts/crawler.html.erb @@ -5,7 +5,7 @@ <%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %> <%= render partial: "layouts/head" %> - <%= raw SiteContent.content_for(:head) %> + <%= raw SiteText.text_for(:head) %> <%= yield :head %> diff --git a/app/views/layouts/no_js.html.erb b/app/views/layouts/no_js.html.erb index c796b08f4f9..076b779a17a 100644 --- a/app/views/layouts/no_js.html.erb +++ b/app/views/layouts/no_js.html.erb @@ -5,7 +5,7 @@ <%=SiteSetting.title%> <%= render partial: "layouts/head" %> - <%= raw SiteContent.content_for(:head) %> + <%= raw SiteText.text_for(:head) %> <%= yield(:no_js_head) %> diff --git a/config/routes.rb b/config/routes.rb index 86bd568d30b..f5d84b627c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,8 +110,8 @@ Discourse::Application.routes.draw do post "flags/defer/:id" => "flags#defer" resources :site_customizations, constraints: AdminConstraint.new scope "/customize" do - resources :site_contents, constraints: AdminConstraint.new - resources :site_content_types, constraints: AdminConstraint.new + resources :site_text, constraints: AdminConstraint.new + resources :site_text_types, constraints: AdminConstraint.new end resources :color_schemes, constraints: AdminConstraint.new diff --git a/db/fixtures/999_topics.rb b/db/fixtures/999_topics.rb index 0c52d1acab4..cf46ccc6e1d 100644 --- a/db/fixtures/999_topics.rb +++ b/db/fixtures/999_topics.rb @@ -33,10 +33,10 @@ unless Rails.env.test? }) create_static_page_topic('guidelines_topic_id', 'guidelines_topic.title', "guidelines_topic.body", - (SiteContent.content_for(:faq) rescue nil), staff, "guidelines") + (SiteText.text_for(:faq) rescue nil), staff, "guidelines") create_static_page_topic('privacy_topic_id', 'privacy_topic.title', "privacy_topic.body", - (SiteContent.content_for(:privacy_policy) rescue nil), staff, "privacy policy") + (SiteText.text_for(:privacy_policy) rescue nil), staff, "privacy policy") end if seed_welcome_topics diff --git a/db/migrate/20140924192418_rename_content_type.rb b/db/migrate/20140924192418_rename_content_type.rb new file mode 100644 index 00000000000..31620962030 --- /dev/null +++ b/db/migrate/20140924192418_rename_content_type.rb @@ -0,0 +1,7 @@ +class RenameContentType < ActiveRecord::Migration + def change + rename_column :site_contents, :content_type, :text_type + rename_column :site_contents, :content, :value + rename_table :site_contents, :site_texts + end +end diff --git a/lib/composer_messages_finder.rb b/lib/composer_messages_finder.rb index fb128bdce0e..8d2865ac257 100644 --- a/lib/composer_messages_finder.rb +++ b/lib/composer_messages_finder.rb @@ -28,7 +28,7 @@ class ComposerMessagesFinder education_posts_text = I18n.t('education.until_posts', count: SiteSetting.educate_until_posts) return {templateName: 'composer/education', wait_for_typing: true, - body: PrettyText.cook(SiteContent.content_for(education_key, education_posts_text: education_posts_text)) } + body: PrettyText.cook(SiteText.text_for(education_key, education_posts_text: education_posts_text)) } end nil diff --git a/lib/site_content_class_methods.rb b/lib/site_content_class_methods.rb deleted file mode 100644 index 87890bc22a4..00000000000 --- a/lib/site_content_class_methods.rb +++ /dev/null @@ -1,50 +0,0 @@ -module SiteContentClassMethods - - def content_types - @types || [] - end - - def find_content_type(ct) - SiteContent.content_types.find {|t| t.content_type == ct.to_sym} - end - - def add_content_type(content_type, opts=nil) - opts ||= {} - @types ||= [] - format = opts[:format] || :markdown - @types << SiteContentType.new(content_type, format, opts) - end - - 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).find_by(content_type: content_type) - - result = "" - if site_content.blank? - ct = find_content_type(content_type) - result = ct.default_content.dup if ct.present? - else - result = site_content.content.dup - end - - result.gsub!(/\%\{[^}]+\}/) do |m| - replacements[m[2..-2].to_sym] || m - end - - result - end - - - def find_or_new(content_type) - site_content = SiteContent.find_by(content_type: content_type) - return site_content if site_content.present? - - site_content = SiteContent.new - site_content.content_type = content_type - site_content - end - -end diff --git a/lib/site_text_class_methods.rb b/lib/site_text_class_methods.rb new file mode 100644 index 00000000000..7e5dafd0daf --- /dev/null +++ b/lib/site_text_class_methods.rb @@ -0,0 +1,49 @@ +module SiteTextClassMethods + + def text_types + @types || [] + end + + def find_text_type(ct) + SiteText.text_types.find {|t| t.text_type == ct.to_sym} + end + + def add_text_type(text_type, opts=nil) + opts ||= {} + @types ||= [] + format = opts[:format] || :markdown + @types << SiteTextType.new(text_type, format, opts) + end + + def text_for(text_type, replacements=nil) + replacements ||= {} + replacements = {site_name: SiteSetting.title}.merge!(replacements) + replacements = SiteSetting.settings_hash.merge!(replacements) + + site_text = SiteText.select(:value).find_by(text_type: text_type) + + result = "" + if site_text.blank? + ct = find_text_type(text_type) + result = ct.default_text.dup if ct.present? + else + result = site_text.value.dup + end + + result.gsub!(/\%\{[^}]+\}/) do |m| + replacements[m[2..-2].to_sym] || m + end + + result + end + + def find_or_new(text_type) + site_text = SiteText.find_by(text_type: text_type) + return site_text if site_text.present? + + site_text = SiteText.new + site_text.text_type = text_type + site_text + end + +end diff --git a/lib/system_message.rb b/lib/system_message.rb index 0862a8c7abb..95e39e6d1be 100644 --- a/lib/system_message.rb +++ b/lib/system_message.rb @@ -50,7 +50,7 @@ class SystemMessage site_name: SiteSetting.title, username: @recipient.username, user_preferences_url: "#{Discourse.base_url}/users/#{@recipient.username_lower}/preferences", - new_user_tips: SiteContent.content_for(:usage_tips), + new_user_tips: SiteText.text_for(:usage_tips), site_password: "", base_url: Discourse.base_url, } diff --git a/spec/controllers/admin/site_contents_controller_spec.rb b/spec/controllers/admin/site_text_controller_spec.rb similarity index 57% rename from spec/controllers/admin/site_contents_controller_spec.rb rename to spec/controllers/admin/site_text_controller_spec.rb index 11c49e0ed9b..8944a54d738 100644 --- a/spec/controllers/admin/site_contents_controller_spec.rb +++ b/spec/controllers/admin/site_text_controller_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe Admin::SiteContentsController do +describe Admin::SiteTextController do it "is a subclass of AdminController" do - (Admin::SiteContentsController < Admin::AdminController).should be_true + (Admin::SiteTextController < Admin::AdminController).should be_true end context 'while logged in as an admin' do @@ -12,15 +12,15 @@ describe Admin::SiteContentsController do end context '.show' do - let(:content_type) { SiteContent.content_types.first.content_type } + let(:text_type) { SiteText.text_types.first.text_type } it 'returns success' do - xhr :get, :show, id: content_type + xhr :get, :show, id: text_type response.should be_success end it 'returns JSON' do - xhr :get, :show, id: content_type + xhr :get, :show, id: text_type ::JSON.parse(response.body).should be_present end end diff --git a/spec/controllers/admin/site_content_types_controller_spec.rb b/spec/controllers/admin/site_text_types_controller_spec.rb similarity index 77% rename from spec/controllers/admin/site_content_types_controller_spec.rb rename to spec/controllers/admin/site_text_types_controller_spec.rb index 4538800eb3e..13f5459bdd3 100644 --- a/spec/controllers/admin/site_content_types_controller_spec.rb +++ b/spec/controllers/admin/site_text_types_controller_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe Admin::SiteContentTypesController do +describe Admin::SiteTextTypesController do it "is a subclass of AdminController" do - (Admin::SiteContentTypesController < Admin::AdminController).should be_true + (Admin::SiteTextTypesController < Admin::AdminController).should be_true end context 'while logged in as an admin' do diff --git a/spec/fabricators/site_content_fabricator.rb b/spec/fabricators/site_content_fabricator.rb deleted file mode 100644 index 2d78864c17a..00000000000 --- a/spec/fabricators/site_content_fabricator.rb +++ /dev/null @@ -1,14 +0,0 @@ -Fabricator(:site_content) do - content "%{flower} are red. %{food} are blue." - content_type "great.poem" -end - -Fabricator(:site_content_basic, from: :site_content) do - content_type "breaking.bad" - content "best show ever" -end - -Fabricator(:site_content_site_setting, from: :site_content) do - content_type "site.replacement" - content "%{title} is evil." -end diff --git a/spec/fabricators/site_text_fabricator.rb b/spec/fabricators/site_text_fabricator.rb new file mode 100644 index 00000000000..34023da8f63 --- /dev/null +++ b/spec/fabricators/site_text_fabricator.rb @@ -0,0 +1,14 @@ +Fabricator(:site_text) do + text_type "great.poem" + value "%{flower} are red. %{food} are blue." +end + +Fabricator(:site_text_basic, from: :site_text) do + text_type "breaking.bad" + value "best show ever" +end + +Fabricator(:site_text_site_setting, from: :site_text) do + text_type "site.replacement" + value "%{title} is evil." +end diff --git a/spec/models/site_content_spec.rb b/spec/models/site_content_spec.rb deleted file mode 100644 index faf35c81ffc..00000000000 --- a/spec/models/site_content_spec.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'spec_helper' - -describe SiteContent do - - it { should validate_presence_of :content } - - - describe "#content_for" do - - it "returns an empty string for a missing content_type" do - SiteContent.content_for('breaking.bad').should == "" - end - - it "returns the default value for a content type with a default" do - SiteContent.content_for("usage_tips").should be_present - end - - context "without replacements" do - let!(:site_content) { Fabricate(:site_content_basic) } - - it "returns the simple string" do - SiteContent.content_for('breaking.bad').should == "best show ever" - end - - end - - context "with replacements" do - let!(:site_content) { Fabricate(:site_content) } - let(:replacements) { {flower: 'roses', food: 'grapes'} } - - it "returns the correct string with replacements" do - SiteContent.content_for('great.poem', replacements).should == "roses are red. grapes are blue." - end - - it "doesn't mind extra keys in the replacements" do - SiteContent.content_for('great.poem', replacements.merge(extra: 'key')).should == "roses are red. grapes are blue." - end - - it "ignores missing keys" do - SiteContent.content_for('great.poem', flower: 'roses').should == "roses are red. %{food} are blue." - end - end - - - context "replacing site_settings" do - let!(:site_content) { Fabricate(:site_content_site_setting) } - - it "replaces site_settings by default" do - SiteSetting.stubs(:title).returns("Evil Trout") - SiteContent.content_for('site.replacement').should == "Evil Trout is evil." - end - - it "allows us to override the default site settings" do - SiteSetting.stubs(:title).returns("Evil Trout") - SiteContent.content_for('site.replacement', title: 'Good Tuna').should == "Good Tuna is evil." - end - - end - - end - -end diff --git a/spec/models/site_text_spec.rb b/spec/models/site_text_spec.rb new file mode 100644 index 00000000000..0ff9ecc3bd7 --- /dev/null +++ b/spec/models/site_text_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +describe SiteText do + + it { should validate_presence_of :value } + + + describe "#text_for" do + + it "returns an empty string for a missing text_type" do + SiteText.text_for('breaking.bad').should == "" + end + + it "returns the default value for a text` type with a default" do + SiteText.text_for("usage_tips").should be_present + end + + context "without replacements" do + let!(:site_text) { Fabricate(:site_text_basic) } + + it "returns the simple string" do + SiteText.text_for('breaking.bad').should == "best show ever" + end + + end + + context "with replacements" do + let!(:site_text) { Fabricate(:site_text) } + let(:replacements) { {flower: 'roses', food: 'grapes'} } + + it "returns the correct string with replacements" do + SiteText.text_for('great.poem', replacements).should == "roses are red. grapes are blue." + end + + it "doesn't mind extra keys in the replacements" do + SiteText.text_for('great.poem', replacements.merge(extra: 'key')).should == "roses are red. grapes are blue." + end + + it "ignores missing keys" do + SiteText.text_for('great.poem', flower: 'roses').should == "roses are red. %{food} are blue." + end + end + + + context "replacing site_settings" do + let!(:site_text) { Fabricate(:site_text_site_setting) } + + it "replaces site_settings by default" do + SiteSetting.stubs(:title).returns("Evil Trout") + SiteText.text_for('site.replacement').should == "Evil Trout is evil." + end + + it "allows us to override the default site settings" do + SiteSetting.stubs(:title).returns("Evil Trout") + SiteText.text_for('site.replacement', title: 'Good Tuna').should == "Good Tuna is evil." + end + + end + + end + +end