From d33322f8e43747efa8ea60aa88c82ec3036d1e43 Mon Sep 17 00:00:00 2001 From: Josh Puetz Date: Wed, 10 Jun 2020 09:42:15 -0500 Subject: [PATCH] Add json template to Pages (#8357) * Add json template to pages * Add jsonb field, tweak form --- app/controllers/internal/pages_controller.rb | 2 +- app/controllers/pages_controller.rb | 1 + app/controllers/stories_controller.rb | 7 ++- app/models/page.rb | 4 +- app/views/internal/pages/_form.html.erb | 48 +++++++++++++++---- .../20200609191943_add_body_json_to_page.rb | 5 ++ db/schema.rb | 3 +- spec/models/page_spec.rb | 3 +- spec/requests/pages_spec.rb | 25 ++++++++++ 9 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20200609191943_add_body_json_to_page.rb diff --git a/app/controllers/internal/pages_controller.rb b/app/controllers/internal/pages_controller.rb index 2c9cabbe6..56a93163d 100644 --- a/app/controllers/internal/pages_controller.rb +++ b/app/controllers/internal/pages_controller.rb @@ -34,7 +34,7 @@ class Internal::PagesController < Internal::ApplicationController private def page_params - allowed_params = %i[title slug body_markdown body_html description template is_top_level_path social_image] + allowed_params = %i[title slug body_markdown body_html body_json description template is_top_level_path social_image] params.require(:page).permit(allowed_params) end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 36fb9d682..7db3b9fab 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -7,6 +7,7 @@ class PagesController < ApplicationController not_found unless FeatureFlag.accessible?(@page.feature_flag_name, current_user) set_surrogate_key_header "show-page-#{params[:slug]}" + render json: @page.processed_html if @page.template == "json" end def about diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index aeaebab3c..d8b1d2b4f 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -157,7 +157,12 @@ class StoriesController < ApplicationController def handle_page_display @story_show = true set_surrogate_key_header "show-page-#{params[:username]}" - render template: "pages/show" + + if @page.template == "json" + render json: @page.processed_html + else + render template: "pages/show" + end end def handle_base_index diff --git a/app/models/page.rb b/app/models/page.rb index 9bddc3107..30ad96026 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,5 +1,5 @@ class Page < ApplicationRecord - TEMPLATE_OPTIONS = %w[contained full_within_layout].freeze + TEMPLATE_OPTIONS = %w[contained full_within_layout json].freeze validates :title, presence: true validates :description, presence: true @@ -38,7 +38,7 @@ class Page < ApplicationRecord end def body_present - errors.add(:body_markdown, "must exist if body_html doesn't exist.") if body_markdown.blank? && body_html.blank? + errors.add(:body_markdown, "must exist if body_html or body_json doesn't exist.") if body_markdown.blank? && body_html.blank? && body_json.blank? end def unique_slug_including_users_and_orgs diff --git a/app/views/internal/pages/_form.html.erb b/app/views/internal/pages/_form.html.erb index 085f5b57a..539f3d344 100644 --- a/app/views/internal/pages/_form.html.erb +++ b/app/views/internal/pages/_form.html.erb @@ -10,16 +10,25 @@ <%= form.text_field :slug, class: "form-control" %>
+ <%= form.label :description %> + <%= form.text_field :description, class: "form-control" %> +
+
+ <%= form.label :template %> + <%= form.select :template, Page::TEMPLATE_OPTIONS, class: "form-control" %> +

(Determines the way page's body will be embedded in the layout)

+
+
<%= form.label :body_markdown %> <%= form.text_area :body_markdown, class: "form-control" %>
-
+
<%= form.label :body_html %> (Only if not using markdown. HTML is dangerous ⚠️) <%= form.text_area :body_html, class: "form-control" %>
-
- <%= form.label :description %> - <%= form.text_field :description, class: "form-control" %> +
+ <%= form.label :body_json %> (For use with template type json) + <%= form.text_area :body_json, class: "form-control" %>
<% if @page.social_image_url %> @@ -28,11 +37,6 @@ <%= form.label :social_image %> <%= form.file_field :social_image, class: "form-control" %>
-
- <%= form.label :template %> - <%= form.select :template, Page::TEMPLATE_OPTIONS, class: "form-control" %> -

(Determines the way page's body will be embedded in the layout)

-
<%= form.label :is_top_level_path %> <%= form.check_box :is_top_level_path %> @@ -60,3 +64,29 @@ <% end %>
+ + diff --git a/db/migrate/20200609191943_add_body_json_to_page.rb b/db/migrate/20200609191943_add_body_json_to_page.rb new file mode 100644 index 000000000..689c3434e --- /dev/null +++ b/db/migrate/20200609191943_add_body_json_to_page.rb @@ -0,0 +1,5 @@ +class AddBodyJsonToPage < ActiveRecord::Migration[6.0] + def change + add_column :pages, :body_json, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index e868911a8..5e8ebe287 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_06_08_175130) do +ActiveRecord::Schema.define(version: 2020_06_09_191943) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -797,6 +797,7 @@ ActiveRecord::Schema.define(version: 2020_06_08_175130) do create_table "pages", force: :cascade do |t| t.text "body_html" + t.jsonb "body_json" t.text "body_markdown" t.datetime "created_at", null: false t.string "description" diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index 5050834d1..828cdb3c4 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -2,10 +2,11 @@ require "rails_helper" RSpec.describe Page, type: :model do describe "#validations" do - it "requires either body_markdown or body_html" do + it "requires either body_markdown, body_html, or body_json" do page = build(:page) page.body_html = nil page.body_markdown = nil + page.body_json = nil expect(page).not_to be_valid end diff --git a/spec/requests/pages_spec.rb b/spec/requests/pages_spec.rb index ba998b88b..ac6172bb0 100644 --- a/spec/requests/pages_spec.rb +++ b/spec/requests/pages_spec.rb @@ -16,6 +16,31 @@ RSpec.describe "Pages", type: :request do expect(response.body).not_to include("/page/#{page.slug}") expect(response.body).to include("stories-show") end + + context "when json template" do + let_it_be(:json_text) { "{\"foo\": \"bar\"}" } + let_it_be(:page) { create(:page, title: "sample_data", template: "json", body_html: json_text, body_markdown: nil) } + + before do + page.save! # Trigger processing of page.body_html + end + + it "returns json data " do + get "/page/#{page.slug}" + + expect(response.content_type).to eq("application/json") + expect(response.body).to include(json_text) + end + + it "returns json data for top level template" do + page.is_top_level_path = true + page.save! + get "/#{page.slug}" + + expect(response.content_type).to eq("application/json") + expect(response.body).to include(json_text) + end + end end describe "GET /about" do