FEATURE: ability to add title to surveys

FEATURE: ability to add title to surveys

diff --git a/app/models/survey.rb b/app/models/survey.rb
index 4ce6545..fbbbfb0 100644
--- a/app/models/survey.rb
+++ b/app/models/survey.rb
@@ -26,6 +26,7 @@ end
 #  visibility    :integer          default(0), not null
 #  created_at    :datetime         not null
 #  updated_at    :datetime         not null
+#  title         :text
 #
 # Indexes
 #
diff --git a/app/models/survey_field.rb b/app/models/survey_field.rb
index 2081270..5e48270 100644
--- a/app/models/survey_field.rb
+++ b/app/models/survey_field.rb
@@ -26,14 +26,15 @@ end
 #
 # Table name: survey_fields
 #
-#  id            :bigint           not null, primary key
-#  survey_id     :bigint
-#  question      :text             not null
-#  response_type :integer          default(0), not null
-#  created_at    :datetime         not null
-#  updated_at    :datetime         not null
-#  digest        :string           not null
-#  position      :integer          default(0)
+#  id                :bigint           not null, primary key
+#  survey_id         :bigint
+#  question          :text             not null
+#  response_type     :integer          default(0), not null
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
+#  digest            :string           not null
+#  position          :integer          default(0)
+#  response_required :boolean          default(TRUE), not null
 #
 # Indexes
 #
diff --git a/app/serializers/survey_serializer.rb b/app/serializers/survey_serializer.rb
index a196151..7a4a5fd 100644
--- a/app/serializers/survey_serializer.rb
+++ b/app/serializers/survey_serializer.rb
@@ -2,6 +2,7 @@
 
 class SurveySerializer < ApplicationSerializer
   attributes :name,
+             :title,
              :active,
              :visibility,
              :fields,
diff --git a/assets/javascripts/lib/discourse-markdown/survey.js.es6 b/assets/javascripts/lib/discourse-markdown/survey.js.es6
index 3f38e12..72164dc 100644
--- a/assets/javascripts/lib/discourse-markdown/survey.js.es6
+++ b/assets/javascripts/lib/discourse-markdown/survey.js.es6
@@ -11,6 +11,7 @@ const WHITELISTED_ATTRIBUTES = [
   "question",
   "required",
   "status",
+  "title",
   "type"
 ];
 
diff --git a/assets/javascripts/widgets/discourse-survey.js.es6 b/assets/javascripts/widgets/discourse-survey.js.es6
index 1c67f74..e516086 100644
--- a/assets/javascripts/widgets/discourse-survey.js.es6
+++ b/assets/javascripts/widgets/discourse-survey.js.es6
@@ -6,6 +6,14 @@ import RawHtml from "discourse/widgets/raw-html";
 import { ajax } from "discourse/lib/ajax";
 import { popupAjaxError } from "discourse/lib/ajax-error";
 
+createWidget("discourse-survey-title", {
+  tagName: "div.survey-title",
+
+  html(attrs) {
+    return new RawHtml({ html: `<p>${attrs.title}</p>` });
+  }
+});
+
 function fieldHtml(field) {
   const $node = $(`<span>${field.question}</span>`);
   return new RawHtml({
@@ -395,6 +403,14 @@ export default createWidget("discourse-survey", {
         })
       );
     } else {
+
+      if (attrs.survey.title) {
+        contents.push(
+          this.attach("discourse-survey-title", {
+            title: attrs.survey.title
+          })
+        );
+      }
       contents.push(
         h(
           "div.survey-fields-container",
diff --git a/assets/stylesheets/common/survey.scss b/assets/stylesheets/common/survey.scss
index 97352d3..0df456c 100644
--- a/assets/stylesheets/common/survey.scss
+++ b/assets/stylesheets/common/survey.scss
@@ -14,6 +14,12 @@
     cursor: pointer;
   }
 
+  .survey-title {
+    font-size: 1.5em;
+    font-weight: bold;
+    text-align: center;
+  }
+
   .survey-fields-container {
     padding: 1.5em 2.5em;
   }
diff --git a/assets/stylesheets/mobile/survey.scss b/assets/stylesheets/mobile/survey.scss
index 31879f6..e69de29 100644
--- a/assets/stylesheets/mobile/survey.scss
+++ b/assets/stylesheets/mobile/survey.scss
@@ -1 +0,0 @@
-// mobile style
diff --git a/db/migrate/20200909052244_add_title_to_survey.rb b/db/migrate/20200909052244_add_title_to_survey.rb
new file mode 100644
index 0000000..662fb0e
--- /dev/null
+++ b/db/migrate/20200909052244_add_title_to_survey.rb
@@ -0,0 +1,5 @@
+class AddTitleToSurvey < ActiveRecord::Migration[6.0]
+  def change
+    add_column :surveys, :title, :text
+  end
+end
diff --git a/lib/discourse-surveys/helper.rb b/lib/discourse-surveys/helper.rb
index 7e6800f..19eb33d 100644
--- a/lib/discourse-surveys/helper.rb
+++ b/lib/discourse-surveys/helper.rb
@@ -13,6 +13,7 @@ module DiscourseSurveys
             post_id: post_id,
             survey_number: survey["survey_number"].presence || 1,
             name: survey["name"].presence || "survey",
+            title: survey["title"].presence || nil,
             active: survey["active"].presence || true,
             visibility: survey["public"] == "true" ? Survey.visibility[:everyone] : Survey.visibility[:secret]
           )
diff --git a/lib/discourse-surveys/survey_updater.rb b/lib/discourse-surveys/survey_updater.rb
index 21f72a8..952468f 100644
--- a/lib/discourse-surveys/survey_updater.rb
+++ b/lib/discourse-surveys/survey_updater.rb
@@ -26,6 +26,7 @@ module DiscourseSurveys
         survey_record.name = survey["name"].presence || "survey"
         survey_record.visibility = survey["public"] == "true" ? Survey.visibility[:everyone] : Survey.visibility[:secret]
         survey_record.active = survey["active"].presence || true
+        survey_record.title = survey["title"].presence || nil
         survey_record.save!
 
         response = ::SurveyResponse

GitHub sha: c52c5ced

There should be a test for that :wink: