mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 08:36:42 +08:00
Add PostCreator#create!
.
This commit is contained in:
parent
5f5e045271
commit
5fe4837e28
|
@ -1,6 +1,7 @@
|
|||
# Helper functions for dealing with errors and objects that have
|
||||
# child objects with errors
|
||||
module HasErrors
|
||||
attr_reader :errors
|
||||
|
||||
def errors
|
||||
@errors ||= ActiveModel::Errors.new(self)
|
||||
|
|
|
@ -160,6 +160,16 @@ class PostCreator
|
|||
@post
|
||||
end
|
||||
|
||||
def create!
|
||||
create
|
||||
|
||||
if !self.errors.full_messages.empty?
|
||||
raise ActiveRecord::RecordNotSaved.new("Failed to create post", self)
|
||||
end
|
||||
|
||||
@post
|
||||
end
|
||||
|
||||
def self.track_post_stats
|
||||
Rails.env != "test".freeze || @track_post_stats
|
||||
end
|
||||
|
@ -172,6 +182,10 @@ class PostCreator
|
|||
PostCreator.new(user, opts).create
|
||||
end
|
||||
|
||||
def self.create!(user, opts)
|
||||
PostCreator.new(user, opts).create!
|
||||
end
|
||||
|
||||
def self.before_create_tasks(post)
|
||||
set_reply_info(post)
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@ describe PostCreator do
|
|||
end
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
|
||||
context "new topic" do
|
||||
let(:category) { Fabricate(:category, user: user) }
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
let(:basic_topic_params) { {title: "hello world topic", raw: "my name is fred", archetype_id: 1} }
|
||||
let(:image_sizes) { {'http://an.image.host/image.jpg' => {"width" => 111, "height" => 222}} }
|
||||
|
||||
|
@ -444,7 +444,7 @@ describe PostCreator do
|
|||
|
||||
# more integration testing ... maximise our testing
|
||||
context 'existing topic' do
|
||||
let!(:topic) { Fabricate(:topic, user: user) }
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
let(:creator) { PostCreator.new(user, raw: 'test reply', topic_id: topic.id, reply_to_post_number: 4) }
|
||||
|
||||
it 'ensures the user can create the post' do
|
||||
|
@ -752,8 +752,6 @@ describe PostCreator do
|
|||
end
|
||||
|
||||
context "events" do
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
|
||||
before do
|
||||
@posts_created = 0
|
||||
@topics_created = 0
|
||||
|
@ -796,7 +794,25 @@ describe PostCreator do
|
|||
expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:watching])
|
||||
expect(topic_user.notifications_reason_id).to eq(TopicUser.notification_reasons[:auto_watch])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create!' do
|
||||
it "should return the post if it was successfully created" do
|
||||
title = "This is a valid title"
|
||||
raw = "This is a really awesome post"
|
||||
|
||||
post_creator = PostCreator.new(user, title: title, raw: raw)
|
||||
post = post_creator.create
|
||||
|
||||
expect(post).to eq(Post.last)
|
||||
expect(post.topic.title).to eq(title)
|
||||
expect(post.raw).to eq(raw)
|
||||
end
|
||||
|
||||
it "should raise an error when post fails to be created" do
|
||||
post_creator = PostCreator.new(user, title: '', raw: '')
|
||||
expect { post_creator.create! }.to raise_error(ActiveRecord::RecordNotSaved)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user