Add description to articles create/update API (#2858)
This commit is contained in:
parent
7f28a0e44b
commit
61a6c7f17c
3 changed files with 47 additions and 8 deletions
|
|
@ -73,7 +73,7 @@ module Api
|
|||
def article_params
|
||||
allowed_params = [
|
||||
:title, :body_markdown, :published, :series, :publish_under_org,
|
||||
:main_image, :canonical_url, tags: []
|
||||
:main_image, :canonical_url, :description, tags: []
|
||||
]
|
||||
params.require(:article).permit(allowed_params)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -474,7 +474,6 @@ class Article < ApplicationRecord
|
|||
end
|
||||
|
||||
def evaluate_front_matter(front_matter)
|
||||
token_msg = body_text[0..80] + "..."
|
||||
self.title = front_matter["title"] if front_matter["title"].present?
|
||||
if front_matter["tags"].present?
|
||||
ActsAsTaggableOn::Taggable::Cache.included(Article)
|
||||
|
|
@ -488,7 +487,7 @@ class Article < ApplicationRecord
|
|||
self.published_at = parsed_date(front_matter["date"]) if published
|
||||
self.main_image = front_matter["cover_image"] if front_matter["cover_image"].present?
|
||||
self.canonical_url = front_matter["canonical_url"] if front_matter["canonical_url"].present?
|
||||
self.description = front_matter["description"] || token_msg
|
||||
self.description = front_matter["description"] || description || "#{body_text[0..80]}..."
|
||||
self.collection_id = nil if front_matter["title"].present?
|
||||
self.collection_id = Collection.find_series(front_matter["series"], user).id if front_matter["series"].present?
|
||||
self.automatically_renew = front_matter["automatically_renew"] if front_matter["automatically_renew"].present? && tag_list.include?("hiring")
|
||||
|
|
|
|||
|
|
@ -293,6 +293,43 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
|||
end.to change(Article, :count).by(1)
|
||||
expect(Article.find(json_response["id"]).canonical_url).to eq(canonical_url)
|
||||
end
|
||||
|
||||
it "creates an article with the given description" do
|
||||
description = "this is a very interesting article"
|
||||
expect do
|
||||
post_article(
|
||||
title: Faker::Book.title + rand(100).to_s,
|
||||
body_markdown: "Yo ho ho #{rand(100)}",
|
||||
description: description,
|
||||
)
|
||||
expect(response).to have_http_status(:created)
|
||||
end.to change(Article, :count).by(1)
|
||||
expect(Article.find(json_response["id"]).description).to eq(description)
|
||||
end
|
||||
|
||||
it "creates an article with description in the front matter" do
|
||||
description = "this is a very interesting article"
|
||||
body_markdown = file_fixture("article_published_canonical_url.txt").read
|
||||
expect do
|
||||
post_article(
|
||||
body_markdown: body_markdown,
|
||||
description: description,
|
||||
)
|
||||
expect(response).to have_http_status(:created)
|
||||
end.to change(Article, :count).by(1)
|
||||
expect(Article.find(json_response["id"]).description).not_to eq(description)
|
||||
end
|
||||
|
||||
it "creates an article with a part of the body as a description" do
|
||||
expect do
|
||||
post_article(
|
||||
title: Faker::Book.title + rand(100).to_s,
|
||||
body_markdown: "yooo" * 100 + rand(100).to_s,
|
||||
)
|
||||
expect(response).to have_http_status(:created)
|
||||
end.to change(Article, :count).by(1)
|
||||
expect(Article.find(json_response["id"]).description).to eq("yooo" * 20 + "y...")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -415,11 +452,7 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
|||
it "removes the article from a series if asked explicitly" do
|
||||
body_markdown = "Yo ho ho #{rand(100)}"
|
||||
|
||||
# for some weird reason the front matter's title resets the collection
|
||||
article.update!(
|
||||
body_markdown: body_markdown,
|
||||
collection: create(:collection, user: user),
|
||||
)
|
||||
article.update!(body_markdown: body_markdown, collection: create(:collection, user: user))
|
||||
expect(article.collection).not_to be_nil
|
||||
|
||||
put_article(
|
||||
|
|
@ -450,6 +483,13 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
|||
expect(response).to have_http_status(:ok)
|
||||
expect(article.reload.published).to be(true)
|
||||
end
|
||||
|
||||
it "updates a description" do
|
||||
description = "this is a very interesting article"
|
||||
put_article(description: description)
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(Article.find(json_response["id"]).description).to eq(description)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue