Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def create
elsif @comment.unreviewed?
redirect_to_all_comments(@commentable)
else
redirect_to_comment(@comment, { view_full_work: (params[:view_full_work] == "true"), page: params[:page] })
redirect_to_comment(@comment, { page: params[:page] })
end
end
end
Expand Down Expand Up @@ -614,7 +614,6 @@ def show_comments
# so we're being extra-nice and preserving any intention to comment along with the show comments option
options = {show_comments: true}
options[:add_comment_reply_id] = params[:add_comment_reply_id] if params[:add_comment_reply_id]
options[:view_full_work] = params[:view_full_work] if params[:view_full_work]
options[:page] = params[:page]
redirect_to_all_comments(@commentable, options)
end
Expand Down Expand Up @@ -644,7 +643,6 @@ def add_comment_reply
options[:controller] = @commentable.class.to_s.underscore.pluralize
options[:anchor] = "comment_#{params[:id]}"
options[:page] = params[:page]
options[:view_full_work] = params[:view_full_work]
if @thread_view
options[:id] = @thread_root
options[:add_comment_reply_id] = params[:id]
Expand Down Expand Up @@ -706,51 +704,61 @@ def cancel_comment_delete
# if necessary to display it
def redirect_to_comment(comment, options = {})
if comment.depth > ArchiveConfig.COMMENT_THREAD_MAX_DEPTH
if comment.ultimate_parent.is_a?(Tag)
default_options = {
controller: :comments,
action: :show,
id: comment.commentable.id,
tag_id: comment.ultimate_parent.to_param,
anchor: "comment_#{comment.id}"
}
else
default_options = {
controller: comment.commentable.class.to_s.underscore.pluralize,
action: :show,
id: (comment.commentable.is_a?(Tag) ? comment.commentable.to_param : comment.commentable.id),
anchor: "comment_#{comment.id}"
}
end
default_options = if comment.ultimate_parent.is_a?(Tag)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rubocop formatting changes

{
controller: :comments,
action: :show,
id: comment.commentable.id,
tag_id: comment.ultimate_parent.to_param,
anchor: "comment_#{comment.id}"
}
else
{
controller: comment.commentable.class.to_s.underscore.pluralize,
action: :show,
id: (comment.commentable.is_a?(Tag) ? comment.commentable.to_param : comment.commentable.id),
anchor: "comment_#{comment.id}"
}
end
# display the comment's direct parent (and its associated thread)
redirect_to(url_for(default_options.merge(options)))
else
# need to redirect to the specific chapter; redirect_to_all will then retrieve full work view if applicable
redirect_to_all_comments(comment.parent, options.merge({show_comments: true, anchor: "comment_#{comment.id}"}))
redirect_to_all_comments(comment.parent, options.merge({ show_comments: true, anchor: "comment_#{comment.id}" }))
end
end

def redirect_to_all_comments(commentable, options = {})
default_options = {anchor: "comments"}
default_options = { anchor: "comments" }
options = default_options.merge(options)

if commentable.is_a?(Tag)
redirect_to comments_path(tag_id: commentable.to_param,
add_comment_reply_id: options[:add_comment_reply_id],
delete_comment_id: options[:delete_comment_id],
page: options[:page],
anchor: options[:anchor])
add_comment_reply_id: options[:add_comment_reply_id],
delete_comment_id: options[:delete_comment_id],
page: options[:page],
anchor: options[:anchor])
else
if commentable.is_a?(Chapter) && (options[:view_full_work] || current_user.try(:preference).try(:view_full_works))
commentable = commentable.work
end
is_coming_from_chapter_view = request.referer&.match(/chapters/).present?
is_not_coming_from_work_view = request.referer&.match(/works/).nil?
view_full_work = if is_coming_from_chapter_view
false
elsif is_not_coming_from_work_view
current_user.try(:preference).try(:view_full_works).present?
else
true
end
commentable = commentable.work if commentable.is_a?(Chapter) && view_full_work
redirect_to polymorphic_path(commentable,
options.slice(:show_comments,
:add_comment_reply_id,
:delete_comment_id,
:view_full_work,
:anchor,
:page))
:page)
# we don't actually use params[:view_full_work] within
# the comments controller anymore.
# still pass to the redirect since it is referred to elsewhere
.merge({ view_full_work: params[:view_full_work] }))
end
end

Expand Down
6 changes: 2 additions & 4 deletions features/comments_and_kudos/comments_redirect.feature
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Scenario: Posting top level comment on a chaptered work, with view full work in
And I post a comment "Woohoo"
Then I should see "Woohoo"
And I should see "Chapter 2" within "div#chapters"
# Once you've commented, it defaults back to your preference
And I should see "Chapter 1" within "div#chapters"
And I should not see "Chapter 1" within "div#chapters"

# REPLY COMMENTS

Expand Down Expand Up @@ -115,5 +114,4 @@ Scenario: Posting top level comment on a middle chapter, while in temporary view
When I reply to a comment with "Supercalifragelistic"
Then I should see "Supercalifragelistic"
And I should see "Chapter 2" within "div#chapters"
# Once you've commented, it defaults back to your preference
And I should see "Chapter 1" within "div#chapters"
And I should not see "Chapter 1" within "div#chapters"
106 changes: 98 additions & 8 deletions spec/controllers/comments/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,56 @@
end

describe "GET #cancel_comment_delete" do
it "redirects to the comment on the commentable without an error" do
get :cancel_comment_delete, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
context "when cancelling from chapter by chapter view" do
before do
request.env["HTTP_REFERER"] = "/works/1/chapters/1"
end

it "redirects to the comment on the chapter view without an error" do
get :cancel_comment_delete, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when cancelling from the work view" do
before do
request.env["HTTP_REFERER"] = "/works/1"
end

it "redirects to the comment on the work view without an error" do
get :cancel_comment_delete, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(work_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when cancelling from comments view" do
before do
request.env["HTTP_REFERER"] = "/comments/1"
end

context "when user set preference to full work view" do
before do
known_user = create(:user)
known_user.preference.update!(view_full_works: true)
fake_login_known_user(known_user)
end

it "redirects to the comment on the work view without an error" do
get :cancel_comment_delete, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(work_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when user set preference to chapter by chapter view" do
it "redirects to the comment on the chapter view without an error" do
get :cancel_comment_delete, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end
end
end

Expand All @@ -91,10 +137,54 @@
before { fake_login_known_user(comment.pseud.user) }

context "when the format is html" do
it "redirects to the comment on the commentable without an error" do
get :cancel_comment_edit, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
context "when user is navigating from chapter view" do
before do
request.env["HTTP_REFERER"] = "/works/1/chapters/1"
end

it "redirects to the comment on the chapter view without an error" do
get :cancel_comment_edit, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when user is navigating from work view" do
before do
request.env["HTTP_REFERER"] = "/works/1"
end

it "redirects to the comment on the full work view without error" do
get :cancel_comment_edit, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(work_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when user is navigating from comment view" do
before do
request.env["HTTP_REFERER"] = "/comments/1"
end

context "when user preference is full work view" do
before do
comment.pseud.user.preference.update!(view_full_works: true)
end

it "redirects to the comment on the full work view without error" do
get :cancel_comment_edit, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(work_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end

context "when user preference is not full work view" do
it "redirects to the comment on the chapter view without error" do
get :cancel_comment_edit, params: { id: comment.id }
expect(flash[:error]).to be_nil
expect(response).to redirect_to(chapter_path(comment.commentable, show_comments: true, anchor: "comment_#{comment.id}"))
end
end
end
end

Expand Down
Loading