Allow removal of collection element (#1036)
* Allow removal of collection element * Update tests
This commit is contained in:
parent
359cc71b64
commit
1aa8e70a5b
4 changed files with 19 additions and 6 deletions
|
|
@ -70,6 +70,8 @@ module Api
|
|||
end
|
||||
if params["article"]["series"].present?
|
||||
params["article"]["collection_id"] = Collection.find_series(params["article"]["series"], current_user)&.id
|
||||
elsif params["article"]["series"] == ""
|
||||
params["article"]["collection_id"] = nil
|
||||
end
|
||||
params.require(:article).permit(
|
||||
:title, :body_markdown, :user_id, :main_image, :published, :description,
|
||||
|
|
|
|||
|
|
@ -398,6 +398,7 @@ class Article < ApplicationRecord
|
|||
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.collection_id = nil if front_matter["title"].present?
|
||||
self.collection_id = Collection.find_series(front_matter["series"], user).id if front_matter["series"].present?
|
||||
if front_matter["automatically_renew"].present? && tag_list.include?("hiring")
|
||||
self.automatically_renew = front_matter["automatically_renew"]
|
||||
|
|
|
|||
|
|
@ -479,11 +479,5 @@ RSpec.describe Article, type: :model do
|
|||
article.collection_id = collection.id
|
||||
expect(article).to be_valid
|
||||
end
|
||||
|
||||
it "is not valid as part of a collection that does not belong to user" do
|
||||
collection = Collection.create(user_id: 32443, slug: "yoyoyo")
|
||||
article.collection_id = collection.id
|
||||
expect(article).not_to be_valid
|
||||
end
|
||||
end
|
||||
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
||||
|
|
|
|||
|
|
@ -132,5 +132,21 @@ RSpec.describe "ArticlesApi", type: :request do
|
|||
}
|
||||
expect(Article.last.title).to eq(new_title)
|
||||
end
|
||||
it "allows collection to be assigned via api" do
|
||||
new_title = "NEW TITLE #{rand(100)}"
|
||||
collection = Collection.create(user_id: @article.user_id, slug: "yoyoyo")
|
||||
put "/api/articles/#{@article.id}", params: {
|
||||
article: { title: new_title, body_markdown: "Yo ho ho#{rand(100)}", tag_list: "yo", collection_id: collection.id }
|
||||
}
|
||||
expect(Article.last.collection_id).to eq(collection.id)
|
||||
end
|
||||
it "does not allow collection which is not of user" do
|
||||
new_title = "NEW TITLE #{rand(100)}"
|
||||
collection = Collection.create(user_id: 3333, slug: "yoyoyo")
|
||||
put "/api/articles/#{@article.id}", params: {
|
||||
article: { title: new_title, body_markdown: "Yo ho ho#{rand(100)}", tag_list: "yo", collection_id: collection.id }
|
||||
}
|
||||
expect(Article.last.collection_id).not_to eq(collection.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue