From 5f5e045271c0c6b52485996f04759090c0509820 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Jul 2016 11:35:18 +0800 Subject: [PATCH 1/4] Update aws-sdk. --- Gemfile.lock | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e931b629980..d888c12e5f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,12 +42,12 @@ GEM activerecord (>= 3.2, < 6.0) rake (>= 10.4, < 12.0) arel (6.0.3) - aws-sdk (2.3.7) - aws-sdk-resources (= 2.3.7) - aws-sdk-core (2.3.7) + aws-sdk (2.3.22) + aws-sdk-resources (= 2.3.22) + aws-sdk-core (2.3.22) jmespath (~> 1.0) - aws-sdk-resources (2.3.7) - aws-sdk-core (= 2.3.7) + aws-sdk-resources (2.3.22) + aws-sdk-core (= 2.3.22) babel-source (5.8.34) babel-transpiler (0.7.0) babel-source (>= 4.0, < 6) @@ -136,14 +136,12 @@ GEM progress (~> 3.0, >= 3.0.1) image_size (1.4.1) in_threads (1.3.1) - jmespath (1.2.4) - json_pure (>= 1.8.1) + jmespath (1.3.0) jquery-rails (4.0.5) rails-dom-testing (~> 1.0) railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) - json_pure (1.8.3) jwt (1.5.2) kgio (2.10.0) librarian (0.1.2) From 5fe4837e28454788e488ed87b7137005a96e89e1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Jul 2016 11:36:06 +0800 Subject: [PATCH 2/4] Add `PostCreator#create!`. --- lib/has_errors.rb | 1 + lib/post_creator.rb | 14 ++++++++++++++ spec/components/post_creator_spec.rb | 24 ++++++++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/has_errors.rb b/lib/has_errors.rb index ddf213e4869..6c5dd02a1d8 100644 --- a/lib/has_errors.rb +++ b/lib/has_errors.rb @@ -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) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 0a6075155c1..e6439270b8e 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -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) diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 1c647fef7a8..37427b60b82 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -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 From 4aa3442824edc80c04f2eeea1df8095735ed75a1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Jul 2016 11:36:29 +0800 Subject: [PATCH 3/4] Skip validations when creating a post by system user. --- lib/system_message.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/system_message.rb b/lib/system_message.rb index d38b5113f57..98280b5e9ce 100644 --- a/lib/system_message.rb +++ b/lib/system_message.rb @@ -47,12 +47,13 @@ class SystemMessage title = I18n.t("system_messages.#{type}.subject_template", params) raw = I18n.t("system_messages.#{type}.text_body_template", params) - PostCreator.create(Discourse.system_user, + PostCreator.create!(Discourse.system_user, title: title, raw: raw, archetype: Archetype.private_message, target_usernames: @recipient.username, - subtype: TopicSubtype.system_message) + subtype: TopicSubtype.system_message, + skip_validations: true) end def defaults From b981041f6f79585953fded3797c821901e5bb9dd Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Jul 2016 11:36:47 +0800 Subject: [PATCH 4/4] Make sure we log failures in `ensure` block. --- lib/backup_restore/backuper.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/backup_restore/backuper.rb b/lib/backup_restore/backuper.rb index b8047abab26..a3bfba621e5 100644 --- a/lib/backup_restore/backuper.rb +++ b/lib/backup_restore/backuper.rb @@ -52,12 +52,18 @@ module BackupRestore rescue Exception => ex log "EXCEPTION: " + ex.message log ex.backtrace.join("\n") + @success = false else @success = true "#{@archive_basename}.tar.gz" ensure - notify_user rescue nil - remove_old rescue nil + begin + notify_user + remove_old + rescue => ex + Rails.logger.error("#{ex}\n" + ex.backtrace.join("\n")) + end + clean_up @success ? log("[SUCCESS]") : log("[FAILED]") end