From 66846c7a17af7852abc499393b38b44f87fbd831 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Mon, 14 Feb 2022 14:38:29 -0500 Subject: [PATCH] Fix NoMethodError on Nil for article preview (#16569) * Fix NoMethodError on Nil for article preview * Remove commented out debugging code --- app/controllers/articles_controller.rb | 14 +++++++++++--- spec/requests/editor_spec.rb | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index f24b205ee..e43e65a01 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -90,11 +90,19 @@ class ArticlesController < ApplicationController format.json { render json: @article.errors, status: :unprocessable_entity } else format.json do + front_matter = parsed.front_matter.to_h + if front_matter["tags"] + tags = Article.new.tag_list.add(front_matter["tags"], parser: ActsAsTaggableOn::TagParser) + end + if front_matter["cover_image"] + cover_image = ApplicationController.helpers.cloud_cover_url(front_matter["cover_image"]) + end + render json: { processed_html: processed_html, - title: parsed["title"], - tags: (Article.new.tag_list.add(parsed["tags"], parser: ActsAsTaggableOn::TagParser) if parsed["tags"]), - cover_image: (ApplicationController.helpers.cloud_cover_url(parsed["cover_image"]) if parsed["cover_image"]) + title: front_matter["title"], + tags: tags, + cover_image: cover_image }, status: :ok end end diff --git a/spec/requests/editor_spec.rb b/spec/requests/editor_spec.rb index fe4bc07b4..ae3f84b4c 100644 --- a/spec/requests/editor_spec.rb +++ b/spec/requests/editor_spec.rb @@ -66,5 +66,24 @@ RSpec.describe "Editor", type: :request do expect(response.media_type).to eq("application/json") end end + + context "with front matter" do + it "returns successfully" do + sign_in user + article_body = <<~MARKDOWN + --- + --- + + Hello + MARKDOWN + + post "/articles/preview", + headers: headers, + params: { article_body: article_body }, + as: :json + + expect(response).to be_successful + end + end end end