Render correct editor based on article's existing format (#2856)

* Render correct editor based on article's existing format

* Add check for user in /new
This commit is contained in:
Ben Halpern 2019-05-16 15:25:43 -04:00 committed by GitHub
parent fb6904f86d
commit 6c2498b1b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 7 deletions

View file

@ -35,6 +35,7 @@ class ArticlesController < ApplicationController
def new
@user = current_user
@version = @user.editor_version if @user
@organization = @user&.organization
@tag = Tag.find_by(name: params[:template])
@prefill = params[:prefill].to_s.gsub("\\n ", "\n").gsub("\\n", "\n")
@ -77,6 +78,7 @@ class ArticlesController < ApplicationController
def edit
authorize @article
@user = @article.user
@version = @article.has_frontmatter? ? "v1" : "v2"
@organization = @user&.organization
end

View file

@ -338,7 +338,7 @@ class Article < ApplicationRecord
fixed_body_markdown = MarkdownFixer.fix_all(body_markdown)
begin
parsed = FrontMatterParser::Parser.new(:md).call(fixed_body_markdown)
parsed.front_matter["title"]
parsed.front_matter["title"].present?
rescue Psych::SyntaxError
# if frontmatter is invalid, still render editor with errors instead of 500ing
true

View file

@ -3,8 +3,8 @@
id="article-form"
data-article="<%= @article.to_json(only: %i[id title cached_tag_list published body_markdown main_image organization_id canonical_url], methods: %i[series all_series]) %>"
data-organization="<%= @organization&.to_json(only: %i[name bg_color_hex text_color_hex], methods: [:profile_image_90]) %>"
data-version="<%= @user.editor_version%>">
<form class="articleform__form articleform__form--<%= @user.editor_version %>">
data-version="<%= @version%>">
<form class="articleform__form articleform__form--<%= @version %>">
<% if @organization %>
<div class="articleform__orgsettings">
<img src="<%= @organization.profile_image_90 %>" style="opacity: <%= @article.organization ? "1.0" : "0.7" %>" alt="<%= @organization.name %> profile image">
@ -14,7 +14,7 @@
</button>
</div>
<% end %>
<% if @user.editor_version == "v2" %>
<% if @version == "v2" %>
<% if @article.main_image.present? %>
<div class="articleform__mainimage"><img src="<%= @article.main_image %>"></div>
<% end %>
@ -30,7 +30,7 @@
<textarea class="articleform__body" placeholder="Body Markdown" name="body_markdown"><%= @article.body_markdown %></textarea>
<div>
<button class="articleform__detailsButton articleform__detailsButton--image articleform__detailsButton--bottom"></button>
<% if @user.editor_version == "v2" %>
<% if @version == "v2" %>
<button class="articleform__detailsButton articleform__detailsButton--moreconfig articleform__detailsButton--bottom"></button>
<% end %>
</div>
@ -42,7 +42,7 @@
</div>
</form>
<div style="display:none">
<% if @user.editor_version == "v2" %>
<% if @version == "v2" %>
<%= render "pages/editor_guide_text", version: "2" %>
<% else %>
<%= render "pages/editor_guide_text", version: "1" %>

View file

@ -30,6 +30,5 @@
</div>
<% end %>
<% current_user.editor_version == "v1" if @article.has_frontmatter? %>
<%= javascript_pack_tag "articleForm", defer: true %>
<%= render "articles/v2_form" %>

View file

@ -305,6 +305,19 @@ RSpec.describe Article, type: :model do
expect(article.decorate.title_length_classification).to eq("short")
end
it "determines that an article has frontmatter" do
body = "---\ntitle: Hellohnnnn#{rand(1000)}\npublished: true\ntags: hiring\n---\n\nHello"
article.body_markdown = body
expect(article.has_frontmatter?).to eq(true)
end
it "determines that an article has frontmatter" do
body = "Hey hey Ho Ho"
article.body_markdown = body
expect(article.has_frontmatter?).to eq(false)
end
it "returns stripped canonical url" do
article.canonical_url = " http://google.com "
expect(article.decorate.processed_canonical_url).to eq("http://google.com")

View file

@ -122,5 +122,10 @@ RSpec.describe "Articles", type: :request do
article = create(:article, user_id: second_user.id)
expect { get "#{article.path}/manage" }.to raise_error(Pundit::NotAuthorizedError)
end
it "shows v1 if article has frontmatter" do
article = create(:article, user_id: user.id)
get "#{article.path}/edit"
expect(response.body).to include("articleform__form--v1")
end
end
end