diff --git a/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml b/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml index d3c946a143..232fc30753 100644 --- a/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml +++ b/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml @@ -1082,7 +1082,7 @@ Performance/FlatMap: Performance/HashEachMethods: Enabled: true - + Performance/LstripRstrip: Enabled: true diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index e17a9d8de7..0fff134403 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -52,6 +52,12 @@ def show @node = Node.find params[:id] end + if @node.status == 3 && (current_user.nil? || @node.author != current_user) + flash[:notice] = "Only author can access the draft note" + redirect_to '/' + return + end + if @node.has_power_tag('question') redirect_to @node.path(:question) return @@ -105,20 +111,25 @@ def create @node.add_tag('event:rsvp', current_user) @node.add_tag('date:' + params[:date], current_user) if params[:date] end - if current_user.first_time_poster - AdminMailer.notify_node_moderators(@node) - flash[:first_time_post] = true - if @node.has_power_tag('question') - flash[:notice] = I18n.t('notes_controller.thank_you_for_question').html_safe + if params[:draft] != true + if current_user.first_time_poster + AdminMailer.notify_node_moderators(@node) + flash[:first_time_post] = true + if @node.has_power_tag('question') + flash[:notice] = I18n.t('notes_controller.thank_you_for_question').html_safe + else + flash[:notice] = I18n.t('notes_controller.thank_you_for_contribution').html_safe + end else - flash[:notice] = I18n.t('notes_controller.thank_you_for_contribution').html_safe + if @node.has_power_tag('question') + flash[:notice] = I18n.t('notes_controller.question_note_published').html_safe + else + flash[:notice] = I18n.t('notes_controller.research_note_published').html_safe + end end else - if @node.has_power_tag('question') - flash[:notice] = I18n.t('notes_controller.question_note_published').html_safe - else - flash[:notice] = I18n.t('notes_controller.research_note_published').html_safe - end + @node.draft + flash[:notice] = I18n.t('notes_controller.saved_as_draft').html_safe end # Notice: Temporary redirect.Remove this condition after questions show page is complete. # Just keep @node.path(:question) diff --git a/app/models/node.rb b/app/models/node.rb index 969764fd09..800eecc918 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -862,4 +862,11 @@ def self.unlike(nid , user) end count end + + # status = 3 for draft nodes,visible to author only + def draft + self.status = 3 + save + self + end end diff --git a/config/locales/controllers/notes_controller/en.yml b/config/locales/controllers/notes_controller/en.yml index 8830dbc1f8..19631adbbc 100644 --- a/config/locales/controllers/notes_controller/en.yml +++ b/config/locales/controllers/notes_controller/en.yml @@ -16,3 +16,4 @@ en: highly_liked_research_notes: "Highly liked research notes" popular_research_notes: "Popular research notes" more_than_one_contributor: "You cannot delete a wiki page once someone else has begun to contribute to it" + saved_as_draft: "Your contribution has been saved as draft and will be visible to you only" diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml index 0c528a7403..471613c8d8 100644 --- a/test/fixtures/nodes.yml +++ b/test/fixtures/nodes.yml @@ -236,3 +236,15 @@ method: type: "page" cached_likes: 0 path: "/wiki/spectrometer" + +draft: + nid: 21 + uid: 2 + title: "Draft note" + path: "/notes/test/<%= Time.now.strftime("%m-%d-%Y") %>/draft-note" + created: <%= Time.now.to_i %> + changed: <%= Time.now.to_i %> + status: 3 + type: "note" + cached_likes: 0 + slug: test-<%= Time.now.strftime("%m-%d-%Y") %>-draft-note diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 0d112fce5b..36313844df 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -625,5 +625,19 @@ def test_get_rss_feed assert_equal 'application/rss+xml', @response.content_type end + test 'draft should not be shown when no user' do + node = nodes(:draft) + post :show, id: '21',title: 'Draft note' + assert_redirected_to '/' + assert_equal "Only author can access the draft note", flash[:notice] + end + + test 'draft should not be shown when user is not author' do + node = nodes(:draft) + UserSession.create(users(:test_user)) + post :show, id: '21',title: 'Draft note' + assert_redirected_to '/' + assert_equal "Only author can access the draft note", flash[:notice] + end end diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index e9320c772a..1a53cf4dfe 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -203,7 +203,7 @@ class NodeTest < ActiveSupport::TestCase test 'should find all research notes' do notes = Node.research_notes - expected = [nodes(:one), nodes(:spam), nodes(:first_timer_note), nodes(:blog), nodes(:moderated_user_note), nodes(:activity), nodes(:upgrade)] + expected = [nodes(:one), nodes(:spam), nodes(:first_timer_note), nodes(:blog), nodes(:moderated_user_note), nodes(:activity), nodes(:upgrade), nodes(:draft)] assert_equal expected, notes end